简介
什么是tensorflow呢?从其名称我们可以猜想,是对tensor信息流动过程的记录工具!
当我们训练神经网络时,我们希望去跟踪神经网络的整个训练过程中的信息,比如迭代的过程中每一层参数是如何变化与分布的,比如每次循环参数更新后模型在测试集与训练集上的准确率是如何的,比如损失值的变化情况,等等。如果能在训练的过程中将一些信息加以记录并可视化得表现出来,是不是对我们探索模型有更深的帮助与理解呢?
Tensorflow官方推出了可视化工具Tensorboard,可以帮助我们实现以上功能,它可以将模型训练过程中的各种数据汇总起来存在自定义的路径与日志文件中,然后在指定的web端可视化地展现这些信息。
使用例子
安装
可以直接使用 pip 进行安装,或者从源码进行安装。
- 使用 pip 安装
pip install tensorboardX
- 从源码安装
git clone https://github.com/lanpa/tensorboardX && cd tensorboardX && python setup.py install
例子
安装好以后,怎么使用呢。首先需要导入包,然后创建log记录器。
from tensorboardX import SummaryWriter
这有三个创建log记录器的方式:
# Creates writer1 object.
# The log will be saved in 'runs/exp'
writer1 = SummaryWriter('runs/exp')
# Creates writer2 object with auto generated file name
# The log directory will be something like 'runs/Aug20-17-20-33'
writer2 = SummaryWriter()
# Creates writer3 object with auto generated file name, the comment will be appended to the filename.
# The log directory will be something like 'runs/Aug20-17-20-33-resnet'
writer3 = SummaryWriter(comment='resnet')
然后就可以添加想要记录的信息,比如要记录loss:
writer2.add_scalar('train_loss', train_loss,step) # step为当前迭代次数
如何可视化
可视化是在网页中呈现的,首先在命令行打开tensorboard:
tensorboard --logdir=/路径/logdir
然后在浏览器中输入:
http://localhost:6006
就可以打开gui界面了!
这里只是一个简单例子,详细的可以去官方教程查看。