TensorBoardX
TensorBoardX is a Python library that enables logging events for TensorBoard visualizations without requiring TensorFlow as a dependency, making it compatible with frameworks like PyTorch, Chainer, MXNet, and NumPy. It supports logging various data types including scalars, images, audio, histograms, text, graphs, and embeddings to help researchers visualize and track machine learning experiment progress. The current version is 2.6.5, released on April 3, 2026, with active development and maintenance.
Warnings
- breaking The `protobuf` dependency has undergone several version changes. Version 2.6.5 requires `protobuf>=5.29.6`. Earlier versions required `>=3.20`. Ensure your `protobuf` version is compatible with your `tensorboardX` installation to avoid `TypeError` or `ImportError` issues.
- gotcha TensorBoardX only generates the log files; the `tensorboard` package itself must be installed separately to run the visualization server. Without it, you cannot view the generated logs.
- gotcha Logging performance can be an issue, especially when displaying many experiments (3+) or logging high-resolution data (images, large histograms) with many points. While logging is cheap, displaying can be expensive and slow down TensorBoard.
- gotcha When logging PyTorch scalar tensors, you must extract the Python scalar value using `.item()` before passing it to `add_scalar()`, otherwise `tensorboardX` may complain.
- deprecated The `add_audio()` function was significantly optimized (200x speedup) starting from version 2.1, but this requires the `soundfile` package to be installed.
- breaking From `tensorboardX` v2.1, the `add_graph` function's underlying implementation was delegated to `torch.utils.tensorboard`. While the API might remain similar, internal behavior and potential compatibility nuances with specific PyTorch versions could change.
- gotcha Importing `tensorboardX` after `tensorboard` (specifically `from tensorboard import main as tb`) in the same Python session can lead to `TypeError: Couldn't build proto file into descriptor pool!` due to conflicts in protobuf descriptor registration.
Install
-
pip install tensorboardx -
pip install tensorboardx crc32c soundfile tensorboard
Imports
- SummaryWriter
from tensorboardX import SummaryWriter
Quickstart
import torch
import numpy as np
from tensorboardX import SummaryWriter
# Create a writer instance, logs will be saved in 'runs/my_experiment'
writer = SummaryWriter('runs/my_experiment')
# Log scalar values
for i in range(100):
writer.add_scalar('training/loss', 100 - i * 0.5 + np.random.rand(), i)
writer.add_scalar('training/accuracy', 0.5 + i * 0.005 + np.random.rand() * 0.01, i)
# Log a Pytorch model graph (dummy model and input)
try:
import torch.nn as nn
class SimpleModel(nn.Module):
def __init__(self):
super().__init__()
self.linear = nn.Linear(10, 2)
def forward(self, x):
return self.linear(x)
dummy_input = torch.randn(1, 10)
model = SimpleModel()
writer.add_graph(model, dummy_input)
except ImportError:
print("PyTorch not installed, skipping add_graph example.")
# Close the writer to flush all pending events to disk
writer.close()
print("TensorBoardX logs written to 'runs/my_experiment'.")
print("To view these logs, run the following command in your terminal:")
print(" tensorboard --logdir=runs")