建好一个神经网络,其实我们也不知道神经网络里头具体细节到底做了什么,要人工调试十分困难(就好比你无法想象出递归的所有步骤一样)。有了TensorBoard
,可以将TensorFlow
程序的执行步骤都显示出来,非常直观。并且,我们可以对训练的参数(比如loss值)进行统计,用图的方式来查看变化的趋势。
TensorBoard
可以用于记录训练数据、评估数据、网络结构、图像等,并且可以在web上展示,对于观察神经网络的过程非常有帮助。PyTorch
也推出了自己的可视化工具,一个是tensorboardX
包,一个是torch.utils.tensorboard
,二者的使用相差不大,这里介绍后者
要启动 TensorBoard,打开终端或命令提示符并运行:
tensorboard --logdir=<directory_name>
将 directory_name 标记替换为保存数据的目录。 默认是“logs”。
add_scalar(tag, scalar_value, global_step=None, walltime=None)
参数:
例子:
from torch.utils.tensorboard import SummaryWriter
import numpy as np
writer = SummaryWriter("scalar")
for x in range(1, 101) :
writer.add_scalar('y = 2x',2 * x, x ) ### 这里反了,正常情况应该是writer.add_scalar('y = 2x', 2 * x, x)
writer.close()
#tensorboard --logdir=scalar
效果:
add_scalars( main_tag , tag_scalar_dict , global_step = None , walltime = None)
参数:
例子:
from torch.utils.tensorboard import SummaryWriter
import numpy as np
writer = SummaryWriter("scalars")
r = 5
for x in range(1, 101) :
writer.add_scalars('run_14h', {'xsinx' : x * np.sin(x / r),
'xcosx' : x * np.cos(x / r),
'xtanx' : x * np.tan(x / r)}, x)
writer.close()
#tensorboard --logdir=scalars
效果:
add_histogram( tag , values , global_step = None , bins = ‘tensorflow’ , walltime = None , max_bins = None )
参数:
tag ( string ) – 数据标识符
值(torch.Tensor、numpy.array或string/blobname)– 构建直方图的值
global_step ( int ) – 要记录的全局步长值
bins ( string ) – {‘tensorflow’,‘auto’, ‘fd’, …} 之一。这决定了柱的制作方式。
walltime ( float ) – 记录训练的时间,默认 walltime (time.time()) 秒
例子:
from torch.utils.tensorboard import SummaryWriter
import numpy as np
writer = SummaryWriter("histogram")
for step in range(10) :
x = np.random.randn(1000)
writer.add_histogram('distribution of gaussion', x, step)
writer.close()
#tensorboard --logdir=histogram
效果:
add_image(tag, img_tensor, global_step=None, walltime=None, dataformats = ‘CHW’)
参数:
tag ( string ) – 数据标识符
img_tensor ( torch.Tensor , numpy.array , or string/blobname ) – 图像数据
global_step ( int ) – 要记录的全局步长值
walltime ( float ) – 记录训练的时间,默认 walltime (time.time()) 秒
例子:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
import torch
from torchvision import transforms
img_path = "1Q12FR626-9-1200.jpg"
img = Image.open(img_path)
writer = SummaryWriter("logs")
tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)
writer.add_image("Tensor_img",tensor_img,0)
writer.close()
效果:
add_figure( tag , figure , global_step = None , close = True , walltime = None )
参数:
tag ( string ) – 数据标识符
image( matplotlib.pyplot.figure ) – 图或图列表
global_step ( int ) – 要记录的全局步长值
close ( bool ) – 自动关闭图形的标志
walltime ( float ) – 记录训练的时间,默认 walltime (time.time()) 秒
例子:
from torch.utils.tensorboard import SummaryWriter
import numpy as np
import matplotlib.pyplot as plt
writer = SummaryWriter("figure")
x = np.linspace(0, 10, 1000)
y = np.sin(x)
figure1 = plt.figure()
plt.plot(x, y, 'r-')
writer.add_figure('my_figure', figure1, 0)
writer.close()
# tensorboard --logdir=figure --port=8888
效果:
add_graph(model, input_to_model=None, verbose=False, use_strict_trace = True)
参数:
model( torch.nn.Module ) – 要绘制的模型。
input_to_model ( torch.Tensor or list of torch.Tensor ) – 要输入的变量或变量元组
verbose(bool)– 是否在控制台中打印图形结构。
use_strict_trace ( bool ) – 是否将关键字参数严格传递给 torch.jit.trace。当您希望跟踪器记录您的可 - 变容器类型(列表、字典)时传递 False.
例子:
from torch.utils.tensorboard import SummaryWriter
import numpy as np
import torch
import torch.nn as nn
writer = SummaryWriter("graph")
class MLP(nn.Module) :
def __init__(self):
super(MLP, self).__init__()
self.Net = nn.Sequential(
nn.Linear(784, 512),
nn.ReLU(),
nn.Linear(512, 128),
nn.ReLU(),
nn.Linear(128, 10)
)
def forward(self, input):
input = input.view(-1, 28 * 28)
return self.Net(input)
model = MLP()
input = torch.FloatTensor(np.random.rand(32, 1, 28, 28))
writer.add_graph(model, input)
#tensorboard --logdir=graph --port=8888
效果:
常用的就以上几种