PyTorch:可视化TensorBoard

Posted -柚子皮-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PyTorch:可视化TensorBoard相关的知识,希望对你有一定的参考价值。

PyTorch  1.2.0 版本开始。

安装及更新

pip3 install --upgrade torch torchvision

pip3 install --upgrade tensorboard

Bugs:

1 AttributeError: module 'tensorflow._api.v1.io' has no attribute 'gfile'

出现这个问题的根本原因在于pytorch调了Tensorflow,最后由Tensorflow报出的错误,tensorflow的新版本与旧版本的不兼容。

可以更新tensorflow;或者[AttributeError: module 'tensorflow._api.v1.io' has no attribute 'gfile']

2 tensorboard安装不了与旧版本的不兼容

可指定版本安装pip3 install tensorboard==1.15,或者更新tensorflow到最新。

 

torch.utils.tensorboard.writer.SummaryWriter(log_dir=None, comment='', purge_step=None, max_queue=10, flush_secs=120, filename_suffix='')

class SummaryWriter(object):

    def set_as_default(self):
        """设定默认的 summary writer 
        亦即让此后出现的所有 summary 写入操作,都默认使用本个 summary writer 实例进行记录"""

    def as_default(self):
        """返回一个上下文管理器(配合with使用),记录context中出现的 summary 写入操作"""

    def flush(self):
        """强制将缓存中的数据写入到日志文件中"""

    def close(self, tensor):
        """强制将缓存中的数据写入到日志文件中,然后关闭 summary writer"""

    def init(self):
        """初始化"""

[tf.summary 与 tensorboard]

参数

log_dir (string) – Save directory location. Default is runs/CURRENT_DATETIME_HOSTNAME, which changes after each run.

flush_secs (int) – How often, in seconds, to flush the pending events and summaries to disk. Default is every two minutes.

使用

from torch.utils.tensorboard import SummaryWriter

# folder location: runs/May04_22-14-54_s-MacBook-Pro.local/
writer = SummaryWriter()

# 指定写入目录
sum_writer = SummaryWriter(sumfile)

e.g. sumfile = 'summary_log_dir'+time.strftime("%H%M", time.localtime())

记录 Scalars

我们常用的 loss、accuracy 等都是数值,我们在 Tensorboard 中记录数值的方法也很简单:

add_scalar(tag, scalar_value, global_step=None, walltime=None)

  • tag 是这个常数值所属的标签(比如 training_loss 等)。常用的一个方法是将 tag 值设置为 section/plot 的格式,这样 Tensorboard 会按照 section 来给结果分组(下面有例子)。
  • global_step 是一个整数,通常是曲线图里的 x 轴,如果不设置则默认一直为 0。注意这里是不存在覆盖的,就是对于同一个 global_step 值,新的值不会覆盖旧的值,而是会同时画到图上。
  • walltime 就是记录的时间戳,默认是系统当前时间 time.time()。

# Record training loss from each epoch into the writer
    writer.add_scalar(\\\\'Train/Loss\\\\', loss.item(), epoch)

    sum_writer.add_scalar(tag='Train/Loss', scalar_value=model.loss_data.item() / last_batch_id, global_step=epoch)
# Record loss and accuracy from the test run into the writer
    writer.add_scalar(\\\\'Test/Loss\\\\', test_loss, epoch)
    writer.add_scalar(\\\\'Test/Accuracy\\\\', accuracy, epoch)

add_scalars(main_tag, tag_scalar_dict, global_step=None, walltime=None)
多个数值展示在同一个图上。
writer.add_scalars('All/Logloss', 'TrainLogloss': logloss, 'TestLogloss': t_logloss, epoch)

可视化

cd du_project/test/summary_log_dir
tensorboard --logdir=./sumfile/ --port 6006

Serving TensorBoard on localhost; to expose to the network, use a proxy or pass --bind_all
TensorBoard 2.3.0 at http://localhost:6006/ (Press CTRL+C to quit)

点击http就可以看到可视化界面

 -柚子皮-

 

第三方工具

[《PyTorch 使用 TensorboardX 进行网络可视化》][Pytorch使用tensorboardX可视化。超详细!!!]

《PyTorch 可视化工具 Visdom 介绍》

《使用 Visdom 在 PyTorch 中进行可视化》

from: -柚子皮-

ref: [https://pytorch.org/docs/stable/tensorboard.html]

[PyTorch 自带 TensorBoard 使用教程]

 

以上是关于PyTorch:可视化TensorBoard的主要内容,如果未能解决你的问题,请参考以下文章

PyTorch:可视化TensorBoard

[十九]深度学习Pytorch-可视化工具TensorBoard

深度学习pytorch使用tensorboard可视化实验数据

深度学习pytorch使用tensorboard可视化实验数据

pytorch实战学习第七篇:tensorboard可视化介绍

学术福利Pytorch的tensorboard食谱帮你可视化误差结果