Use TensorBoard for Visualization
Use TensorBoard for visualization in this Applied AI engineering tutorial — learn the core concept, follow a hands-on walkthrough, and troubleshoot common issues. Step 146 in the Python AI track.
Focus: use tensorboard for visualization
You've just trained a model and watched the loss curve bounce around in your terminal like a heart monitor in a bad movie. You squint at the numbers — but the story they're telling is buried under a wall of text. This is the exact pain that TensorBoard was built to cure: raw training metrics are nearly impossible to interpret, compare, or debug. In this lesson, you'll learn how to use TensorBoard for visualization, turning messy logs into clean, interactive dashboards that reveal exactly what your model is learning — and where it's going wrong.
The problem this lesson solves
Training a neural network without visual feedback is like flying a plane using only the fuel gauge. You might know how much gas you have, but you have no idea if you're heading toward the runway or straight into a mountain. Traditional console logs tell you the loss value at a single moment, but they fail to show trends, spikes, or plateaus. You can't easily compare two runs, zoom into a specific epoch, or spot when your learning rate is too high — until it's too late.
TensorBoard solves this by giving you a time machine for your training runs. It records every scalar (like loss and accuracy), every histogram of weights, and even images and graphs, then lets you explore them interactively in your browser. This makes debugging faster, experimentation clearer, and your final results more trustworthy.
Pro tip: If you've ever spent hours guessing why your model stopped improving, you know the value of a good visualization. TensorBoard turns that guesswork into a quick glance.
Core concept / mental model
Think of TensorBoard as a flight recorder for your machine learning experiments. You attach sensors to your training loop (via torch.utils.tensorboard or the tensorboard package), and it logs everything that happens during a run — scalars, histograms, images, even the computation graph. After training (or even during), you launch a local web server that reads those logs and displays them as interactive plots.
The key mental model: you write to a log directory, and TensorBoard reads from that directory. The writer is your Python code; the dashboard is your browser. You control what to log and how often, so you can keep the overhead low or dive deep into every detail.
Definitions to know
- Event file: A binary file (or set of files) that TensorBoard generates from your logged data. It lives in your
log_dir. - Scalar: A single number at each step — loss, accuracy, learning rate.
- Histogram: A distribution of values — e.g., the distribution of weights in a layer.
- Graph: The computational graph of your model — useful for understanding layer connections and spotting bottlenecks.
How it works step by step
Here's the logical flow from logging to dashboard:
- Create a
SummaryWriterpointing to a log directory. - Add data during training — scalars, histograms, images — using the writer's methods.
- Close the writer after training (or use a context manager) to flush all data.
- Launch TensorBoard from the terminal, pointing it to the log directory.
- Open your browser at the local URL (default
http://localhost:6006) and explore.
The writer can be used in multiple training scripts. If you point multiple runs to the same parent directory (with different subdirectories), TensorBoard automatically overlays them in the Scalar tab, making comparisons effortless.
Hands-on walkthrough
Let's put this into practice with a minimal PyTorch example. First, install the necessary package if you haven't already:
pip install torch torchvision tensorboard
Then create a simple training loop that logs loss and accuracy:
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.tensorboard import SummaryWriter
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
# Setup
writer = SummaryWriter(log_dir="runs/experiment_1")
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))])
train_dataset = datasets.MNIST("./data", train=True, download=True, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
# Simple model
model = nn.Sequential(
nn.Flatten(),
nn.Linear(28 * 28, 128),
nn.ReLU(),
nn.Linear(128, 10)
)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
def compute_accuracy(output, target):
_, pred = torch.max(output, 1)
return (pred == target).float().mean()
for epoch in range(3):
running_loss = 0.0
running_acc = 0.0
for i, (images, labels) in enumerate(train_loader):
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, labels)
acc = compute_accuracy(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
running_acc += acc.item()
if i % 100 == 99: # Log every 100 batches
avg_loss = running_loss / 100
avg_acc = running_acc / 100
step = epoch * len(train_loader) + i
writer.add_scalar("Loss/train", avg_loss, step)
writer.add_scalar("Accuracy/train", avg_acc, step)
running_loss = 0.0
running_acc = 0.0
print(f"Epoch {epoch+1} done")
writer.close()
print("Training complete. Run `tensorboard --logdir runs` to view.")
After running this script, you'll see something like:
Epoch 1 done
Epoch 2 done
Epoch 3 done
Training complete. Run `tensorboard --logdir runs` to view.
Now launch TensorBoard (in a new terminal, from the same directory):
tensorboard --logdir runs
Then open your browser to http://localhost:6006. You'll see the Scalars tab with two curves: Loss/train and Accuracy/train.
Adding histograms and images
To see the weight distributions and sample images, use add_histogram and add_images:
# Inside the loop, after a few batches:
if i % 500 == 0:
for name, param in model.named_parameters():
writer.add_histogram(f"{name}/weights", param.data.cpu().numpy(), step)
if param.grad is not None:
writer.add_histogram(f"{name}/grads", param.grad.cpu().numpy(), step)
# Log some sample images
img_grid = torchvision.utils.make_grid(images[:8])
writer.add_image("sample_images", img_grid, step)
Now you'll have Histograms and Images tabs too.
Compare multiple runs
To compare runs, use separate subdirectories:
writer = SummaryWriter(log_dir="runs/lr_0.001")
# ... train with lr=0.001
writer.close()
writer2 = SummaryWriter(log_dir="runs/lr_0.01")
# ... train with lr=0.01
writer2.close()
Now launch TensorBoard with --logdir runs and both curves appear overlaid, making it trivial to see which learning rate converges better.
Compare options / when to choose what
TensorBoard is not the only game in town. Here's how it stacks up against other visualization tools:
| Feature/Tool | TensorBoard | Weights & Biases (W&B) | Matplotlib/Plotly |
|---|---|---|---|
| Real-time streaming | Yes (local) | Yes (cloud) | No |
| Scalar curves | Yes | Yes | Yes (manual) |
| Histograms of weights | Built-in | Built-in | Manual |
| Image/embedding visualization | Yes | Yes | Limited |
| Model graph | Yes | No | No |
| Setup effort | Low (just pip install) | Medium (account + API key) | Low |
| Collaboration | Local only | Cloud, team-friendly | Local |
| Cost | Free, open-source | Free tier, paid plans | Free |
When to choose what:
- TensorBoard is your default for local, self-contained experiments. It's built into PyTorch and TensorFlow, free, and offline.
- W&B shines when you need team collaboration, experiment tracking with automatic hyperparameter logging, or you're working in a remote environment.
- Matplotlib/Plotly are great for ad-hoc analysis or when you need custom, publication-quality figures after training — but they require manual logging and are not interactive at scale.
Pro tip: Start with TensorBoard. It covers 90% of your visualization needs and keeps your workflow simple.
Troubleshooting & edge cases
Nothing shows up in the dashboard
- Check your log directory: Ensure you're launching TensorBoard from the correct path, or specify an absolute path with
--logdir. If you usedruns/experiment_1, launch withtensorboard --logdir runs. - Wait a moment: TensorBoard may take a few seconds to read the event files. Refresh the page.
- Web server conflict: If port 6006 is in use, specify another:
tensorboard --logdir runs --port 6007.
Curves look flat or missing
- You didn't flush the writer: Always call
writer.close()or use a context manager to ensure data is written. Without it, you might lose the last steps. - You're logging too infrequently: If you only log once per epoch, the early behavior is invisible. Log every N batches for smoother curves.
TensorBoard shows “No dashboards are active”
- Wrong logdir: The path doesn't contain valid event files. Verify the directory has
events.out.tfevents.*files. - Permission issues: On Linux, make sure the directory is readable.
Memory or speed overhead
- Logging too much: If you log histograms every step, your disk fills quickly and training slows. Log histograms every few hundred steps.
- Large images: Use
make_gridto combine images into one, reducing storage.
Data doesn't match your training loop
- Step mismatch: Ensure you're logging all data at the same step value. If you mix steps, curves will look broken.
What you learned & what's next
You now know how to use TensorBoard for visualization, turning raw training logs into interactive, insightful dashboards. You can log scalars, histograms, and images, compare multiple runs, and troubleshoot common issues. This skill directly supports your Applied AI engineering journey: whenever you train a model, you can now see what's happening, debug faster, and make data-driven decisions about hyperparameters.
Next step: With visualization mastered, you're ready to tackle hyperparameter tuning and experiment tracking — where you'll systematically explore learning rates, batch sizes, and architectures, and use tools like TensorBoard or W&B to organize and compare dozens of experiments efficiently.
Keep experimenting — your models will thank you.
Practice recap
Run the MNIST example with two different learning rates (e.g., 0.001 and 0.01) and launch TensorBoard to compare the loss curves. Then add add_histogram calls for the first layer's weights and observe how they change over 3 epochs. This will cement your understanding of logging and comparison.
Common mistakes
- Forgetting to call
writer.close()or not using a context manager — data may never flush to disk, and your dashboard appears empty. - Logging all data at inconsistent step values — curves become misaligned and look chaotic. Always use the global training step.
- Logging histograms and images too frequently — this fills your disk and slows training. Log them every few hundred steps.
- Launching TensorBoard from the wrong directory — if you used
runs/experiment_1, you must point toruns, not the parent folder.
Variations
- Weights & Biases (W&B): a cloud-based alternative with automatic logging and team collaboration features.
- Neptune AI or MLflow: other experiment tracking tools that include visualization and model registry capabilities.
- Matplotlib/Plotly: if you need custom, static plots for publications, you can replicate some TensorBoard charts manually.
Real-world use cases
- Debug a ResNet training run: spot vanishing gradients via histograms and tune the learning rate using overlaid scalar curves.
- Comparing two optimizer configurations (Adam vs SGD) for a language model — use TensorBoard's runs overlay to decide which converges faster.
- Monitoring a production model's fine-tuning process on a custom dataset — log validation loss every epoch to catch overfitting early.
Key takeaways
- TensorBoard transforms raw training logs into interactive scalars, histograms, images, and graphs.
- Create a
SummaryWriterfor each experiment and log data at consistent global steps. - Launch the dashboard with
tensorboard --logdir <dir>and open the local URL to explore. - Compare runs by using separate subdirectories under the same
logdir. - TensorBoard is free, offline, and ideal for local projects — W&B is better for collaboration.
- Always close the writer to flush data; log histograms sparingly to avoid disk bloat.
Keep learning
Related tutorials, quizzes, and articles for this topic.
Discussion
Questions, corrections, and tips help everyone reading this page.
0 comments
Add a comment
No comments yet — start the thread.