TensorBoardX

2.6.5 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a `SummaryWriter`, log scalar values over iterations, and log a PyTorch model graph. After running the script, a 'runs' directory will be created containing the event files. You then use the `tensorboard` command-line tool to launch the visualization server.

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")

view raw JSON →