当前位置:网站首页>Pytorch框架学习记录2——TensorBoard的使用
Pytorch框架学习记录2——TensorBoard的使用
2022-07-30 03:54:00 【柚子Roo】
Pytorch框架学习记录2——TensorBoard的使用
Tensorboard在训练模型时很有用,可以看训练过程中loss的变化。之前用于Tensorflow框架,自Pytorch1.1之后,Pytorch也加了这个功能。
1. TensorBoard的使用
从torch.utils.tensorboard中导入SummaryWriter类。
1.1 SummaryWriter类使用
SummaryWriter类可以在指定文件夹生成一个事件文件,这个事件文件可以对TensorBoard解析。
首先实例化一个SummaryWriter的类,参数代表保存的文件夹的名称
writer = SummaryWriter("logs") #文件夹名称
1.2 writer.add_scalar()方法:
def add_scalar(
self,
tag,
scalar_value,
global_step=None,
walltime=None,
new_style=False,
double_precision=False,
):
上面是官方定义的参数,这个方法是添加标量的意思。
tag:所画图标的title,str类型,注意引号
scalar_value:需要保存的数值,对应y轴的y值
global_step:全局的步数,对应x轴
这里的tag起到唯一标识图像的作用,如果需要两幅图,则重新改写tag!!!
1.4 举例
绘制y = x 的图像。
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter("logs")
for i in range(100):
writer.add_scalar("y = x", i, i)
writer.close()
1.5 add_image()方法
def add_image(self, tag, img_tensor, global_step=None, walltime=None, dataformats='CHW'):
''' Args: tag (string): Data identifier img_tensor (torch.Tensor, numpy.array, or string/blobname): Image data global_step (int): Global step value to record walltime (float): Optional override default walltime (time.time()) seconds after epoch of event Shape: img_tensor: Default is :math:`(3, H, W)`. You can use ``torchvision.utils.make_grid()`` to convert a batch of tensor into 3xHxW format or call ``add_images`` and let us do the job. Tensor with :math:`(1, H, W)`, :math:`(H, W)`, :math:`(H, W, 3)` is also suitable as long as corresponding ``dataformats`` argument is passed, e.g. ``CHW``, ``HWC``, ``HW``. '''
其中,img_tensor参数类型要求为:torch.Tensor、numpy.array或者string类型。global_step为步骤,int类型。
img_tensor默认的图片尺寸格式为(3,H,W),但是一般我们的图片格式为(H,W,3),因此需要对图片格式进行调整,调整的方法如下:设置dataformats='HWC'即可!!!
from torch.utils.tensorboard import SummaryWriter
from PIL import Image
import numpy as np
writer = SummaryWriter("logs")
img_path = "C:\\Users\\hp\\PycharmProjects\pythonProject\\Pytorch_Learning\\flower_data\\train\daisy\\5547758_eea9edfd54_n.jpg"
img_PIL = Image.open(img_path)
img_array = np.array(img_PIL)
print(type(img_array))
writer.add_image("test", img_array, 1, dataformats='HWC')
for i in range(100):
writer.add_scalar("y = x", i, i)
writer.close()

1.6 事件文件打开方法:
在PyCharm中,找到项目所在的terminal,
tensorboard --logdir=事件文件所在的文件夹名 --port=端口号
在弹出的网址上打开即可。

![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yfqvOSZM-1658972434470)(C:\Users\hp\AppData\Roaming\Typora\typora-user-images\image-20220728092728557.png)]](/img/b4/b5f851ad538fb869c29b69676de9c7.png)
2. 常见的报错情况
2.1 ImportError: TensorBoard logging requires TensorBoard version 1.15 or above
tensorboard版本过低,卸载后重新安装最新版本。
2.2 AttributeError: module ‘tensorflow._api.v1.io’ has no attribute ‘gfile’
tensorflow 的版本和tensorboard的版本不匹配。升级tensorflow版本为2.0版本即可,执行下面命令会自动升级tensorflow版本和tensorboard版本:
pip install tensorflow==2.0
2.3 ModuleNotFoundError: No module named ‘grpc’
重新安装grpcio
pip uninstall grpcio
pip install grpcio
2.4 No dashboards are active for the current data set.
打开的文件位置不存在,将文件夹位置的绝对路径复制,然后重新执行terminal语句即可
边栏推荐
- Tcp编程
- 逆向分析实战2
- CMake的安装和测试
- Hystrix service circuit breaker
- OpenFeign implementation downgrade
- Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (4) Opening Report
- 发给你的好友,让 TA 请你吃炸鸡!
- vscode debugging and remote
- 传统项目转型
- EasyCVR启动时报错“no such file or directory”,该如何解决?
猜你喜欢

CMake的安装和测试

ospf 综合实验(重发布,特殊区域)

北京bgp机房和普通机房的区别

Nacos achieves high availability

Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (6) Question Opening Reply PPT

微服务CAP原则

WeChat second-hand transaction small program graduation design finished works (8) graduation design thesis template

Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Work (7) Interim Inspection Report

What is the difference between mission, vision and values?

【Use of scientific research tools】A
随机推荐
Public chains challenging the "Impossible Triangle"
小程序毕设作品之微信积分商城小程序毕业设计成品(8)毕业设计论文模板
淘宝/天猫获取卖出的商品订单列表 API
路由过滤器
组织在线化:组织数字化变革的新趋势
进程优先级 nice
Wechat second-hand transaction small program graduation design finished product (1) Development overview
Process priority nice
Taobao/Tmall get Taobao store details API
逆向分析实战2
操作配置:如何在一台服务器中以服务方式运行多个EasyCVR程序?
小程序毕设作品之微信二手交易小程序毕业设计成品(2)小程序功能
redis分布式锁的原子保证
北京bgp机房和普通机房的区别
Anti-shake and throttling
sublime text 3 settings
Let's learn the layout components of flutter together
第51篇-知乎请求头参数分析【2022-07-28】
【转】Swift 中的面向协议编程:引言
Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (7) Interim Inspection Report