🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEtensorflow

tensorflow Documentation

LOADING ENGINE...

model.summary()

AI & DATA SCIENCE // model-summary

model.summary() prints a table describing a Keras model's layers, their output shapes, and their number of trainable parameters.

Syntax

model.summary()

Deep Dive Course

summary() is a quick diagnostic tool for inspecting a model's architecture — it lists every layer in order, along with the shape of the tensor it outputs and how many trainable parameters, weights and biases, it contributes, finishing with a total parameter count for the whole model. It's especially useful for catching shape mismatches or unexpectedly huge parameter counts, often caused by a Flatten layer feeding into a large Dense layer, before spending time actually training the model.

1Understanding model.summary()

summary() is a quick diagnostic tool for inspecting a model's architecture — it lists every layer in order, along with the shape of the tensor it outputs and how many trainable parameters, weights and biases, it contributes, finishing with a total parameter count for the whole model. It's especially useful for catching shape mismatches or unexpectedly huge parameter counts, often caused by a Flatten layer feeding into a large Dense layer, before spending time actually training the model.

💡

A surprisingly huge total parameter count is very often caused by a Flatten layer feeding directly into a large Dense layer — check summary()'s output shapes right at that boundary first when a model's shown parameter count looks unexpectedly enormous.

editor.html
import tensorflow as tf
from tensorflow.keras import layers, Sequential

model = Sequential([
    layers.Dense(64, activation='relu', input_shape=(32,)),
    layers.Dense(10, activation='softmax')
])
model.summary()
localhost:3000

2Practical Example

Here is a real-world application of model.summary() showing how it is used in production TensorFlow code.

editor.html
import tensorflow as tf
from tensorflow.keras import layers, Sequential

model = Sequential([layers.Dense(64, activation='relu', input_shape=(32,))])
total_params = model.count_params()
print(total_params)
localhost:3000

3Best Practices

Follow these guidelines when working with model.summary():

1. Check model.summary() immediately after building a model, before training, to catch shape mismatches or unexpectedly huge parameter counts early

2. Watch the output shape column specifically at each layer boundary to confirm data is flowing through the architecture the way you intended

3. Investigate a surprisingly large parameter count at a Flatten-to-Dense boundary first, since that's the most common source of an unintentionally massive model

⚠️

Tip: A surprisingly huge total parameter count is very often caused by a Flatten layer feeding directly into a large Dense layer — check summary()'s output shapes right at that boundary first when a model's shown parameter count looks unexpectedly enormous.

editor.html
import tensorflow as tf
from tensorflow.keras import layers, Sequential

model = Sequential([
    layers.Dense(64, activation='relu', input_shape=(32,)),
    layers.Dense(10, activation='softmax')
])
model.summary()
localhost:3000

Examples

Example 01Basic Usage
import tensorflow as tf
from tensorflow.keras import layers, Sequential

model = Sequential([
    layers.Dense(64, activation='relu', input_shape=(32,)),
    layers.Dense(10, activation='softmax')
])
model.summary()
Example 02Advanced Example
import tensorflow as tf
from tensorflow.keras import layers, Sequential

model = Sequential([layers.Dense(64, activation='relu', input_shape=(32,))])
total_params = model.count_params()
print(total_params)

Best Practices

  • Check model.summary() immediately after building a model, before training, to catch shape mismatches or unexpectedly huge parameter counts early
  • Watch the output shape column specifically at each layer boundary to confirm data is flowing through the architecture the way you intended
  • Investigate a surprisingly large parameter count at a Flatten-to-Dense boundary first, since that's the most common source of an unintentionally massive model

Interview Question

Why does adding a Flatten layer right before a large Dense layer often produce a surprisingly huge total parameter count in model.summary()?

Hint: Think about what Flatten does to a multi-dimensional feature map, and how a Dense layer's parameter count depends on its input size.

Flatten collapses a multi-dimensional tensor, like a convolutional layer's output feature map with height, width, and channel dimensions, into a single long 1D vector, and that vector's length is the product of all those original dimensions, which can easily be many thousands of values even for a modestly sized feature map. A Dense layer's number of parameters is roughly its input size multiplied by its number of units, plus biases, so feeding a several-thousand-element flattened vector into a Dense layer with even a modest number of units multiplies out to a very large parameter count, often dwarfing every other layer in the model combined — this is exactly why architectures processing images typically use pooling layers to shrink the feature map's spatial dimensions before flattening, rather than flattening a large feature map directly.

Exercises

MediumPractice using model.summary() in a real scenario.
View Solution
import tensorflow as tf
from tensorflow.keras import layers, Sequential

model = Sequential([
    layers.Dense(64, activation='relu', input_shape=(32,)),
    layers.Dense(10, activation='softmax')
])
model.summary()

Frequently Asked Questions

Why does adding a Flatten layer right before a large Dense layer often produce a surprisingly huge total parameter count in model.summary()?

Flatten collapses a multi-dimensional tensor, like a convolutional layer's output feature map with height, width, and channel dimensions, into a single long 1D vector, and that vector's length is the product of all those original dimensions, which can easily be many thousands of values even for a modestly sized feature map. A Dense layer's number of parameters is roughly its input size multiplied by its number of units, plus biases, so feeding a several-thousand-element flattened vector into a Dense layer with even a modest number of units multiplies out to a very large parameter count, often dwarfing every other layer in the model combined — this is exactly why architectures processing images typically use pooling layers to shrink the feature map's spatial dimensions before flattening, rather than flattening a large feature map directly.

Related Functions

tf-keras-sequentialmodel-compiletf-reshape