Tutorial on how to use TensorBoard with PyTorch

Posted by


PyTorch is a popular deep learning library that allows users to easily build neural networks. One of the key advantages of PyTorch is its seamless integration with TensorBoard, a visualization tool that helps users monitor the performance of their models during training and evaluation. In this tutorial, we will cover how to use TensorBoard with PyTorch to monitor the training process and visualize the results.

Step 1: Install TensorBoard

Before we can start using TensorBoard with PyTorch, we need to install the TensorBoard library. To do this, you can use pip to install the library:

pip install tensorboard

Step 2: Import the necessary libraries

Next, we will need to import the necessary libraries in our PyTorch script. In addition to importing the PyTorch libraries, we will also import the torch.utils.tensorboard module which provides the necessary tools for integrating TensorBoard with PyTorch.

import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.tensorboard import SummaryWriter

Step 3: Set up TensorBoard writer

Now, we need to set up a SummaryWriter which will create log files that TensorBoard can read. We will specify the directory where the log files will be stored, and we will use the add_scalar method to log the loss and accuracy during training.

writer = SummaryWriter('logs')

Step 4: Add TensorBoard logging to your training loop

In your training loop, you can add logging statements to record the loss and accuracy of the model at each iteration of training. This can be done by using the add_scalar method of the SummaryWriter object.

for epoch in range(num_epochs):
    for i, (inputs, targets) in enumerate(train_loader):
        # Forward pass
        outputs = model(inputs)
        loss = criterion(outputs, targets)

        # Backward pass and optimization
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        # Log the loss
        writer.add_scalar('Loss/train', loss.item(), len(train_loader) * epoch + i)

Step 5: Launch TensorBoard

Once you have logged the necessary information in your training loop, you can launch TensorBoard from the command line using the following command:

tensorboard --logdir=logs

This will start a local server that you can access in your web browser at http://localhost:6006/. Here, you can see visualizations of your training process, such as loss curves and accuracy plots, as well as any custom visualizations you have implemented.

Step 6: Customize your TensorBoard visualizations

In addition to logging scalar values like loss and accuracy, you can also log more complex data structures such as images, text, histograms, and graphs. This can be done using the various add methods provided by the SummaryWriter class. For example, to log an image, you can use the add_image method:

writer.add_image('Input image', inputs[0], epoch)

Conclusion

In this tutorial, we have covered how to use TensorBoard with PyTorch to monitor the training process and visualize the results. By logging scalar values and more complex data structures, you can gain insights into the performance of your model and make informed decisions on how to improve it. TensorBoard is a powerful tool that can help you debug your code, optimize your hyperparameters, and ultimately build better deep learning models. I hope this tutorial has been helpful in getting you started with integrating TensorBoard with PyTorch. Happy training!

0 0 votes
Article Rating
40 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@kaanguler9492
1 month ago

For some reason some of the graphs are totally fine and some show up very limited step.

@RaviTeja-zk4lb
1 month ago

Does this support writing to remote path directly?

@omar-elgammal
1 month ago

Fantastic !! very well done man

@lecteranxa
1 month ago

Thank you! Very helpful video

@boxiongtan69
1 month ago

Thanks! Very helpful

@Kwaiigurlie
1 month ago

Thank you for sharing valuable series on Pytorch. At time 19.44 in this video, you are showing orange bar to change the images in different batches, however with same code typed by me, i am not able to get the orange bar. please look into this and suggest.

@AD-ox4ng
1 month ago

At 6:37 you mention it's not the best way to do hyperparameter search, aren't the learning rates already on a log-10 scale? In any case, do you have other videos exploring hyperparameter search?

@christopherwashington9448
1 month ago

I suggest you move the part of code you are working on in the middle of the screen. It was blocked by the video progress bar all the time.

@Xurshid_Mirzayev
1 month ago

THANKS

@carlosroquesuarezgurruchag8681
1 month ago

thanks for the time.

@massisenergy
1 month ago

Everything works for me except the last step of 'embedding projector'. It says: 'warning: Embedding dir exists, did you set global_step for add_embedding()?'

@HeadshotComing
1 month ago

I was really fortunate to stumble upon this tutorial, it's pure gold!
Decided to check your other videos on the channel real quick, I've eventually spent the last hour going through them!
You're doing an amazing job with your videos, thank you 🙂

@AlbertMunda
1 month ago

This is very impressive work! Thank you very much @Aladdin

@boquangdong
1 month ago

i run your code and i have this bug in writer.add_hparams

device = cudabatch_size = 256

learning_rate = 0.001

2021-08-18 04:33:10.400288: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll

Traceback (most recent call last):

File "015s.py", line 140, in <module>

global_step=batch_idx,

File "C:Usersboqua.condaenvspy37libsite-packagestorchutilstensorboardwriter.py", line 797, in add_embedding

fs = tf.io.gfile.get_filesystem(save_path)

AttributeError: module 'tensorflow._api.v2.io.gfile' has no attribute 'get_filesystem'

@ereh3423
1 month ago

amazing class

@zijingwu7547
1 month ago

thanks for the video!

@salmaelghourbal9622
1 month ago

very nice tuto ! I love your videos !

@wernerlucas12
1 month ago

Your videos are just gold! Thanks for doing this!

@harshkumaragarwal8326
1 month ago

This is really helpful, Thanks 🙂

@amnesie148
1 month ago

I would recommend your videos to my friends who are still struggling with deep learning. Thank you again.