当前位置:网站首页>TensorBoard快速入门(Pytorch使用TensorBoard)
TensorBoard快速入门(Pytorch使用TensorBoard)
2022-07-03 17:17:00 【iioSnail】
文章目录
TensorBoard 简介
TensorBoard 是Google开发的一个机器学习可视化工具。其主要用于记录机器学习过程,例如:
- 记录损失变化、准确率变化等
- 记录图片变化、语音变化、文本变化等,例如在做GAN时,可以过一段时间记录一张生成的图片
- 绘制模型
具体支持的内容可以参考官方文档
TensorBoard 界面介绍

TensorBoard 安装
直接使用pip安装即可:
pip install tensorboard
安装后,在命令行输入:
tensorboard --help
若可以正常输出,则说明安装成功。
TensorBoard 运行
运行启动命令即可:
tensorboard --logdir my_log
my_log是TensorBoard的log文件所在的目录。Tensorboard面板中展示的数据都来源于log文件,一般一次完整的运行生成一份log文件。
例如,在Pytorch中,我们会调用new一个SummaryWriter对象,此时就会创建一个log文件,之后我们就会调用其add_something方法,往log里面写日志,之后在TensorBoard面板中就可以看到数据了。最后在训练完成后,调用close方法结束。
若看到了如下输出,说明启动成功:
TensorBoard 2.8.0 at http://localhost:6006/ (Press CTRL+C to quit)
此时只需要在浏览器中输入http://localhost:6006/即可进入TensorBoard界面。
Pytorch 使用 TensorBoard
Pytorch使用Tensorboard主要用到了三个API:
SummaryWriter:这个用来创建一个log文件,TensorBoard面板查看时,也是需要选择查看那个log文件。add_something: 向log文件里面增添数据。例如可以通过add_scalar增添折线图数据,add_image可以增添图片。close:当训练结束后,我们可以通过close方法结束log写入。
接下来,我们来模拟记录训练过程中准确率的变化。
首先需要new一个SummaryWriter对象:
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter(log_dir='runs/mock_loss')
当运行完该行代码后,可以看到当前目录下生成了一个runs/mock_accuracy文件夹,并且里面有event日志
此时已经可以启动tensorboard来查看了:
tensorboard --logdir runs
此时进入tensorboard页面后,啥都看不到,因为我们还没有向log里面写入任何数据:

接下来使用 add_scalar 绘制准确率折线图:
for i in range(100):
writer.add_scalar(tag="accuracy", # 可以暂时理解为图像的名字
scalar_value=i * random.uniform(0.8, 1), # 纵坐标的值
global_step=i # 当前是第几次迭代,可以理解为横坐标的值
)
time.sleep(2 * random.uniform(0.5, 1.5))
这里模拟 1~3 秒完成一次迭代并计算准确率,然后将准确率增添到 accuracy 这个图下面。
过一会后,我们刷新页面,就可以看到我们的准确率变化曲线了:

由于数据还在写入,所以曲线还在不断变化。
Tensor折线图(Scalars)

- TODO:其他图形的介绍
边栏推荐
- [JDBC] API parsing
- [error reporting] omp: error 15: initializing libiomp5md dll, but found libiomp5md. dll already initialized.
- STM32H7 HAL库SPI DMA发送一直处于busy的解决办法
- HP 阵列卡排障一例
- The most complete postman interface test tutorial in the whole network, API interface test
- visual studio “通常每个套接字地址(协议/网络地址/端口)只允许使用一次“
- PS screen printing brush 131, many illustrators have followed suit
- The difference between get and post
- Financial management (Higher Vocational College) financial management online Assignment 1 in autumn 20
- 聊聊接口优化的几个方法
猜你喜欢

kubernetes资源对象介绍及常用命令(三)

问题随记 —— 在 edge 上看视频会绿屏

Unity notes unityxr simple to use

静态程序分析(一)—— 大纲思维导图与内容介绍

聊聊接口优化的几个方法

What is your income level in the country?

Kotlin学习快速入门(7)——扩展的妙用

免费数据 | 新库上线 | CnOpenData中国保险中介机构网点全集数据

Simple use of unity pen XR grab

Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
随机推荐
[2. Basics of Delphi grammar] 1 Identifiers and reserved words
Analysis of variance summary
Answer to the homework assessment of advanced English reading (II) of the course examination of Fuzhou Normal University in February 2022
Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
绝对定位时元素水平垂直居中
[combinatorics] recursive equation (the relationship theorem between the solution of the recursive equation and the characteristic root | the linear property theorem of the solution of the recursive e
Kotlin learning quick start (7) -- wonderful use of expansion
How to promote cross department project collaboration | community essay solicitation
[UE4] brush Arctic pack high quality Arctic terrain pack
[try to hack] active detection and concealment technology
Collection of the most beautiful graduation photos in the graduation season, collection of excellent graduation photos
在iptables防火墙下开启vsftpd的端口
How to delete a specific line from a text file using the SED command?
C language string practice
Execute script unrecognized \r
网络硬盘NFS的安装与配置
RDS数据库的监测页面在哪看?
How to judge the region of an IP through C?
汇编实例解析--实模式下屏幕显示
PS screen printing brush 131, many illustrators have followed suit