Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
Visualizing Feature Maps using PyTorch
Next article icon

Visualizing PyTorch Neural Networks

Last Updated : 18 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Visualizing neural network models is a crucial step in understanding their architecture, debugging, and conveying their design. PyTorch, a popular deep learning framework, offers several tools and libraries that facilitate model visualization. This article will guide you through the process of visualizing a PyTorch model using two powerful libraries: torchsummary and torchviz.

Prerequisites

Before we dive into model visualization, ensure you have the following prerequisites:

  • Python Installed: Make sure Python is installed on your system. You can download it from python.org.
  • PyTorch Library: Install PyTorch by following the instructions on the official PyTorch website.
  • torchsummary Library: This library provides a summary of the model architecture. Install it using pip:

Refer to the link : How to Print the Model Summary in PyTorch

Table of Content

  • Building a Simple Neural Network in PyTorch
  • Visualization Techniques for Neural Network in Pytorch
    • 1. Torchviz
    • 2. TensorBoard
  • Best Practices for Visualizing Neural Networks in PyTorch
    • 1. Visualize Intermediate Layers
    • 2. Regularly Monitor Training Metrics
    • 3. Visualize Feature Maps
    • 4. Debug Gradients

Visualizing the Model Architecture

Visualizing the architecture of a neural network can help you understand its structure and the flow of data through its layers. Visualizing neural networks can help you:

  • Understand the architecture and flow of data.
  • Debug and optimize the model.
  • Communicate the model's structure and performance to others.

PyTorch provides several libraries and tools to visualize neural networks, including Torchviz, Netron, and TensorBoard. These tools can generate graphical representations of the model architecture, track training metrics, and visualize activations and gradients.

Building a Simple Neural Network in PyTorch

Before diving into visualization techniques, let's first build a simple neural network using PyTorch.

Python
import torch import torch.nn as nn import torch.optim as optim  class SimpleNN(nn.Module):     def __init__(self):         super(SimpleNN, self).__init__()         self.fc1 = nn.Linear(28 * 28, 128)         self.fc2 = nn.Linear(128, 64)         self.fc3 = nn.Linear(64, 10)      def forward(self, x):         x = x.view(-1, 28 * 28)         x = torch.relu(self.fc1(x))         x = torch.relu(self.fc2(x))         x = self.fc3(x)         return x  model = SimpleNN() print(model) 

This code defines a simple feed-forward neural network with three fully connected layers. Now, let's explore different visualization techniques.

Visualization Techniques for Neural Network in Pytorch

1. Torchviz

Torchviz is a library that provides a way to visualize the computational graph of a PyTorch model.

Installation

!pip install torchviz

Usage

This code generates a graphical representation of the model's computational graph and saves it as a PNG file.

Python
from torchviz import make_dot  x = torch.randn(1, 28 * 28) y = model(x) make_dot(y, params=dict(model.named_parameters())).render("simple_nn", format="png") 

Output:

simple_nn
Torchviz

2. TensorBoard

TensorBoard is a visualization toolkit for machine learning experiments. It can be used to visualize the model architecture, training metrics, and more.

Installation

!pip install tensorboard

Usage

First, set up TensorBoard in your PyTorch code:

Python
from torch.utils.tensorboard import SummaryWriter  dummy_input = torch.randn(1, 1, 28, 28) writer = SummaryWriter('runs/simple_nn') writer.add_graph(model, dummy_input) writer.close() 

Then, launch TensorBoard within the Python environment:

Python
import tensorboard from tensorboard import notebook  notebook.start("--logdir=runs") 

Output:

Capture
launched TensorBoard

Best Practices for Visualizing Neural Networks in PyTorch

1. Visualize Intermediate Layers

Understanding how data flows through intermediate layers can help diagnose issues like vanishing gradients or identify which features are being extracted at different stages. Use hooks in PyTorch to capture and visualize activations from intermediate layers.

def hook_fn(module, input, output):
print(f"Layer: {module}, Output shape: {output.shape}")

for name, layer in model.named_modules():
layer.register_forward_hook(hook_fn)

# Forward pass
output = model(torch.randn(1, 1, 28, 28))

2. Regularly Monitor Training Metrics

Use TensorBoard to log and monitor metrics such as loss and accuracy during training. This helps in identifying overfitting, underfitting, and other training issues.

for epoch in range(num_epochs):
# Training code...
writer.add_scalar('Loss/train', loss, epoch)
writer.add_scalar('Accuracy/train', accuracy, epoch)

3. Visualize Feature Maps

Feature maps provide insights into what each convolutional layer is learning. This is particularly useful for convolutional neural networks (CNNs)

def visualize_feature_maps(model, input_image):
layers = [model.conv1, model.conv2, model.conv3]
for layer in layers:
input_image = layer(input_image)
plt.figure(figsize=(10, 10))
for i in range(input_image.shape[1]):
plt.subplot(8, 8, i+1)
plt.imshow(input_image[0, i].detach().numpy(), cmap='gray')
plt.axis('off')
plt.show()

input_image = torch.randn(1, 3, 224, 224)
visualize_feature_maps(model, input_image)

4. Debug Gradients

Visualizing gradients can help in understanding how the model is learning and identifying issues like vanishing or exploding gradients. Tools like Weights & Biases can be used for this purpose.

import wandb

wandb.init(project="pytorch-visualization")
wandb.watch(model, log="all")

Conclusion

Visualizing neural networks in PyTorch is essential for understanding and debugging models. This article covered several techniques, including Torchviz, Netron, TensorBoard, saliency maps, and class activation maps. These tools provide valuable insights into the model's architecture and performance, making it easier to develop and optimize deep learning models.


Next Article
Visualizing Feature Maps using PyTorch

P

poojashu00qn
Improve
Article Tags :
  • Blogathon
  • Deep Learning
  • AI-ML-DS
  • Python-PyTorch
  • AI-ML-DS With Python
  • Data Science Blogathon 2024

Similar Reads

  • How to Visualize PyTorch Neural Networks
    Visualizing neural networks is crucial for understanding their architecture, debugging, and optimizing models. PyTorch offers several ways to visualize both simple and complex neural networks. In this article, we'll explore how to visualize different types of neural networks, including a simple feed
    7 min read
  • Graph Neural Networks with PyTorch
    Graph Neural Networks (GNNs) represent a powerful class of machine learning models tailored for interpreting data described by graphs. This is particularly useful because many real-world structures are networks composed of interconnected elements, such as social networks, molecular structures, and c
    4 min read
  • Training Neural Networks using Pytorch Lightning
    Introduction: PyTorch Lightning is a library that provides a high-level interface for PyTorch. Problem with PyTorch is that every time you start a project you have to rewrite those training and testing loop. PyTorch Lightning fixes the problem by not only reducing boilerplate code but also providing
    7 min read
  • Training Neural Networks with Validation using PyTorch
    Neural Networks are a biologically-inspired programming paradigm that deep learning is built around. Python provides various libraries using which you can create and train neural networks over given data. PyTorch is one such library that provides us with various utilities to build and train neural n
    8 min read
  • Visualizing Feature Maps using PyTorch
    Interpreting and visualizing feature maps in PyTorch is like looking at snapshots of what's happening inside a neural network as it processes information. In this Tutorial, we will walk through interpreting and visualizing feature maps in PyTorch. What are Feature Maps?Feature maps enable us to capt
    7 min read
  • Visualization of ConvNets in Pytorch - Python
    Convolutional Neural Networks (ConvNets or CNNs) are a category of Neural Networks that have proven very effective in areas such as image recognition and classification. Understanding the behavior of ConvNets can be a complex task, especially when working with large image datasets. To help with this
    5 min read
  • A single neuron neural network in Python
    Neural networks are the core of deep learning, a field that has practical applications in many different areas. Today neural networks are used for image classification, speech recognition, object detection, etc. Now, Let's try to understand the basic unit behind all these states of art techniques.A
    3 min read
  • Types of Neural Networks
    Neural networks are computational models that mimic the way biological neural networks in the human brain process information. They consist of layers of neurons that transform the input data into meaningful outputs through a series of mathematical operations. In this article, we are going to explore
    6 min read
  • How to implement neural networks in PyTorch?
    This tutorial shows how to use PyTorch to create a basic neural network for classifying handwritten digits from the MNIST dataset. Neural networks, which are central to modern AI, enable machines to learn tasks like regression, classification, and generation. With PyTorch, you'll learn how to design
    5 min read
  • Implementing Recurrent Neural Networks in PyTorch
    Recurrent Neural Networks (RNNs) are a class of neural networks that are particularly effective for sequential data. Unlike traditional feedforward neural networks RNNs have connections that form loops allowing them to maintain a hidden state that can capture information from previous inputs. This m
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences