How do I print the model summary in PyTorch?
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
PyTorch does not ship with a built-in model.summary() method like Keras, but it gives you enough hooks to inspect model structure clearly. In practice, most PyTorch projects use three layers of inspection: plain print(model), parameter counting, and a third-party summary tool such as torchinfo.
Start With print(model)
Every nn.Module has a readable string representation. This is the quickest sanity check when you want to confirm nested blocks, activation layers, and top-level classifier structure.
This is built in and costs nothing extra, but it does not tell you output shapes or parameter counts per layer.
Use torchinfo for a Keras-Style Summary
If you want a table with input shapes, output shapes, and parameter counts, torchinfo is the usual choice.
This is the closest PyTorch equivalent to Keras-style summaries. It is especially helpful for catching wrong spatial dimensions before a long training run.
Count Parameters Programmatically
Even when you use torchinfo, it is useful to have a lightweight parameter-count utility for experiment logs, test assertions, or CI checks.
This is a good habit because model size often changes silently during refactors, and logging the counts makes those changes visible.
Inspect Intermediate Shapes With Forward Hooks
Sometimes the model summary table is not enough because the bug lives inside a custom block. In that case, forward hooks let you inspect live shape flow during a real pass.
Hooks are excellent for debugging shape mismatches in real inputs, but they should be removed promptly so they do not pollute later runs.
Summaries for Complex Models
For transformer or multi-input models, summary tools need realistic sample input shapes. If the model takes several tensors, pass them in the shape format or input-data format expected by the summary library. If the model uses conditional branches, make sure the chosen sample input actually exercises the path you care about.
Another practical detail is device placement. If the model lives on the GPU, either create the sample input on the same device or run the summary on CPU before moving the model for training.
A Reasonable Default Workflow
A simple workflow that works well in most projects is:
print(model)for the structure.count_parameters(model)for total and trainable size.torchinfo.summary(...)when you need layer-by-layer shapes.- forward hooks only when debugging a specific issue.
That keeps model inspection lightweight most of the time and deeper only when necessary.
Common Pitfalls
The most common mistake is giving the wrong input_size to torchinfo. If the batch dimension, channel count, or sequence length is wrong, the summary error may look like a model bug when it is really an input bug.
Another issue is forgetting that print(model) shows module composition, not runtime tensor shapes. Many shape mismatches only appear during a forward pass.
Hooks can also become noisy if you register too many of them or forget to remove them after debugging. That makes logs harder to read and can create unnecessary overhead.
Finally, if you freeze layers during fine-tuning, count trainable parameters separately. Total parameters alone does not tell you what the optimizer will actually update.
Summary
- '
print(model)is the fastest built-in way to inspect a PyTorch model.' - '
torchinfois the standard option for Keras-style summary tables.' - Parameter-count utilities are useful for logs, tests, and experiment tracking.
- Forward hooks help debug runtime shapes in custom blocks.
- Always match summary inputs to the real shapes and devices your model expects.
Related reading
- How do I save a trained model in PyTorch?
- How do I save and load BatchNormalization Layer in this Tensorflow model?
- How do I set TensorFlow \`RNN\` state when state_is_tupleTrue?
- How do I set TensorFlow \`RNN\` state when state_is_tupleTrue?
- How do loss functions know for which model to compute gradients in PyTorch?
- How do you alter the size of a Pytorch Dataset?
- How do I profile a tf.data.Dataset?
- How do I resolve one hot encoding if my test data has missing values in a col?
.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.