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.
- 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
- 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
- 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
- Set up TensorBoard writer
Create aSummaryWriter
object to log the training process:
writer = SummaryWriter()
- 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)
- 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.
Thank you!
steep increase in the learning curve here.
If anyone else has issues with the projector missing, I found restarting the TensorBoard instance to resolve it 🙂
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 🙂
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) #<<<<<<<<<<<<<<<<<<<<<
Thanks for the demo. I used the exact same code but the embedding projector part just won’t work.
tensorboard version
pip install tensorboard==2.4.1
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?
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."
i cant hear a thing. the audio is not of good quality
Great presentation
480p seriously?😡