How to Print Model Summary in PyTorch: Simple Guide
To print a model summary in PyTorch, use the
summary function from the torchsummary package by passing your model and input size. This shows layer details, output shapes, and parameter counts in a neat table.Syntax
The basic syntax to print a model summary in PyTorch is:
summary(model, input_size): Prints the model architecture summary.model: Your PyTorch model instance.input_size: Tuple representing the shape of a single input example (channels, height, width).
This function displays each layer's name, output shape, and number of parameters.
python
from torchsummary import summary summary(model, input_size=(channels, height, width))
Example
This example shows how to print the summary of a simple convolutional neural network using torchsummary. It demonstrates the layer types, output shapes, and parameter counts.
python
import torch import torch.nn as nn from torchsummary import summary class SimpleCNN(nn.Module): def __init__(self): super(SimpleCNN, self).__init__() self.conv1 = nn.Conv2d(3, 16, kernel_size=3, padding=1) self.pool = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(16, 32, kernel_size=3, padding=1) self.fc1 = nn.Linear(32 * 8 * 8, 10) # assuming input 3x32x32 def forward(self, x): x = self.pool(torch.relu(self.conv1(x))) x = self.pool(torch.relu(self.conv2(x))) x = x.view(-1, 32 * 8 * 8) x = self.fc1(x) return x model = SimpleCNN() summary(model, input_size=(3, 32, 32))
Output
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [1, 16, 32, 32] 448
MaxPool2d-2 [1, 16, 16, 16] 0
Conv2d-3 [1, 32, 16, 16] 4,640
MaxPool2d-4 [1, 32, 8, 8] 0
Linear-5 [1, 10] 20,490
================================================================
Total params: 25,578
Trainable params: 25,578
Non-trainable params: 0
----------------------------------------------------------------
Common Pitfalls
Common mistakes when printing model summaries in PyTorch include:
- Not installing
torchsummarybefore use. Install it viapip install torchsummary. - Passing incorrect
input_sizeshape. It must exclude batch size and be (channels, height, width). - Trying to use
summaryon models that require multiple inputs without adjustment. - Forgetting to move the model to the correct device (CPU/GPU) before summary if needed.
python
import torch from torchsummary import summary # Wrong input size example # summary(model, input_size=(32, 32, 3)) # Incorrect order # Correct input size # summary(model, input_size=(3, 32, 32))
Quick Reference
Tips for printing model summaries in PyTorch:
- Always install
torchsummaryfirst. - Use
summary(model, input_size=(channels, height, width)). - Input size excludes batch dimension.
- Works best for single input tensor models.
- Shows layer names, output shapes, and parameter counts.
Key Takeaways
Use torchsummary's summary() to print PyTorch model architecture clearly.
Pass the model and input size as (channels, height, width) without batch size.
Install torchsummary via pip before using summary().
Check input size order carefully to avoid errors.
Summary helps understand layer outputs and parameter counts quickly.