【pytorch】tensorboard的使用

一、tensorboard的说明

TensorBoard是深度学习领域通用的可视化工具套件,TensorBoard的核心价值在于将抽象的模型训练过程转化为直观的可视化图表。

在PyTorch中,SummaryWriter是TensorBoard的数据写入器,用于将训练数据(指标、模型结构、图像等)记录到事件文件(event files)中。

python 复制代码
class SummaryWriter(object):
    """Writes entries directly to event files in the log_dir to be
    consumed by TensorBoard.

    The `SummaryWriter` class provides a high-level API to create an event file
    in a given directory and add summaries and events to it. The class updates the
    file contents asynchronously. This allows a training program to call methods
    to add data to the file directly from the training loop, without slowing down
    training.
    """

记录标量(指标):使用add_scalar方法跟踪损失、准确率等指标。

记录图像:使用add_image方法展示训练数据或生成样本。

二、tensorboard的画图使用

add_scalar:scalar_value是y轴,global_step是x轴

创建y=x和y=2x的图,并通过网页展示

python 复制代码
from torch.utils.tensorboard import SummaryWriter

writer = SummaryWriter('logs')
# writer.add_image()
for i in range(100):
    writer.add_scalar('y=2x', 2*i, i)

writer.close()

在控制台输出,也可以指定其他端口tensorboard --logdir=logs --port=6007:

powershell 复制代码
tensorboard --logdir=logs

输出

浏览器打开http://localhost:6006/

三、tensorboard的记录图片使用

python 复制代码
from torch.utils.tensorboard import SummaryWriter
import numpy as np
from PIL import Image

writer = SummaryWriter('logs')
image_path = r'data/train/ants_image/0013035.jpg'
img_PIL = Image.open(image_path)
img_array = np.array(img_PIL)
#img_array的shape是(512, 768, 3),通道3,需要加dataformats='HWC'参数,否则报错
writer.add_image("test", img_array, 1, dataformats='HWC')


# for i in range(100):
#     writer.add_scalar('y=x', i, i)

writer.close()

输出

相关推荐
淮北4948 小时前
图神经网络与pytorch
人工智能·pytorch·神经网络
爱ZW的小白猿9 小时前
【pytorch】transform的使用
pytorch
laocooon52385788610 小时前
TensorFlow与 PyTorch有什么关联么
人工智能·pytorch·tensorflow
盼小辉丶10 小时前
生成模型实战 | 残差流(Residual Flow)详解与实现
pytorch·深度学习·生成模型
IT老兵20251 天前
PyTorch DDP多GPU训练实践问题总结
人工智能·pytorch·python·分布式训练·ddp
All The Way North-1 天前
AdaGrad 深度解析:从数学原理到 PyTorch 实现,为什么它在稠密问题中“学不动”?
pytorch·深度学习·adagrad算法·梯度下降优化算法
keineahnung23451 天前
從 SymBool 到 SymFloat:PyTorch user magic methods 如何支持符號形狀運算?
人工智能·pytorch·python·深度学习
Rabbit_QL2 天前
【PyTorch】detach:从计算图中切断梯度的原理与实践
人工智能·pytorch·python
FrameNotWork2 天前
HarmonyOS 教学实战:从 0 写一个完整应用(真正能跑、能扩展)
pytorch·华为·harmonyos