How to Add a Projector on TensorBoard with PyTorch
If youre looking to add visualizations to your PyTorch models, using TensorBoard is a great option. One useful feature of TensorBoard is the ability to visualize high-dimensional data in lower dimensions using a projector. In this article, well show you how to add a projector on TensorBoard with PyTorch.
First, we need to install TensorBoard and PyTorch. You can do this using pip:
```bash
pip install tensorboard
pip install torch torchvision
```
Next, well build a simple PyTorch model and train it on some data. For this example, well use the Fashion-MNIST dataset.
```python
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torchvision.datasets import FashionMNIST
from torchvision.transforms import ToTensor
from torch.utils.tensorboard import SummaryWriter
# Define the model
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(784, 128)
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = x.view(-1, 784)
x = nn.ReLU()(self.fc1(x))
x = self.fc2(x)
return x
# Load the data
train_data = FashionMNIST(root="./data", train=True, transform=ToTensor())
train_loader = DataLoader(train_data, batch_size=32, shuffle=True)
# Define the loss function and optimizer
model = Net()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
# Train the model
writer = SummaryWriter()
for epoch in range(10):
for i, (images, labels) in enumerate(train_loader):
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
writer.add_scalars("loss", {"train": loss.item()}, epoch * len(train_loader) + i)
writer.close()
```
Once we have trained our model, we can add a projector to visualize the embeddings. To do this, we first need to extract the embeddings from the model. We can do this by running the model on the dataset and saving the output of the last layer.
```python
embeddings = None
labels = None
model.eval()
with torch.no_grad():
for images, batch_labels in train_loader:
batch_embeddings = model(images).detach().numpy()
embeddings = batch_embeddings if embeddings is None else np.concatenate([embeddings, batch_embeddings])
labels = batch_labels.numpy() if labels is None else np.concatenate([labels, batch_labels.numpy()])
writer.add_embedding(
embeddings,
metadata=labels,
tag="embeddings"
)
```
Finally, we just need to run TensorBoard to see the embeddings. You can do this with the following command:
```bash
tensorboard --logdir=runs
```
This will start TensorBoard on your local machine. If you navigate to the "Projector" tab, you should see your embeddings. You can use the dropdown menus to select different dimensions to visualize, and hover over the points to see their labels.
With these steps you can easily add a projector on TensorBoard with PyTorch and visualize your high-dimensional data in lower dimensions. |