torch.utils.tensorboard.writer.SummaryWriter(log_dir=None, comment=‘’, purge_step=None, max_queue=10, flush_secs=120, filename_suffix=‘’)
代码案例:
from torch.utils.tensorboard import SummaryWriter
# create a summary writer with automatically generated folder name.
writer = SummaryWriter() # 默认方法,不传参数
# folder location: runs/May04_22-14-54_s-MacBook-Pro.local/
# create a summary writer using the specified folder name.
writer = SummaryWriter("my_experiment") # 相当于把前面说的runs替换为指定的my_experiment
# folder location: my_experiment
# create a summary writer with comment appended.
writer = SummaryWriter(comment="LR_0.1_BATCH_16")
# folder location: runs/May04_22-14-54_s-MacBook-Pro.localLR_0.1_BATCH_16/
add_scalar(tag, scalar_value, global_step=None, walltime=None, new_style=False, double_precision=False)
代码案例:
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter()
x = range(100)
for i in x:
writer.add_scalar('y=2x', i * 2, i) # 此处的i就时global_step
writer.close() # 关闭
add_scalars(main_tag, tag_scalar_dict, global_step=None, walltime=None)
代码案例:
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter()
r = 5
for i in range(100):
writer.add_scalars('run_14h', {'xsinx':i*np.sin(i/r),
'xcosx':i*np.cos(i/r),
'tanx': np.tan(i/r)}, i)
writer.close() # 关闭
# This call adds three values to the same scalar plot with the tag
# 'run_14h' in TensorBoard's scalar section.
add_image(tag, img_tensor, global_step=None, walltime=None, dataformats=‘CHW’)
代码案例:
from torch.utils.tensorboard import SummaryWriter
import numpy as np
img = np.zeros((3, 100, 100)) # 默认格式,CHW
img[0] = np.arange(0, 10000).reshape(100, 100) / 10000
img[1] = 1 - np.arange(0, 10000).reshape(100, 100) / 10000
img_HWC = np.zeros((100, 100, 3)) # 非默认格式,HWC
img_HWC[:, :, 0] = np.arange(0, 10000).reshape(100, 100) / 10000
img_HWC[:, :, 1] = 1 - np.arange(0, 10000).reshape(100, 100) / 10000
writer = SummaryWriter()
writer.add_image('my_image', img, 0) # 默认图片直接添加
# If you have non-default dimension setting, set the dataformats argument.
writer.add_image('my_image_HWC', img_HWC, 0, dataformats='HWC') # 非默认格式要设置dataformats为对应格式
writer.close()
add_images(tag, img_tensor, global_step=None, walltime=None, dataformats=‘NCHW’)
代码案例:
from torch.utils.tensorboard import SummaryWriter
import numpy as np
img_batch = np.zeros((16, 3, 100, 100)) # 生成了默认格式(N,3, H, W)的图片数据
for i in range(16): # 对图片数据中的像素值调整
img_batch[i, 0] = np.arange(0, 10000).reshape(100, 100) / 10000 / 16 * i
img_batch[i, 1] = (1 - np.arange(0, 10000).reshape(100, 100) / 10000) / 16 * i
writer = SummaryWriter()
writer.add_images('my_image_batch', img_batch, 0) # 添加批量图片
writer.close()
add_text(tag, text_string, global_step=None, walltime=None)
代码案例:
writer.add_text('lstm', 'This is an lstm', 0)
writer.add_text('rnn', 'This is an rnn', 10)
add_graph(model, input_to_model=None, verbose=False, use_strict_trace=True)
添加精确召回曲线。绘制精确召回曲线可让您了解模型在不同阈值设置下的性能。使用此功能,您可以为每个目标提供真实标记 (T/F) 和预测置信度(通常是模型的输出)。 TensorBoard UI 将让您以交互方式选择阈值。
add_pr_curve(tag, labels, predictions, global_step=None, num_thresholds=127, weights=None, walltime=None)
代码案例:
from torch.utils.tensorboard import SummaryWriter
import numpy as np
labels = np.random.randint(2, size=100) # binary label
predictions = np.random.rand(100)
writer = SummaryWriter()
writer.add_pr_curve('pr_curve', labels, predictions, 0)
writer.close()
add_hparams(hparam_dict, metric_dict, hparam_domain_discrete=None, run_name=None)
代码案例:
from torch.utils.tensorboard import SummaryWriter
with SummaryWriter() as w:
for i in range(5): # 每次超参数随着i变化
w.add_hparams({'lr': 0.1*i, 'bsize': i},
{'hparam/accuracy': 10*i, 'hparam/loss': 10*i})
对于add_hparams,最好是在模型完成训练时,使用模型对训练集、验证集、测试集计算完accuracy、loss等metric后再将本次训练的有些超参数一起保存,并且此处的metric中的名称要与前面add_scalar中的不同
在算法训练完毕后,会在项目目录下生成在初始化中指定的log_dir的路径,在终端输入tensorboard --logdir=路径名,用浏览器访问http://127.0.0.1:6006即可进入可视化界面
tensorboard会在你指定的这个log-dir里面做路径搜索,寻找所有的events文件以及子文件夹中的events文件。路径相同的events文件可视化的图形线条颜色一样,如果文件路径不同但是数据名称相同,则会被用不同线条颜色画到同一个图里面。
pytorch官方文档连接https://pytorch.org/docs/stable/tensorboard.html,其中还有很多其他的方法