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.
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()2Practical Example
Here is a real-world application of model.summary() showing how it is used in production TensorFlow code.
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)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.
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()