我们知道TensorBoard是Tensorflow中的一个强大的可视化工具,它可以让我们非常方便地记录训练loss波动情况。如果我们是其它深度学习框架用户(如Pytorch),而想使用TensorBoard工具,可以安装TensorBoard的封装版本TensorBoardX:
pip install tensorboardx
具体使用方法如下:
from tensorboardX import SummaryWriter
writer = SummaryWriter('tensorboard/')
x = range(100)
for i in x:
writer.add_scalar('y=2x', i * 2, i,)
writer.add_scalar('y=x^2', i ** 2, i,)
writer.close()
然后再再当前路径下会生成名称为tensorboard
文件夹及对应的记录文件,我们再使用命令 tensorboard --logdir tensorboard
,可以看到对应输出:
(base) ➜ Learn-Pytorch tensorboard --logdir tensorboard
NOTE: Using experimental fast data loading logic. To disable, pass
"--load_fast=false" and report issues on GitHub. More details:
https://github.com/tensorflow/tensorboard/issues/4784
I1126 18:06:12.417282 6209499136 plugin.py:429] Monitor runs begin
Serving TensorBoard on localhost; to expose to the network, use a proxy or pass --bind_all
TensorBoard 2.9.0 at http://localhost:6006/ (Press CTRL+C to quit)
此时只需要按照提醒打开http://localhost:6006/
路径即可查看数据可视化情况,如下所示:
如果我们想将图片保存为矢量图或者导出csv/json文件,可以点击右边的Show data download links
选项,即可出现对应的导出选项:
最后,需要提到的是,因为Tensorboard太常用了,所以在目前最新的Pytorch版本中已经直接集成进来了。所以,现在使用Tensorboard只需要直接导入torch.utils.tensorboard
即可:
from torch.utils.tensorboard import SummaryWriter
x = range(100)
for i in x:
writer.add_scalar('y=2x', i * 2, i,)
writer.add_scalar('y=x^2', i ** 2, i,)
writer.close()
标签:Tensorboard,TensorBoard,训练,writer,scalar,Pytorch,add,tensorboard
From: https://www.cnblogs.com/orion-orion/p/16928235.html