1.概况
在 PyTorch 中,SummaryWriter
用于将训练过程中的各种信息写入到 TensorBoard 日志文件中,然后可以通过 TensorBoard 可视化这些信息。SummaryWriter
通常用于记录以下内容:
- 标量(Scalar):例如损失函数值、准确率、学习率等随着训练迭代的变化趋势,可以用于绘制曲线图。
- 图(Graph):可视化神经网络的计算图,用于观察网络结构和参数情况。
- 图片(Image):例如输入样本图片、模型输出图片等,可以用于查看样本和模型输出的变化。
- 音频(Audio):例如模型生成的音频样本等。
- 文本(Text):记录训练过程中的注释和文本信息。
2.代码
tb = SummaryWriter(logdir)
for epoch in range(args.epoch):
adaptive_Ks, running_mean_loss, running_residual_loss, running_softmax_loss, running_loss = train(
train_loader, model, criterion1, criterion2, optimizer, epoch, args.result_directory, args.loss)
tb.add_scalar('info/train_mean_loss', running_mean_loss, epoch)
tb.add_scalar('info/train_residual_loss', running_residual_loss, epoch)
tb.add_scalar('info/train_softmax_loss', running_softmax_loss, epoch)
tb.add_scalar('info/train_total_loss', running_loss, epoch)
K_values.append(adaptive_Ks)
mean_loss, residual_loss, softmax_loss, loss_val, eps = evaluate(val_loader, model, criterion1, criterion2,
args.loss)
tb.add_scalar('info/val_mean_loss', mean_loss, epoch)
tb.add_scalar('info/val_mean_loss', residual_loss, epoch)
tb.add_scalar('info/val_softmax_loss', softmax_loss, epoch)
tb.add_scalar('info/val_total_loss', loss_val, epoch)
tb.add_scalar('info/val_eps', eps, epoch)
eps_test, cs_list = test(test_loader, model)
tb.add_scalar('info/test_eps', eps_test, epoch)
上面代码来自:https://github.com/jacobzhaoziyuan/AMR-Loss
在命令行窗口中,输入tensorboard --logdir=日志路径
,这里的日志路径
就是代码tb = SummaryWriter(logdir)
中定义的logdir
3.再看个例子
看的B占小土堆的:https://www.bilibili.com/video/BV1hE411t7RN/
import torch
from torch import nn
import torchvision
from torch.nn import Conv2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
class Min(nn.Module):
def __init__(self):
super(Min, self).__init__()
self.conv1 = Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=0)
def forward(self, x):
x = self.conv1(x)
return x
dataset = torchvision.datasets.CIFAR10(r"E:\TM\dataset\CIFAR10", train=False,
transform=torchvision.transforms.ToTensor(), download=True)
dataloader = DataLoader(dataset, batch_size=64)
min = Min()
# print(min)
writer = SummaryWriter("../logs1")
step = 0
for data in dataloader:
imgs, target = data
output = min(imgs)
print(imgs.shape)
print(output.shape)
# 输入大小torch.Size([64, 3, 32, 32])
writer.add_images("input", imgs, step)
# 输出大小torch.Size([64, 6, 30, 30]) -> [xxx, 3, 30, 30]
output = torch.reshape(output, (-1, 3, 30, 30))
writer.add_images("output", output, step)
step = step + 1
# tensorboard --logdir=logs1
在命令行窗口输入命令:tensorboard --logdir=logs1
,
跳转网址: http://localhost:6006/就能看到输入图片和输出图片是什么样。