PyTorch Model Summary – Detailed Tutorial

In this Python tutorial, we will learn How to create a PyTorch model summary in Python and we will also cover different examples related to the PyTorch model summary. Moreover, we will cover these topics.

  • PyTorch model summary
  • PyTorch model summary example
  • PyTorch model summary lstm
  • PyTorch model summary multiple inputs
  • PyTorch bert model summary
  • PyTorch lightning model summary
  • PyTorch model summary Keras

PyTorch model summary

In this section, we will learn how to create the PyTorch model summary in python.

The model summary gives us a fine visualization of our model and the aim is to provide complete information that is not provided by the print statement.

Syntax:

summary(model,input_size,batch_size=-1,show_input=False, show_hierarchical=False, max_depth=1,print_summary=False,show_parent_layers=False)

Parameters:

  • model The model which we want to use and get the summary.
  • input_size is required to make a forward pass through the network.
  • batch_size is proved and it’s printed in the summary table.
  • show_input shows the input and output shape of each layer.
  • show_hierarchical, the addition of a summary table returns the hierarchical view.
  • max_depth defines how many times it can go inside the user-defined layer to show them.
  • print_summary, when it is true it does not require a print function outside the summary method.
  • show_parennt_layers it adds a column to show the parent layer path until reaching the current layer.

Read: PyTorch Model Eval + Examples

PyTorch model summary example

In this section, we will learn about how to implement the PyTorch model summary with the help of an example.

Summary of a model that gives a fine visualization and the model summary provides the complete information.

Code:

In the following code, we will import the torch module from which we can get the summary of the model.

  • pretrainedmodel_vgg = models.vgg16() is used as a model.
  • summary(pretrainedmodel_vgg, (3, 224, 224)) it give the fine visualization and complete information of the model.
import torch
from torchvision import models
from torchsummary import summary

pretrainedmodel_vgg = models.vgg16()
summary(pretrainedmodel_vgg, (3, 224, 224))

Output:

After running the above code, we get the following output in which we can see that the summary of the model is printed on the screen.

PyTorch model summary example
PyTorch model summary example

Read: PyTorch Pretrained Model

PyTorch model summary lstm

In this section, we will learn about the PyTorch model summary lstm in python.

Before moving forward we should have some piece of knowledge about lstm. LSTM stands for long short-term memory which is well suited for making a prediction based on time-series data.

Code:

In the following code, we will import the torch module from which we can summarize the model summary.

  • class model_summary(nn.Module): is used to define a class.
  • nn.Conv2d(1, 10, kernel_size=5) is used to implement the 2d convolutional layer of CNN.
  • nn.Linear() is used to create the single-layer feed-forward network with inputs and outputs.
  • device_name = torch.device(“cuda” if torch.cuda.is_available() else “cpu”) is used as a cuda device.
  • summary(modl, (1, 28, 28)) is used to print the model summary.
import torch
import torch.nn as nn
import torch.nn.functional as fun
from torchsummary import summary
class model_summary(nn.Module):
    def __init__(self):
        super(model_summary, self).__init__()
        self.conv = nn.Conv2d(1, 10, kernel_size=5)
        self.conv1 = nn.Conv2d(10, 20, kernel_size=5)
        self.conv1_drop = nn.Dropout2d()
        self.fc = nn.Linear(320, 50)
        self.fc1 = nn.Linear(50, 10)
    def forward(self, z):
        z = fun.relu(fun.max_pool2d(self.conv(z), 2))
        z = fun.relu(fun.max_pool2d(self.conv1_drop(self.conv1(z)), 2))
        z = z.view(-1, 320)
        z = fun.relu(self.fc(z))
        z = fun.dropout(z, training=self.training)
        z = self.fc1(z)
        return fun.log_softmax(z, dim=1)
device_name = torch.device("cuda" if torch.cuda.is_available() else "cpu") 
modl = model_summary().to(device_name)
summary(modl, (1, 28, 28))

Output:

In the following output, we can see that the summary of the PyTorch model lstm is shown on the screen.

PyTorch model summary lstm
PyTorch model summary lstm

Read: PyTorch Early Stopping + Examples

PyTorch model summary multiple inputs

In this section, we will learn about the PyTorch model summary multiple inputs in python.

The model summary provides fine visualization and also provides the information that the print function does not provide. Here we give multiple inputs.

Code:

In the following code, we will import the torch module from which we can get the summary of the model.

  • multi_inputdevice = torch.device(“cuda” if torch.cuda.is_available() else “cpu”) is used as available device.
  • model = Multi_input().to(multi_inputdevice) is used as model.
  • summary(model, [(1, 18, 18), (1, 30, 30)]) is used to describe the summary of a model and we give multiple inputs in the parameter.
import torch
import torch.nn as nn
from torchsummary import summary

class Multi_input(nn.Module):
    def __init__(self):
        super(Multi_input, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(1, 1, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
        )

    def forward(self, y, z):
        a1 = self.features(y)
        a2 = self.features(z)
        return a1, a2
    
multi_inputdevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = Multi_input().to(multi_inputdevice)

summary(model, [(1, 18, 18), (1, 30, 30)])

Output:

After running the above code, we get the following output in which we can see that the summary of the model with multiple inputs is shown on the screen.

PyTorh model summary multiple inputs
PyTorch model summary multiple inputs

Read: PyTorch MSELoss – Detailed Guide

PyTorch bert model summary

In this section, we will learn about the PyTorch bert model summary in python.

Bert model is defined as a bidirectional encoder representation the model is designed for pretrained model.

The bert model can be altered with one extra layer to make the best model.

Code:

In the following code, we will import the torch module from which we can get the bert model summary.

  • torch.manual_seed(dt) is used to generate random numbers.
  • BT = BertTokenizer.from_pretrained(‘bert-base-uncased’) is used as a bert tokenizer.
  • pretrainedmodel_vgg = models.vgg16() is used as a pretrained model.
  • print(bertresult) is used to print the bert result.
  • summary(pretrainedmodel_vgg, (3, 224, 224)) is used to get the bert summary.
import torch
from torchvision import models
from torchsummary import summary
dt = 2020
torch.manual_seed(dt)
torch.backends.cudnn.deterministic = True
from transformers import BertTokenizer
pretrainedmodel_vgg = models.vgg16()
BT = BertTokenizer.from_pretrained('bert-base-uncased')
len(BT)
bertresult = BT.tokenize('Hi!! Welcome To The PythonGuides')
print(bertresult)
summary(pretrainedmodel_vgg, (3, 224, 224))

Output:

In the following output, we can see that the PyTorch bert model summary is printed on the screen.

PyTorch bert model summary
PyTorch bert model summary

Read: PyTorch Batch Normalization

PyTorch lightning model summary

In this section, we will learn about the PyTorch lightning model summary in python.

PyTorch lightning is a lightweight and open-source model. It is a python cover for machine learning researchers.

Code:

In the following code, we will import the torch module from which we can get the summary of the lightning model.

  • nn.Linear() is used to get the feed-forward network with inputs and outputs.
  • embding = self.encoder(m) is used to define the prediction or inference action.
  • def training_step(self, btch, btch_indx) is used to define the train loop which is independent of forward.
  • optim = torch.optim.Adam(self.parameters(), lr=1e-3) is used to optimize the optimizer.
  • dt = MNIST(os.getcwd(), download=True, transform=transforms.ToTensor()) is used to define the dataset.
  • traine.fit(autoencoder, DataLoader(trained), DataLoader(valid)) is used to fir the trainer.
import os
import torch
from torch import nn
import torch.nn.functional as func
from torchvision.datasets import MNIST
from torch.utils.data import DataLoader, random_split
from torchvision import transforms
import pytorch_lightning as pylig
class litauto_encoder(pylig.LightningModule):
    def __init__(self):
        super().__init__()
        self.encoder = nn.Sequential(nn.Linear(28 * 28, 128), nn.ReLU(), nn.Linear(128, 3))
        self.decoder = nn.Sequential(nn.Linear(3, 128), nn.ReLU(), nn.Linear(128, 28 * 28))

    def forward(self, m):
        embding = self.encoder(m)
        return embding

    def training_step(self, btch, btch_indx):
        m, n = btch
        m = m.view(m.size(0), -1)
        o = self.encoder(m)
        m_hat = self.decoder(o)
        losses = func.mse_loss(m_hat, m)
        self.log("train_loss", losses)
        return losses

    def configure_optimizers(self):
        optim = torch.optim.Adam(self.parameters(), lr=1e-3)
        return optim
dt = MNIST(os.getcwd(), download=True, transform=transforms.ToTensor())
trained, valid = random_split(dt, [55000, 5000])

autoencoder = litauto_encoder()
traine = pylig.Trainer()
traine.fit(autoencoder, DataLoader(trained), DataLoader(valid))
summary(litauto_encoder,(1,28,28))

Output:

After running the above code, we get the following output in which we can see that the PyTorch lightning model summary is shown on the screen.

PyTorch lightning model summary
PyTorch lightning model summary

Read: PyTorch Load Model + Examples

PyTorch model summary Keras

In this section, we will learn about the PyTorch model summary Keras in python.

Keras has a well-ordered API to view the visualization of the model which is very helpful while debugging the network.

Code:

In the following code, we will import the torch module from which we can get the model summary.

  • nn.Sequential() is used when we want certain layers sequentially.
  • nn.Conv2d() is used to change the convolution of two-dimensional data structure.
  • devce = torch.device(“cuda” if torch.cuda.is_available() else “cpu”) is used as a device .
  • modl = smplcon().to(devce) is used as a model.
  • summary(modl, [(1, 18, 18), (1, 30, 30)]) is used to explain the model summary.
import torch
import torch.nn as nn
from torchsummary import summary

class smplcon(nn.Module):
    def __init__(self):
        super(smplcon, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(1, 1, kernel_size=5, stride=1, padding=1),
            nn.ReLU(),
        )

    def forward(self, a, b):
        a1 = self.features(a)
        a2 = self.features(b)
        return a1, a2
    
devce = torch.device("cuda" if torch.cuda.is_available() else "cpu")
modl = smplcon().to(devce)

summary(modl, [(1, 18, 18), (1, 30, 30)])

Output:

After running the above code, we get the following output in which we can see that the PyTorch model summary Keras is printed on the screen.

PyTorch model summary keras
PyTorch model summary Keras

Also, take a look at some more PyTorch tutorials.

So, in this tutorial, we discussed PyTorch Model Summary and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • PyTorch model summary
  • PyTorch model summary example
  • PyTorch model summary lstm
  • PyTorch model summary multiple inputs
  • PyTorch bert model summary
  • PyTorch lightning model summary
  • PyTorch model summary Keras