当前位置:网站首页>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:其他图形的介绍
边栏推荐
- Apache service suspended asynchronous acceptex failed
- 【JokerのZYNQ7020】DDS_ Compiler。
- 定义一个结构体Fraction,表示分数,用于表示 2/3, 5/6这样的分数
- One brush 149 force deduction hot question-10 regular expression matching (H)
- 在iptables防火墙下开启vsftpd的端口
- [combinatorics] recursive equation (the problem of solving recursive equation with multiple roots | the problem is raised)
- The largest matrix (H) in a brush 143 monotone stack 84 histogram
- Svn full backup svnadmin hotcopy
- How to promote cross department project collaboration | community essay solicitation
- How SVN views modified file records
猜你喜欢

How to train mask r-cnn model with your own data

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

New features of C 10
![[RT thread] construction and use of --hwtimer of NXP rt10xx device driver framework](/img/df/a7719bcb00ff66e21f3a391ab94573.png)
[RT thread] construction and use of --hwtimer of NXP rt10xx device driver framework

Why is WPA3 security of enterprise business so important?
![[JDBC] API parsing](/img/75/0f69a4e246a571688355bb13e2cd73.jpg)
[JDBC] API parsing

C language modifies files by line

设计电商秒杀

Design e-commerce spike

PHP online confusion encryption tutorial sharing + basically no solution
随机推荐
大消费企业怎样做数字化转型?
Test your trained model
Dagong 21 autumn "power plant electrical part" online operation 1 [standard answer] power plant electrical part
企业级自定义表单引擎解决方案(十一)--表单规则引擎1
【RT-Thread】nxp rt10xx 设备驱动框架之--Audio搭建和使用
跨境电商:外贸企业做海外社媒营销的优势
RedHat 6.2 configuring ZABBIX
[RT thread] NXP rt10xx device driver framework -- RTC construction and use
Electronic technology 20th autumn "Introduction to machine manufacturing" online assignment 3 [standard answer]
Kotlin learning quick start (7) -- wonderful use of expansion
Online assignment 3 of mobile Internet technology in the 20th autumn of electronic technology [standard answer]
【Try to Hack】主动侦查隐藏技术
Analysis of variance summary
[RT thread] NXP rt10xx device driver framework -- pin construction and use
C language modifies files by line
[combinatorics] recursive equation (solution of linear non-homogeneous recursive equation with constant coefficients | standard form and general solution of recursive equation | proof of general solut
Kubernetes resource object introduction and common commands (4)
Résolution de l'instance d'assemblage - - affichage à l'écran en mode réel
Deep understanding of grouping sets statements in SQL
一位普通程序员一天工作清单