标量数据可视化
import numpy as np
from torch.utils.tensorboard import SummaryWriterwriter = SummaryWriter(log_dir='./runs') #log_dir用于指定保存目录
flag = 1if flag:for x in range(100):# 把x*2的数据加入标签y=2x的曲线writer.add_scalar(tag='y=2x', scalar_value=x * 2, global_step=x)# 把2**x的数据加入标签y=pow(2,x)的曲线writer.add_scalar(tag='y=pow(2,x)', scalar_value=2 ** x,global_step=x)# 把x*sin(x)和x*cos(x)的数据加入data/scalar_group的标签组中,即# 两个曲线绘制在一张图中writer.add_scalars('data/scalar_group',{'xsinx': x * np.sin(x), 'xcosx': x * np.cos(x)}, x)
writer.close() # 将event log写完之后,记得close()
tensorboard --logdir=./runs/