Support for TensorBoard in PyTorch

Posted by


PyTorch TensorBoard support is a useful feature that allows you to visualize and monitor the training process of your deep learning models in real-time. In this tutorial, I will guide you through the steps to set up and use TensorBoard with PyTorch.

  1. Install TensorBoard and PyTorch
    First, you need to install TensorBoard and PyTorch in your Python environment. You can install them using pip by running the following commands:
pip install torch torchvision
pip install tensorboard
  1. Import the necessary libraries
    Next, import the necessary libraries in your Python script:
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.tensorboard import SummaryWriter
  1. Define your deep learning model
    For this tutorial, let’s create a simple neural network with one hidden layer:
class NeuralNetwork(nn.Module):
    def __init__(self):
        super(NeuralNetwork, self).__init__()
        self.fc1 = nn.Linear(784, 128)
        self.relu = nn.ReLU()
        self.fc2 = nn.Linear(128, 10)

    def forward(self, x):
        x = self.fc1(x)
        x = self.relu(x)
        x = self.fc2(x)
        return x
  1. Set up TensorBoard writer
    Create a SummaryWriter object to log the training process:
writer = SummaryWriter()
  1. Train your model
    Now, let’s train the model using your dataset. Here is an example training loop:
model = NeuralNetwork()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

for epoch in range(10):
    for i, data in enumerate(train_loader):
        inputs, labels = data
        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        # Log metrics to TensorBoard
        writer.add_scalar('Loss', loss.item(), epoch * len(train_loader) + i)
  1. Visualize the training process
    To visualize the training process, run the TensorBoard server from the command line:
tensorboard --logdir=runs

Then, open your browser and navigate to http://localhost:6006 to view the TensorBoard dashboard. Here, you can monitor the loss curve, model graph, and other metrics logged during training.

In conclusion, PyTorch TensorBoard support is a powerful tool for monitoring and visualizing the training process of your deep learning models. By following this tutorial, you can easily set up and use TensorBoard with PyTorch to improve your model’s performance.

0 0 votes
Article Rating

Leave a Reply

12 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@rafaelcardosotrentin7644
24 days ago

Thank you!

@PurtiRS
24 days ago

steep increase in the learning curve here.

@MarvinZtrubas
24 days ago

If anyone else has issues with the projector missing, I found restarting the TensorBoard instance to resolve it 🙂

@gebbione
24 days ago

Thank you for the intro. Despite running all blocks I am still unable to get a 3d visualisation working. I have run it a few times and I have no outputs in projector. Any suggestions please follow up here 🙂

@j34n3l4k34
24 days ago

I had to change to this to get it to run:

# Extract a batch of 4 images

dataiter = iter(training_loader)

images, labels = next(dataiter) #<<<<<<<<<<<<<<<<<<<<<

@Mine2148
24 days ago

Thanks for the demo. I used the exact same code but the embedding projector part just won’t work.

@myelinsheathxd
24 days ago

tensorboard version

pip install tensorboard==2.4.1

@sportsdude2828
24 days ago

How is it that when training your neural network at 3:25, you don't have to call net.forward(inputs) but can instead just use net(inputs)? Also, my laptop takes over a minute to complete the training. Is this normal?

@giladfelsen398
24 days ago

Thanks for the Tutorial.
How would you run this from a colab notebook?
I've run tensorboard from within a colab notebook before without any problems (using keras and tensorflow callbacks),
but now when I try using tensorboard and using %tensorboard –logdir=./my_logs –port=6006
It says "No dashboards are active for the current data set."

@arunabhsingh5448
24 days ago

i cant hear a thing. the audio is not of good quality

@lalalafamille
24 days ago

Great presentation

@aseemsrivastava3230
24 days ago

480p seriously?😡

12
0
Would love your thoughts, please comment.x
()
x