【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()

输出

相关推荐
独隅9 小时前
PyTorch 分布式训练完整指南:策略、实现与模型选型
人工智能·pytorch·分布式
沪漂阿龙9 小时前
卷积神经网络(CNN)零基础通关指南:原理、图解与PyTorch实战
人工智能·pytorch·cnn
龙文浩_1 天前
AI中NLP的注意力机制的计算公式解析
人工智能·pytorch·深度学习·神经网络·自然语言处理
断眉的派大星1 天前
pytorch中链式法则求解梯度
人工智能·pytorch·python
龙文浩_1 天前
AI中NLP的深入浅出注意力机制 Seq2Seq 模型
人工智能·pytorch·深度学习·神经网络·自然语言处理
却道天凉_好个秋2 天前
pytorch(一):张量
人工智能·pytorch·python·深度学习
网上邻居YY2 天前
深度学习DL 之 安装PyTorch·GPU版、CUDA(本人Anaconda、Python、PyCharm已提前安装好)
pytorch·经验分享·python·深度学习·pycharm·学习方法
ZhiqianXia2 天前
Pytorch 学习笔记(21) : PyTorch Profiler
pytorch·笔记·学习
盼小辉丶2 天前
PyTorch实战(41)——Hugging Face在PyTorch中的应用
人工智能·pytorch·深度学习·hugging face
ZhiqianXia2 天前
PyTorch学习笔记(16):scheduler.py
pytorch·笔记·学习