How can I use tf.keras.Model.summary to see the layers of a child model which in a father model?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Nested models are common in Keras. You might build an encoder as one Model, then plug it into a larger classifier or sequence model. The confusing part is that model.summary() does not always expand nested child models by default, and subclassed models must be built before Keras can show meaningful layer information.
Use expand_nested=True
The simplest solution is to ask Keras to expand nested models in the summary output.
With expand_nested=True, Keras prints the child model and its internal layers instead of showing only one line for the nested model object.
If you omit that flag, the child may appear as a single layer-like entry, which is often not enough when debugging shape flow or parameter counts.
The Child Model Must Be Built
summary() can only describe a model once Keras knows the layer shapes. Functional and Sequential models are usually built as soon as the graph is connected. Subclassed models are different. If you define layers in __init__ and logic in call, you usually need to build the model first.
The dummy call builds the model and its nested layers. Without that step, the summary may be incomplete or fail.
Summarize the Child Model Directly
Sometimes the best debugging step is to summarize the nested child model separately.
This is especially useful when:
- you want to inspect only one reusable component
- the parent model is large and noisy
- '
expand_nested=Truestill does not show what you need clearly'
A direct child summary is often easier to read than a giant parent summary.
Naming Helps a Lot
Nested models are easier to inspect when you name them and their layers clearly.
If every nested component is called model, model_1, or dense_7, the summary becomes harder to use. Good names make shape debugging much faster.
summary() Is Useful, but Not the Only Tool
summary() is great for parameter counts and layer order, but it is not the only inspection tool. For complex graphs, these alternatives help too:
- '
model.layersto inspect top-level layer objects in Python' - '
model.get_layer(name)to retrieve a specific nested component' - '
tf.keras.utils.plot_model(...)for a diagrammatic view'
For example:
A diagram is often easier than console text for branched or deeply nested architectures.
Common Pitfalls
The biggest pitfall is calling summary() on a subclassed model before it has been built. Keras cannot summarize unknown shapes.
Another issue is forgetting expand_nested=True and then assuming Keras cannot see the child layers at all.
Developers also sometimes inspect model.layers and expect it to flatten every nested internal layer automatically. Usually it only shows the top-level structure.
Finally, unnamed nested models make the output harder to interpret than it needs to be.
Summary
- Use
model.summary(expand_nested=True)to expand child models inside a parent summary. - Make sure subclassed models are built before calling
summary(). - Summarize the child directly with
get_layer(...).summary()when that is clearer. - Name nested models and layers deliberately to improve readability.
- Use
plot_model(..., expand_nested=True)when text output is not enough.

