tensorboard的pytorch使用指南

使用pytorch之后自然想到了使用tensorboard可视化一下loss和accuracy,好在现在的最新版本早就支持了tensorboard,不然就麻烦了。

参考链接

https://pytorch.org/tutorials/recipes/recipes/tensorboard_with_pytorch.html

https://pytorch.org/tutorials/intermediate/tensorboard_tutorial.html

https://cuijiahua.com/blog/2020/05/dl-17.html

Install tensorboard

1
pip install tensorboard

Using TensorBoard in PyTorch

1.TensorBoard setup

We need to create SummaryWriter instance first

1
2
3
import torch
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter()

Writer will output to ./runs/ directory by default. The file name will be like this Mar25_20-11-21_ubuntu(date, time, and username)

You also can decide the directory like this.

1
2
3
4
# default `log_dir` is "runs" - we'll be more specific here
writer = SummaryWriter('runs/fashion_mnist_experiment_1')
# you can add summaries. The final name will be date_time_username_comment
writer = SummaryWriter(comment='_resnet')

2.Writing to TensorBoard

To log a scalar value, use add_scalar(tag, scalar_value, global_step=None, walltime=None). For example, lets create a simple linear regression training, and log loss value using add_scalar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
x = torch.arange(-5, 5, 0.1).view(-1, 1)
y = -5 * x + 0.1 * torch.randn(x.size())

model = torch.nn.Linear(1, 1)
criterion = torch.nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr = 0.1)

def train_model(iter):
for epoch in range(iter):
y1 = model(x)
loss = criterion(y1, y)
writer.add_scalar("Loss/train", loss, epoch)
optimizer.zero_grad()
loss.backward()
optimizer.step()

train_model(10)
writer.flush()

Call flush() method to make sure that all pending events have been written to disk.

3. Close the summary writer

If you do not need the summary writer anymore, call close() method.

1
writer.close()

Run TensorBoard

Now, start TensorBoard, specifying the root log directory you used above. Argument logdir points to directory where TensorBoard will look to find event files that it can display. TensorBoard will recursively walk the directory structure rooted at logdir, looking for .tfevents. files.

In a netshell, your shell need in the directory

1
tensorboard --logdir=runs

tensorboard的pytorch使用指南
http://example.com/2021/03/25/tensorboard的pytorch使用指南/
Author
Neko kiku
Posted on
March 25, 2021
Licensed under