惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Jina AI
Jina AI
Hugging Face - Blog
Hugging Face - Blog
博客园 - 三生石上(FineUI控件)
博客园 - 【当耐特】
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
宝玉的分享
宝玉的分享
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
酷 壳 – CoolShell
酷 壳 – CoolShell
阮一峰的网络日志
阮一峰的网络日志
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
爱范儿
爱范儿
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
S
SegmentFault 最新的问题
博客园 - Franky
博客园_首页
T
Tailwind CSS Blog
雷峰网
雷峰网
罗磊的独立博客

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
How to Use Tensorboard in Pytorch
2022-04-20 · via jdhao's digital space

This is a brief note on how to use Tensorboard in PyTorch.

Install#

First we need to install tensorboard:

SummaryWriter#

The main interface we use is SummaryWriter. It has many builtin functions, such as add_scalar, add_image, add_graph (for torch models) etc.

For most use cases, we just need to use add_scalar().

import numpy as np
import os
from torch.utils.tensorboard import SummaryWriter


def main():
    log_dir = "exp_log"
    if not os.path.exists(log_dir):
        os.makedirs(log_dir)

    writer = SummaryWriter(log_dir=log_dir)
    for i in range(50):
        writer.add_scalar("my curve", np.random.random(), i)

    # need to close the writer after training
    writer.close()

The first argument is the tag given to this value series.

Group plots#

We can also group the plot like this:

for n_iter in range(100):
    writer.add_scalar('Loss/train', np.random.random(), n_iter)
    writer.add_scalar('Loss/test', np.random.random(), n_iter)
    writer.add_scalar('Accuracy/train', np.random.random(), n_iter)
    writer.add_scalar('Accuracy/test', np.random.random(), n_iter)

In the visualization, we will get two groups, one for Loss and one for Accuracy. Each group has two plots, for train and test respectively.

Compare stats in the same plot#

Often we want to show/compare several curves on the same plot. This can be achieved with add_scalars():

for n_iter in range(100):
    writer.add_scalars('Loss', {'train': np.random.random(),
                                'test': np.random.random()}, n_iter)

    writer.add_scalars('Accuracy', {'train': np.random.random(),
                                    'test': np.random.random()}, n_iter)

In the above code, we have two groups, and each group has one plot showing both train and test stats.

Change the axis scale?#

Sometimes the scale for x and y axis may not be right. The first thing you can do is to disable outlier removal, since it is enabled by default.

Or we can manually select a region using the mouse to only show that region. After tensorboard version 2.5, you can set axis range in the tensorboard web interface interactively, thanks to the work of this pr.

ref:

Visualize the plot#

To actually show the visualizations, we can run the following command:

tensorboard --logdir=exp_log

The argument --logdir should be followed one of valid tensorboard logs you have written during your experiment. Then you can open the browser and check the plots.

References#