当前位置:网站首页>PyTorch入门:(二)Tensorboard的使用
PyTorch入门:(二)Tensorboard的使用
2022-08-08 18:59:00 【Here_SDUT】
前言:本文为学习 PyTorch深度学习快速入门教程(绝对通俗易懂!)【小土堆】时记录的 Jupyter 笔记,部分截图来自视频中的课件。
SummaryWriter对象:
class SummaryWriter(builtins.object)
| SummaryWriter(log_dir=None, comment='', purge_step=None, max_queue=10, flush_secs=120, filename_suffix='')
|
| 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.
__init__(self, log_dir=None, comment='', purge_step=None, max_queue=10, flush_secs=120, filename_suffix='')
| Creates a `SummaryWriter` that will write out events and summaries
| to the event file.
|
| Args:
| log_dir (string): Save directory location. Default is
| runs/**CURRENT_DATETIME_HOSTNAME**, which changes after each run.
| Use hierarchical folder structure to compare
| between runs easily. e.g. pass in 'runs/exp1', 'runs/exp2', etc.
| for each new experiment to compare across them.
| comment (string): Comment log_dir suffix appended to the default
| ``log_dir``. If ``log_dir`` is assigned, this argument has no effect.
|
| Examples::
|
| from torch.utils.tensorboard import SummaryWriter
|
| # create a summary writer with automatically generated folder name.
| writer = SummaryWriter()
| # folder location: runs/May04_22-14-54_s-MacBook-Pro.local/
|
| # create a summary writer using the specified folder name.
| writer = SummaryWriter("my_experiment")
| # folder location: my_experiment
|
| # create a summary writer with comment appended.
| writer = SummaryWriter(comment="LR_0.1_BATCH_16")
| # folder location: runs/May04_22-14-54_s-MacBook-Pro.localLR_0.1_BATCH_16/add_scalar方法–新增标量
add_scalar(self, tag, scalar_value, global_step=None, walltime=None, new_style=False, double_precision=False)
| Add scalar data to summary.
|
| Args:
| tag (string): Data identifier(图标标题)
| scalar_value (float or string/blobname): Value to save
| global_step (int): Global step value to record
| walltime (float): Optional override default walltime (time.time())
| with seconds after epoch of event
| new_style (boolean): Whether to use new style (tensor field) or old
| style (simple_value field). New style could lead to faster data loading.add_image方法–新增图片
add_image(self, tag, img_tensor, global_step=None, walltime=None, dataformats='CHW')
| Add image data to summary.
|
| Note that this requires the ``pillow`` package.
|
| 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 eventfrom torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter("logs") # 设置文件名为logs
# writer.add_image()
# y = x
for i in range(100) :
writer.add_scalar("y=x^2",i*i,i)
writer.close()可以发现在和代码存放路径同级的路径下会多出一个 logs 的文件夹,文件夹内文件如下:
然后需要在终端启动tensorboard,使用如下命令: tensorboard --logdir=D:\work\StudyCode\jupyter\logs --port=6007
--logdir=写log所在路径--port=写想在哪个端口打开tensorboard
结果如下:
需要注意的是,使用add_scalar方法,如果图片的标题(tag)相同的话,数据是累加上去的,而不是覆盖,如果需要重新绘图就要删掉log文件,重新运行。
下面演示在tensorboard中添加图片,与添加标量不同的是,添加的图片必须是tensor类型或者numpy类型,并且还要指定数据每一维度的意义(长、宽、通道)
import numpy as np
from PIL import Image
image_path = "D:/work/StudyCode/jupyter/dataset_for_pytorch_dataloading/train/ants/0013035.jpg"
img_PIL = Image.open(image_path)
img_array = np.array(img_PIL)
writer.add_image("test", img_array, 1, dataformats="HWC")边栏推荐
- nyoj714 Card Trick(第六届河南省程序设计大赛)
- 十六、一起学习Lua 文件 I/O
- 我们想更换RDS数据库,从sqlserver 2016 web升级到 2017企业集群版,有专家咨询
- Build DG will increase the amount of lead to archive log problem
- ABAP 报表中如何给报表的输入参数增添 F4 Value Help
- 在Unity URP中实现Forward+
- PyTorch入门:(五)模型的搭建
- 软考中级网络工程师全面学习笔记第2版(5万字)+配套视频及课件
- nyoj 712 探 寻 宝 藏(双线dp 第六届河南省程序设计大赛)
- Rethinking HTAP database caused by rereading GPDB and TiDB papers
猜你喜欢
随机推荐
C语言初阶-结构体
Redhat 7 Maria DB installation and configuration
【kali-权限提升】(4.2.6)社会工程学工具包(上):中间人攻击原理
We want to replace the RDS database and upgrade from sqlserver 2016 web to 2017 enterprise cluster version, with expert consultation
APICloud AVM 封装日期和时间选择组件
文档管理系统对于企业来说有哪些作用?
鹅厂机器狗花式穿越10m梅花桩:前空翻、单桩跳、起身作揖...全程不打一个趔趄
nyoj714 Card Trick(第六届河南省程序设计大赛)
Implementing Forward+ in Unity URP
Generate captchas tools
性能优化|从ping延时看CPU电源管理
OpenSSH生成的私钥如何在putty中使用?
16. Learn Lua file I/O together
数组!!!
Implement the entire process of Mock API with tools
MogDB学习笔记-从0开始
一起了解分层架构&SOA架构
golang流程控制:if分支、switch分支和fallthrough switch穿透
无标题文章
Fortinet全新云原生保护产品上线亚马逊云科技平台









