Mastering PyTorch Visualization: TensorBoard and Visdom Guide

This tutorial explains how to install, launch, and use TensorBoard and Visdom with PyTorch, providing step‑by‑step commands, code examples for logging training metrics, and visualizing images and plots to monitor deep‑learning experiments.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Mastering PyTorch Visualization: TensorBoard and Visdom Guide

PyTorch Visualization Tools: TensorBoard and Visdom

1. TensorBoard

TensorBoard is primarily a visualization tool for TensorFlow, but it can also be used independently with PyTorch to display computation graphs, scalar metrics, and custom data.

1. Install

pip install tensorboard

2. Launch

tensorboard --logdir="log directory"

When launching TensorBoard you can specify logdir, port (default 6006), host (default localhost) and other parameters.

usage: tensorboard [-h] [--helpfull] [--logdir PATH] [--logdir_spec PATH_SPEC]
                   [--host ADDR] [--bind_all] [--port PORT]
                   [--purge_orphaned_data BOOL] [--db URI] [--db_import]
                   [--inspect] [--version_tb] [--tag TAG] [--event_file PATH]
                   [--path_prefix PATH] [--window_title TEXT]
                   [--max_reload_threads COUNT] [--reload_interval SECONDS]
                   [--reload_task TYPE] [--reload_multifile BOOL]
                   [--reload_multifile_inactive_secs SECONDS]
                   [--generic_data TYPE]
                   [--samples_per_plugin SAMPLES_PER_PLUGIN]
                   [--debugger_data_server_grpc_port PORT]
                   [--debugger_port PORT] [--master_tpu_unsecure_channel ADDR]

3. TensorBoard demo (PyTorch)

During model training, use SummaryWriter to log loss, accuracy, etc.

# Import SummaryWriter
from torch.utils.tensorboard import SummaryWriter
# Create writer instance
summaryWriter = SummaryWriter(log_dir="/path/to/logs")
# Log training metrics
summaryWriter.add_scalars("loss", {"train_loss_avg": train_loss_avg, "test_loss_avg": test_loss_avg}, epoch)
summaryWriter.add_scalar("score", score, epoch)

Launch TensorBoard to visualize the training process: tensorboard --logdir=/path/to/logs Successful launch screenshot:

Resulting visualizations:

2. Visdom

Visdom, developed by Facebook for PyTorch, supports remote data visualization and works with Torch and NumPy.

1. Install

pip install visdom

2. Launch

python -m visdom.server

– start the server as a module nohup python -m visdom.server & – run in background on Linux/macOS

You can specify port (default 8097) and hostname (default localhost) when launching.

usage: server.py [-h] [-port port] [--hostname hostname] [-base_url base_url]
                [-env_path env_path] [-logging_level logger_level]
                [-readonly] [-enable_login] [-force_new_cookie]
                [-use_frontend_client_polling]

3. Visdom demo

Start Visdom server: python -m visdom.server -port 8097 Successful launch screenshot:

Training visualization code:

# Import visdom package
import visdom
# Create Visdom object, connect to server, specify environment
viz = visdom.Visdom(server='http://localhost', port=8097, env='liyunfei')
# Initialize lines
viz.line([0.], [0], win='train_loss', opts=dict(title='train_loss'))
viz.line([0.], [0], win='accuracy', opts=dict(title='accuracy'))
# During training, update loss and accuracy
viz.line([train_loss_avg], [epoch], win='train_loss', update='append')
viz.line([accuracy], [epoch], win='accuracy', update='append')

Resulting visualizations:

4. Visualizing images

Example of displaying a single random image and a batch of random images:

import visdom
import numpy as np
viz = visdom.Visdom(server='http://localhost', port=8097, env='liyunfei')
# Single image
viz.image(np.random.rand(3, 512, 256), opts=dict(title='Random!', caption='How random.'))
# Multiple images
viz.images(np.random.randn(20, 3, 64, 64), nrow=5,
           opts=dict(title='Random images', caption='How random.'))

Result:

5. Common Visdom APIs

vis.scatter : 2D or 3D scatter plot
vis.line    : line plot
vis.stem    : stem plot
vis.heatmap : heat map
vis.bar     : bar chart
vis.histogram : histogram
vis.boxplot : box plot
vis.surf    : surface plot
vis.contour : contour plot
vis.quiver  : 2D vector field
vis.image   : image display
vis.text    : text display
vis.mesh    : mesh plot
vis.save    : serialize state
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonDeep LearningPyTorchTensorBoardVisdom
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.