tf.keras
model.summary
child model
neural networks
multi-level models

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.

python
1import tensorflow as tf
2
3inputs = tf.keras.Input(shape=(32,), name="input")
4x = tf.keras.layers.Dense(16, activation="relu", name="enc_dense_1")(inputs)
5encoded = tf.keras.layers.Dense(8, activation="relu", name="enc_dense_2")(x)
6encoder = tf.keras.Model(inputs, encoded, name="encoder")
7
8parent_inputs = tf.keras.Input(shape=(32,), name="parent_input")
9x = encoder(parent_inputs)
10outputs = tf.keras.layers.Dense(1, activation="sigmoid", name="classifier")(x)
11parent_model = tf.keras.Model(parent_inputs, outputs, name="parent_model")
12
13parent_model.summary(expand_nested=True)

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.

python
1import tensorflow as tf
2
3class Encoder(tf.keras.Model):
4    def __init__(self):
5        super().__init__()
6        self.d1 = tf.keras.layers.Dense(16, activation="relu")
7        self.d2 = tf.keras.layers.Dense(8, activation="relu")
8
9    def call(self, inputs):
10        x = self.d1(inputs)
11        return self.d2(x)
12
13class ParentModel(tf.keras.Model):
14    def __init__(self):
15        super().__init__()
16        self.encoder = Encoder()
17        self.classifier = tf.keras.layers.Dense(1, activation="sigmoid")
18
19    def call(self, inputs):
20        x = self.encoder(inputs)
21        return self.classifier(x)
22
23model = ParentModel()
24model(tf.zeros((1, 32)))
25model.summary(expand_nested=True)

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.

python
child = parent_model.get_layer("encoder")
child.summary()

This is especially useful when:

  • you want to inspect only one reusable component
  • the parent model is large and noisy
  • 'expand_nested=True still 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.

python
encoder = tf.keras.Model(inputs, encoded, name="encoder")

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.layers to 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:

python
1tf.keras.utils.plot_model(
2    parent_model,
3    show_shapes=True,
4    expand_nested=True,
5    to_file="parent_model.png",
6)

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.

Course illustration
Course illustration

All Rights Reserved.