Display Tensorflow Model Summary as like in Keras
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
In TensorFlow 2.x, model.summary() works directly on Keras models (Sequential, Functional, or subclassed). For TensorFlow 1.x models or raw tf.function graphs, you can use tf.debugging.set_log_device_placement, tf.profiler, or manually iterate over tf.trainable_variables() to display a model summary. The torchsummary-style output can be achieved with the tf.keras.utils.plot_model() function for visual summaries.
Keras model.summary() (Standard Approach)
Output:
Functional API Summary
Subclassed Model Summary
Subclassed models require calling the model once with sample input before summary() works:
Visual Model Plot
Requires graphviz and pydot:
Custom Summary Function
Printing Summary to a String or File
TensorFlow 1.x: Manual Variable Summary
Common Pitfalls
- Calling summary() on an unbuilt subclassed model: Subclassed
tf.keras.Modeldoes not know its input shape until it processes data. Callmodel.build(input_shape=...)ormodel(sample_input)before callingsummary(), otherwise it raisesValueError. - Expecting summary() to show nested model details: By default,
summary()shows nested models (like a pretrained backbone) as a single line. Usemodel.summary(expand_nested=True)to recursively expand nested models and show all internal layers. - Missing graphviz for plot_model:
tf.keras.utils.plot_model()requires both the Pythonpydotpackage and the systemgraphvizbinary. Installing only the Python package without the system binary causesFileNotFoundError: dot not found. - Incorrect parameter count for shared layers: Layers used multiple times in a Functional model share parameters.
summary()shows the correct total, but counting parameters by iteratingmodel.layerscan double-count shared layers. Usemodel.count_params()for the accurate total. - Summary showing "multiple" for output shapes: When a layer has multiple outputs or is used in multiple places, the output shape column shows "multiple" instead of a concrete shape. This is expected — use
model.summary(expand_nested=True)or inspect individual layers withlayer.output_shapefor details.
Summary
- Use
model.summary()on any Keras model (Sequential, Functional, or subclassed) to display layer names, output shapes, and parameter counts - For subclassed models, call
model.build(input_shape=...)or pass sample data before callingsummary() - Use
tf.keras.utils.plot_model()to generate a visual diagram of the model architecture - Capture summary output as a string with
print_fn=lambda x: buffer.write(x) - Use
expand_nested=Trueto show internal layers of nested models - For TF1 graphs, manually iterate over
tf.trainable_variables()to display variable shapes and parameter counts
Related reading
- Displaying tf.summary.text with underscores correctly in Tensorboard
- Distribute data from tf.data.Dataset to multiple workers e.g. for Horovod
- Distributed Tensorflow good example for synchronous training on CPUs
- Distributed tensorflow the difference between In-graph replication and Between-graph replication
- Distinguishing overfitting vs good prediction
- Distributed algorithm for SVD?
- Distributed Tensorflow who applies the parameter update?
- Distributed Tensorflow who applies the parameter update?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.