PyTorch
model summary
deep learning
neural networks
programming tutorial

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.

Practice ML system design

To effectively work with neural networks in PyTorch, it's crucial to understand the structure and parameters of your model. In contrast to TensorFlow and Keras, which provide a straightforward method to print a model summary, PyTorch requires a more manual approach, though libraries like torchsummary can help. This article explores methods to print and understand your PyTorch model's summary, including manual techniques and utilizing helpful tools.

Understanding Model Architecture

Before delving into tools and techniques, it's essential to understand why a model summary is beneficial:

  • Network Architecture: Helps visualize the layers and their sequences.
  • Parameter Count: Provides the number of trainable parameters, aiding in model size assessment.
  • Layer Output Shapes: Assists in debugging and ensuring the network is passing data correctly.

Manual Method to Print Model Details

In PyTorch, to view a model's layers and parameters manually, you can iterate through the model's modules and their parameters.

Here's an example using a simple Convolutional Neural Network (CNN):

python
1import torch
2import torch.nn as nn
3
4class SimpleCNN(nn.Module):
5    def __init__(self):
6        super(SimpleCNN, self).__init__()
7        self.conv1 = nn.Conv2d(1, 32, 3, 1)
8        self.conv2 = nn.Conv2d(32, 64, 3, 1)
9        self.fc1 = nn.Linear(9216, 128)
10        self.fc2 = nn.Linear(128, 10)
11
12    def forward(self, x):
13        x = self.conv1(x)
14        x = self.conv2(x)
15        x = torch.flatten(x, 1)
16        x = self.fc1(x)
17        x = self.fc2(x)
18        return x
19
20model = SimpleCNN()
21print(model)

Explanation

  1. Modules: The architecture is defined in separate layers like nn.Conv2d and nn.Linear.
  2. Parameters: When printed, each layer outputs the number of input and output channels or features, kernel sizes, and other relevant configurations.
  3. Sequences vs. Modules: Unlike Keras's sequential API, PyTorch uses a class definition. This may provide more control but requires a comprehension of each module's structure.

Using torchsummary for Model Summary

To simplify model summaries, the torchsummary package provides an interface similar to Keras's model.summary(). Install it with:

bash
pip install torchsummary

Example Usage

python
1from torchsummary import summary
2
3# Assuming the model is defined and imported
4# Place the model on the desired device
5device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
6model.to(device)
7
8# Display model summary
9summary(model, input_size=(1, 28, 28))

Explanation

  • input_size: Defines the input shape. This is critical for calculating the output shapes and parameter counts.
  • Device Management: Model must be transferred to the appropriate device (CPU or GPU) before using summary.

Output

This method provides a table that includes:

  • Layer Types: Such as Convolutional, Pooling, or Linear.
  • Output Shapes: Depicting how the data's shape changes through the network.
  • Parameter Counts: Easily view trainable and non-trainable parameters.

Common Pitfalls and Considerations

  • Input Shape: Ensure the correct input shape is used for torchsummary. Mismatches can cause errors or incorrect summaries.
  • Dynamic Layers: Some models with dynamic architecture (e.g., RNNs) may not easily fit this paradigm.
  • GPU/CPU Compatibility: The model must be on the same device as specified for input_size.

Summary Table

AspectManual Methodtorchsummary Utility
ComplexityHighLow
Output DetailsOnly basic layer information & Manual parameter countComprehensive Summary (Layer Types, I/O Shapes)
Code LengthRequires loop and manual prints for full detailSingle function call
Ideal Use CaseSmall models or debugging specific layersQuick overview of complete models

Conclusion

Printing the model summary in PyTorch provides invaluable insights into your model architecture, parameter count, and potential errors. While PyTorch's default capabilities for summaries necessitate more manual work, external libraries like torchsummary streamline the process, offering clear, concise model overviews akin to TensorFlow and Keras. By leveraging these techniques and tools, you can enhance model interpretability and debugging efficiency.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.