How to use tensorboard for analysing and monitoring torch models

This recipe helps you use tensorboard for analysing and monitoring torch models

Recipe Objective

How to use tensorboard for analysing and monitoring torch models? As we all know what is a tensorboard, it is tool for visualizing and analyzing the data for various operations such as the loss and accuracy, model graph, histograms of weights, biases, images, text, and audio data etc. In PyTorch for analyzing the torch models we can use tensorboard and for that we are going to use SummaryWriter() for consumption and visualization of our data. For dataset, we are going to use MNIST data and try some operations onto it. Lets understand the tensorboard practically.

Step 1 - Import library

import torch
import torchvision
from torch.utils.tensorboard import SummaryWriter
from torchvision import datasets, transforms

Step 2 - Store summary writer

summary_writer = SummaryWriter()

Here we are storing the summary writer in a variable, to log the data for consumption and visualization the summary writer is the main entry class.

Step 3 - Normalization and loading dataset

transform_norm = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
train_dataset = datasets.MNIST('mnist_train', train=True, download=True, transform=transform_norm)
loader_train = torch.utils.data.DataLoader(train_dataset, batch_size=64, shuffle=True)

Step 4 - Modelling

my_model = torchvision.models.resnet50(False)
my_model.conv1 = torch.nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=False) image_data,
label = next(iter(loader_train))

Step 5 - Make the grid

make_grid = torchvision.utils.make_grid(image_data)
summary_writer.add_image('image_data', make_grid, 0)
summary_writer.add_graph(my_model, image_data)
summary_writer.close()

Step 6 - Install tensorboard

!pip install tensorboard

Step 7 - Import tensorboard and numpy

from torch.utils.tensorboard import SummaryWriter
import numpy as np

Step 8 - Perform operations

summary_writer = SummaryWriter()
for n_iter in range(100):
    summary_writer.add_scalar('Loss/train', np.random.random(), n_iter)
    summary_writer.add_scalar('Loss/test', np.random.random(), n_iter)
    summary_writer.add_scalar('Accuracy/train', np.random.random(), n_iter)
    summary_writer.add_scalar('Accuracy/test', np.random.random(), n_iter)

Step 9 - Load the tensorboard

%tensorboard --logdir runs

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

GCP MLOps Project to Deploy ARIMA Model using uWSGI Flask
Build an end-to-end MLOps Pipeline to deploy a Time Series ARIMA Model on GCP using uWSGI and Flask

Linear Regression Model Project in Python for Beginners Part 2
Machine Learning Linear Regression Project for Beginners in Python to Build a Multiple Linear Regression Model on Soccer Player Dataset.

Learn How to Build PyTorch Neural Networks from Scratch
In this deep learning project, you will learn how to build PyTorch neural networks from scratch.

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Build Portfolio Optimization Machine Learning Models in R
Machine Learning Project for Financial Risk Modelling and Portfolio Optimization with R- Build a machine learning model in R to develop a strategy for building a portfolio for maximized returns.

Build Deep Autoencoders Model for Anomaly Detection in Python
In this deep learning project , you will build and deploy a deep autoencoders model using Flask.