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:
Change view of Tensor in PyTorch
Next article icon

PyTorch Lightning with TensorBoard

Last Updated : 24 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Pytorch-Lightning is a popular deep learning framework. It basically works with PyTorch models to simplify the training and testing of the models. This library is useful for distributed training as one can train the model seamlessly without much complex codes. Now to get the metrics in an user interactive, we need TensorBoard. TensorBoard is a powerful library that provides visualizations, loss and accuracy of the model. It also helps to debug our models.

Integrating PyTorch Lightning with TensorBoard, a powerful visualization tool, enhances the ability to monitor metrics, model performance, and training progress in real time.

Table of Content

  • Setting Up TensorBoard with PyTorch Lightning
  • Why Use PyTorch Lightning with TensorBoard?
  • Logging Metrics with PyTorch Lightning to TensorBoard
  • Example: Neural Network with PyTorch Lightning and TensorBoard

Setting Up TensorBoard with PyTorch Lightning

To setup PyTorch Lightning with TensorBoard, we have to ensure that PyTorch has been installed. To install the library use the below command. We can also use pip command.

conda install pytorch torchvision torchaudio cpuonly -c pytorch

After installing PyTorch, we need to install PyTorch Lightning. To install the library use pip command:

pip install pytorch-lightning

Screenshot-2024-09-21-175118
Setting Up TensorBoard with PyTorch Lightning

Now to install the TensorBoard Library use the below command:

pip install tensorboard

Screenshot-2024-09-21-194706
Setting Up TensorBoard with PyTorch Lightning

Why Use PyTorch Lightning with TensorBoard?

Using PyTorch Lightning and TensorBoard together has multiple benefits:

  • Automated Logging: PyTorch Lightning automatically logs metrics, making it easier to monitor the training process.
  • Visualization: TensorBoard visualizes training progress, making debugging and analysis more efficient.
  • Scalability: PyTorch Lightning scales models across multiple GPUs and TPUs, while TensorBoard keeps track of metrics across these distributed systems.

Logging Metrics with PyTorch Lightning to TensorBoard

TensorBoard works hand in hand with Pytorch-Lightning. Whatever errors we log in using PyTorch Lightning, TensorBoard automatically captures the data, creates interactive visualizations and hosts them on local host. To store the results we use self.log method. This method interacts with TensorBoard and provides with the logs.

Python
x, y = batch y_hat = self(x) loss = self.loss_fn(y_hat, y) acc = (y_hat.argmax(dim=1) == y).float().mean() self.log('test_loss', loss, prog_bar=True) self.log('test_acc', acc, prog_bar=True) 

self.log method is applicable for training, testing and validation methods.

  • After running the model, we need to log in to the TensorBoard and get the details of the metrics and its corresponding visualizations.
  • The results are usually hosted on http://localhost:6006/. TensorBoard is useful as it also helps in comparison of many versions of models.

The command to log in to TensorBoard is as follows:

tensorboard --logdir=lightning_logs/

Screenshot-2024-09-21-202855
Logging Metrics with PyTorch Lightning
Screenshot-2024-09-21-203044
Sample - Visualizing Model Training in TensorBoard

Example: Neural Network with PyTorch Lightning and TensorBoard

Here we have used MNIST dataset. We have defined the class using Pytorch-Lightning.

  • In the class there are three fully connected layers and methods like forward pass, optimizing, training steps to train model, validating steps to prevent model from overfitting and testing the model.
  • On the dataset, we apply transformations like converting to tensors, normalizations etc. Finally we call the Trainer object to train and test the model.
Batch Size:32
Epochs:5
Learning Rate:0.001
Optimizer: Adam
Activation Function: ReLU
Loss: Cross Entropy
Python
import pytorch_lightning as pl import torch from torch import nn from torch.utils.data import DataLoader from torchvision import transforms, datasets  # Step 1: Define the LightningModule class LitModel(pl.LightningModule):     def __init__(self):         super().__init__()         self.layer_1 = nn.Linear(28 * 28, 128)         self.layer_2 = nn.Linear(128, 256)         self.layer_3 = nn.Linear(256, 10)         self.loss_fn = nn.CrossEntropyLoss()      def forward(self, x):         # Flatten the input (28x28 images to 784)         x = x.view(x.size(0), -1)         x = torch.relu(self.layer_1(x))         x = torch.relu(self.layer_2(x))         x = self.layer_3(x)         return x      def training_step(self, batch, batch_idx):         x, y = batch         y_hat = self(x)         loss = self.loss_fn(y_hat, y)         acc = (y_hat.argmax(dim=1) == y).float().mean()  # Accuracy for training         self.log('train_loss', loss)         self.log('train_acc', acc)  # Logging training accuracy         return loss      def validation_step(self, batch, batch_idx):         x, y = batch         y_hat = self(x)         loss = self.loss_fn(y_hat, y)         acc = (y_hat.argmax(dim=1) == y).float().mean()  # Validation accuracy         self.log('val_loss', loss, prog_bar=True)         self.log('val_acc', acc, prog_bar=True)  # Logging validation accuracy      def test_step(self, batch, batch_idx):         x, y = batch         y_hat = self(x)         loss = self.loss_fn(y_hat, y)         acc = (y_hat.argmax(dim=1) == y).float().mean()  # Testing accuracy         self.log('test_loss', loss, prog_bar=True)         self.log('test_acc', acc, prog_bar=True)  # Logging test accuracy      def configure_optimizers(self):         return torch.optim.Adam(self.parameters(), lr=1e-3)  # Step 2: Prepare Data transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))]) mnist_train = datasets.MNIST(root='.', train=True, download=True, transform=transform) mnist_test = datasets.MNIST(root='.', train=False, download=True, transform=transform)  train_loader = DataLoader(mnist_train, batch_size=32) test_loader = DataLoader(mnist_test, batch_size=32)  # Step 3: Create Trainer and Train Model model = LitModel() trainer = pl.Trainer(max_epochs=5, accelerator='cpu')  # Step 4: Train the model trainer.fit(model, train_loader, test_loader)  # Step 5: Test the model trainer.test(model, test_loader) 

Output:

Screenshot-2024-09-21-204454
Neural Network on the MNIST Dataset with PyTorch Lightning

After the complete training and testing of the model use the command 'tensorboard --logdir=lightning_logs/' as all the logs are stored in lightning_logs. Now go to the host link as provided.

As we can see that the training loss decreases with the step size and the same goes for test as well. It also shows the relative time that has been taken to train the model. The overall testing accurcy of the model is 96.67%.

Conclusion

TensorBoard is a powerful library as it captures all the logs that has been generated during the training and testing of the models. By incorporating it with PyTorch Lightning model and simply using the self.log method, all the records gets captured and using the command all those logs are hosted on the local host. TensorBoard efficiently uses those logs to create interactive visualizations thereby reducing the code size.


Next Article
Change view of Tensor in PyTorch
author
baidehi1874
Improve
Article Tags :
  • Deep Learning
  • AI-ML-DS
  • PyTorch-Lightning
  • AI-ML-DS With Python

Similar Reads

  • PyTorch vs PyTorch Lightning
    The PyTorch research team at Facebook AI Research (FAIR) introduced PyTorch Lightning to address these challenges and provide a more organized and standardized approach. In this article, we will see the major differences between PyTorch Lightning and Pytorch. Table of Content PytorchPytorch Lightnin
    9 min read
  • Tensors in Pytorch
    A Pytorch Tensor is basically the same as a NumPy array. This means it does not know anything about deep learning or computational graphs or gradients and is just a generic n-dimensional array to be used for arbitrary numeric computation. However, the biggest difference between a NumPy array and a P
    6 min read
  • Change view of Tensor in PyTorch
    In this article, we will learn how to change the shape of tensors using the PyTorch view function. We will also look at the multiple ways in which we can change the shape of the tensors. Also, we can use the view function to convert lower-dimensional matrices to higher dimensions. What is the necess
    3 min read
  • Two-Dimensional Tensors in Pytorch
    PyTorch is a python library developed by Facebook to run and train machine learning and deep learning models. In PyTorch everything is based on tensor operations. Two-dimensional tensors are nothing but matrices or vectors of two-dimension with specific datatype, of n rows and n columns. Representat
    3 min read
  • Way to Copy a Tensor in PyTorch
    In deep learning, PyTorch has become a popular framework for building and training neural networks. At the heart of PyTorch is the tensor—a multi-dimensional array that serves as the fundamental building block for all operations in the framework. There are many scenarios where you might need to copy
    5 min read
  • How to Install PyTorch Lightning
    PyTorch Lightning is a powerful and flexible framework designed to streamline the process of building complex deep learning models using PyTorch. By organizing PyTorch code, it allows researchers and engineers to focus more on research and less on boilerplate code. This article will guide you throug
    3 min read
  • Tensor Operations in PyTorch
    In this article, we will discuss tensor operations in PyTorch. PyTorch is a scientific package used to perform operations on the given data like tensor in python. A Tensor is a collection of data like a numpy array. We can create a tensor using the tensor function: Syntax: torch.tensor([[[element1,e
    5 min read
  • Creating a Tensor in Pytorch
    All the deep learning is computations on tensors, which are generalizations of a matrix that can be indexed in more than 2 dimensions. Tensors can be created from Python lists with the torch.tensor() function. The tensor() Method: To create tensors with Pytorch we can simply use the tensor() method:
    6 min read
  • What is TensorBoard?
    TensorBoard is a powerful visualization tool designed specifically for machine learning workflows. It provides insights into the training process of machine learning models, allowing developers to track and optimize the performance of their models more effectively. TensorBoard is often associated wi
    7 min read
  • How to resize a tensor in PyTorch?
    In this article, we will discuss how to resize a Tensor in Pytorch. Resize allows us to change the size of the tensor. we have multiple methods to resize a tensor in PyTorch. let's discuss the available methods. Method 1: Using view() method We can resize the tensors in PyTorch by using the view() m
    5 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