Pytorch Tensorboard 笔记

安装运行

pip install tensorboard
tensorboard --logdir=runs

快速示例

import torch
import torchvision
from torch.utils.tensorboard import SummaryWriter
from torchvision import datasets, transforms

# Writer will output to ./runs/ directory by default
writer = SummaryWriter()

transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
trainset = datasets.MNIST('mnist_train', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
model = torchvision.models.resnet50(False)
# Have ResNet model take in grayscale rather than RGB
model.conv1 = torch.nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=False)
images, labels = next(iter(trainloader))

grid = torchvision.utils.make_grid(images)
writer.add_image('images', grid, 0)
writer.add_graph(model, images)
writer.close()

SummaryWriter 类

torch.utils.tensorboard.writer.SummaryWriter(log_dir=None, comment='', purge_step=None, max_queue=10, flush_secs=120, filename_suffix='')

__init__

__init__(log_dir=None, comment='', purge_step=None, max_queue=10, flush_secs=120, filename_suffix='')
  • log_dir(string) - log 存储路径。默认为 runs/CURRENT_DATETIME_HOSTNAME
  • comment(string)
  • purge_step(int)
  • max_queue(int)
  • flush_secs(int) - 事件和摘要写入硬盘的间隔。默认为 2 分钟。
  • filename_suffix(string)

示例:

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(tag, scalar_value, global_step=None, walltime=None, new_style=False, double_precision=False)
  • tag(string) - 数据名称
  • scalar_value(float or string/blobname) - 数据值
  • global_step(int) - 一般是指横坐标 x
  • walltime(float)
  • new_style(boolean)

示例:

from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter()
x = range(100)
for i in x:
    writer.add_scalar('y=2x', i * 2, i)
writer.close()

在这里插入图片描述

add_scalars

add_scalars(main_tag, tag_scalar_dict, global_step=None, walltime=None)
  • main_tag(string) - 数据名称
  • tag_scalar_dict(dict) - 键值对
  • global_step(int) - 一般是指横坐标 x
  • walltime(float)
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter()
r = 5
for i in range(100):
    writer.add_scalars('run_14h', {'xsinx':i*np.sin(i/r),
                                    'xcosx':i*np.cos(i/r),
                                    'tanx': np.tan(i/r)}, i)
writer.close()
# This call adds three values to the same scalar plot with the tag
# 'run_14h' in TensorBoard's scalar section.

在这里插入图片描述

add_histogram

add_histogram(tag, values, global_step=None, bins='tensorflow', walltime=None, max_bins=None)
  • tag(string) - 数据名称
  • values(torch.Tensor, dumpy.array, or string/blobname) - 数据值
  • global_step(int) - 一般是指横坐标 x
  • bins(string) - {‘tensorflow’,’auto’, ‘fd’, …} 之一。https://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html
  • walltime(float)

示例:

from torch.utils.tensorboard import SummaryWriter
import numpy as np
writer = SummaryWriter()
for i in range(10):
    x = np.random.random(1000)
    writer.add_histogram('distribution centers', x + i, i)
writer.close()

在这里插入图片描述

add_image

add_image(tag, img_tensor, global_step=None, walltime=None, dataformats='CHW')
  • tag(string) - 数据名称
  • img_tensor(torch.Tensor, dumpy.array, or string/blobname) - 数据值
  • global_step(int) - 一般是指横坐标 x
  • walltime(float)
  • dataformats(string)

img_tensor: 默认为 (3,H,W)。可以使用 torchvision.utils.make_grid()把一个 batch_size 的图片合为一张。也可以直接调用add_images。(1,H,W),(H,W), (H,W,3) 格式的 Tensor 也可以接受,只要 dataformats参数传对,例如 CHW,HWC, HW

示例:

from torch.utils.tensorboard import SummaryWriter
import numpy as np
img = np.zeros((3, 100, 100))
img[0] = np.arange(0, 10000).reshape(100, 100) / 10000
img[1] = 1 - np.arange(0, 10000).reshape(100, 100) / 10000

img_HWC = np.zeros((100, 100, 3))
img_HWC[:, :, 0] = np.arange(0, 10000).reshape(100, 100) / 10000
img_HWC[:, :, 1] = 1 - np.arange(0, 10000).reshape(100, 100) / 10000

writer = SummaryWriter()
writer.add_image('my_image', img, 0)

# If you have non-default dimension setting, set the dataformats argument.
writer.add_image('my_image_HWC', img_HWC, 0, dataformats='HWC')
writer.close()

在这里插入图片描述

add_images

add_images(tag, img_tensor, global_step=None, walltime=None, dataformats='NCHW')
  • tag(string) - 数据名称
  • img_tensor(torch.Tensor, dumpy.array, or string/blobname) - 数据值
  • global_step(int) - 一般是指横坐标 x
  • walltime(float)
  • dataformats(float) - NCHW, NHWC, CHW, HWC, HW, WH, etc.

img_tensor: 默认为 (N,3,H,W).

示例:

from torch.utils.tensorboard import SummaryWriter
import numpy as np

img_batch = np.zeros((16, 3, 100, 100))
for i in range(16):
    img_batch[i, 0] = np.arange(0, 10000).reshape(100, 100) / 10000 / 16 * i
    img_batch[i, 1] = (1 - np.arange(0, 10000).reshape(100, 100) / 10000) / 16 * i

writer = SummaryWriter()
writer.add_images('my_image_batch', img_batch, 0)
writer.close()

在这里插入图片描述

add_figure

add_figure(tag, figure, global_step=None, close=True, walltime=None)

把 matplotlib figure 渲染成图片并显示

Parameters

  • tag (string) – Data identifier
  • figure (matplotlib.pyplot.figure) – Figure or a list of figures
  • global_step (int) – Global step value to record
  • close (bool) – Flag to automatically close the figure
  • walltime (float) – Optional override default walltime (time.time()) seconds after epoch of event

add_video

add_video(tag, vid_tensor, global_step=None, fps=4, walltime=None)

Parameters

  • tag (string) – Data identifier
  • vid_tensor (torch.Tensor) – Video data
  • global_step (int) – Global step value to record
  • fps (float or int) – Frames per second
  • walltime (float) – Optional override default walltime (time.time()) seconds after epoch of event

vid_tensor: (N,T,C,H,W). The values should lie in [0, 255] for type uint8 or [0, 1] for type float.

add_audio

add_audio(tag, snd_tensor, global_step=None, sample_rate=44100, walltime=None)

Parameters

  • tag (string) – Data identifier
  • snd_tensor (torch.Tensor) – Sound data
  • global_step (int) – Global step value to record
  • sample_rate (int) – sample rate in Hz
  • walltime (float) – Optional override default walltime (time.time()) seconds after epoch of event

snd_tensor: (1,L). The values should lie between [-1, 1].

add_text

add_text(tag, text_string, global_step=None, walltime=None)

Parameters

  • tag (string) – Data identifier
  • text_string (string) – String to save
  • global_step (int) – Global step value to record
  • walltime (float) – Optional override default walltime (time.time()) seconds after epoch of event

示例:

writer.add_text('lstm', 'This is an lstm', 0)
writer.add_text('rnn', 'This is an rnn', 10)

add_graph

add_graph(model, input_to_model=None, verbose=False, use_strict_trace=True)

Parameters

  • model (torch.nn.Module) – Model to draw
  • input_to_model (torch.Tensor or list of torch.Tensor) – A variable or a tuple of variables to be fed
  • verbose (bool) – Whether to print graph structure in console
  • use_strict_trace (bool) – Whether to pass keyword argument strict to torch.jit.trace. Pass False when you want the tracer to record your mutable container types (list, dict)

add_embedding

add_embedding(mat, metadata=None, label_img=None, global_step=None, tag='default', metadata_header=None)

Parameters

  • mat (torch.Tensor or numpy.array) – A matrix which each row is the feature vector of the data point
  • metadata (list) – A list of labels, each element will be convert to string
  • label_img (torch.Tensor) – Images correspond to each data point
  • global_step (int) – Global step value to record
  • tag (string) – - metadata (list) – A list of labels, each element will be convert to string

mat: (N,D), where N is number of data and D is feature dimension

label_img: (N,C,H,W)

示例:

import keyword
import torch
meta = []
while len(meta)<100:
    meta = meta+keyword.kwlist # get some strings
meta = meta[:100]

for i, v in enumerate(meta):
    meta[i] = v+str(i)

label_img = torch.rand(100, 3, 10, 32)
for i in range(100):
    label_img[i]*=i/100.0

writer.add_embedding(torch.randn(100, 5), metadata=meta, label_img=label_img)
writer.add_embedding(torch.randn(100, 5), label_img=label_img)
writer.add_embedding(torch.randn(100, 5), metadata=meta)

add_pr_curve

add_pr_curve(tag, labels, predictions, global_step=None, num_thresholds=127, weights=None, walltime=None)

Adds precision recall curve. Plotting a precision-recall curve lets you understand your model’s performance under different threshold settings. With this function, you provide the ground truth labeling (T/F) and prediction confidence (usually the output of your model) for each target. The TensorBoard UI will let you choose the threshold interactively.

参数:

  • tag (string) – Data identifier
  • labels (torch.Tensor, numpy.array, or string/blobname) – Ground truth data. Binary label for each element.
  • predictions (torch.Tensor, numpy.array, or string/blobname) – The probability that an element be classified as true. Value should be in [0, 1]
  • global_step (int) – Global step value to record
  • num_thresholds (int) – Number of thresholds used to draw the curve.
  • walltime (float) – Optional override default walltime (time.time()) seconds after epoch of event

示例:

from torch.utils.tensorboard import SummaryWriter
import numpy as np
labels = np.random.randint(2, size=100)  # binary label
predictions = np.random.rand(100)
writer = SummaryWriter()
writer.add_pr_curve('pr_curve', labels, predictions, 0)
writer.close()

add_custom_scalars

add_custom_scalars(layout)
Create special chart by collecting charts tags in ‘scalars’. Note that this function can only be called once for each SummaryWriter() object. Because it only provides metadata to tensorboard, the function can be called before or after the training loop.

Parameters

  • layout (dict) – {categoryName: charts}, where charts is also a dictionary {chartName:ListOfProperties}. The first element in ListOfProperties is the chart’s type (one of Multiline or Margin) and the second element should be a list containing the tags you have used in add_scalar function, which will be collected into the new chart.

Examples:

layout = {'Taiwan':{'twse':['Multiline',['twse/0050', 'twse/2330']]},
             'USA':{ 'dow':['Margin',   ['dow/aaa', 'dow/bbb', 'dow/ccc']],
                  'nasdaq':['Margin',   ['nasdaq/aaa', 'nasdaq/bbb', 'nasdaq/ccc']]}}

writer.add_custom_scalars(layout)

add_mesh

add_mesh(tag, vertices, colors=None, faces=None, config_dict=None, global_step=None, walltime=None)

Add meshes or 3D point clouds to TensorBoard. The visualization is based on Three.js, so it allows users to interact with the rendered object. Besides the basic definitions such as vertices, faces, users can further provide camera parameter, lighting condition, etc. Please check https://threejs.org/docs/index.html#manual/en/introduction/Creating-a-scene for advanced usage.

Parameters

  • tag (string) – Data identifier
  • vertices (torch.Tensor) – List of the 3D coordinates of vertices.
  • colors (torch.Tensor) – Colors for each vertex
  • faces (torch.Tensor) – Indices of vertices within each triangle. (Optional)
  • config_dict – Dictionary with ThreeJS classes names and configuration.
  • global_step (int) – Global step value to record
  • walltime (float) – Optional override default walltime (time.time()) seconds after epoch of event

Shape:
vertices: (B,N,3). (batch, number_of_vertices, channels)
colors: (B,N,3). The values should lie in [0, 255] for type uint8 or [0, 1] for type float.
faces: (B,N,3). The values should lie in [0, number_of_vertices] for type uint8.

Examples:

from torch.utils.tensorboard import SummaryWriter
vertices_tensor = torch.as_tensor([
    [1, 1, 1],
    [-1, -1, 1],
    [1, -1, -1],
    [-1, 1, -1],
], dtype=torch.float).unsqueeze(0)
colors_tensor = torch.as_tensor([
    [255, 0, 0],
    [0, 255, 0],
    [0, 0, 255],
    [255, 0, 255],
], dtype=torch.int).unsqueeze(0)
faces_tensor = torch.as_tensor([
    [0, 2, 3],
    [0, 3, 1],
    [0, 1, 2],
    [1, 3, 2],
], dtype=torch.int).unsqueeze(0)

writer = SummaryWriter()
writer.add_mesh('my_mesh', vertices=vertices_tensor, colors=colors_tensor, faces=faces_tensor)

writer.close()

add_hparams

add_hparams(hparam_dict, metric_dict, hparam_domain_discrete=None, run_name=None)

Add a set of hyperparameters to be compared in TensorBoard.

Parameters

  • hparam_dict (dict) – Each key-value pair in the dictionary is the name of the hyper parameter and it’s corresponding value. The type of the value can be one of bool, string, float, int, or None.
  • metric_dict (dict) – Each key-value pair in the dictionary is the name of the metric and it’s corresponding value. Note that the key used here should be unique in the tensorboard record. Otherwise the value you added by add_scalar will be displayed in hparam plugin. In most cases, this is unwanted.
  • hparam_domain_discrete – (Optional[Dict[str, List[Any]]]) A dictionary that contains names of the hyperparameters and all discrete values they can hold
  • run_name (str) – Name of the run, to be included as part of the logdir. If unspecified, will use current timestamp.
    Examples:
from torch.utils.tensorboard import SummaryWriter
with SummaryWriter() as w:
    for i in range(5):
        w.add_hparams({'lr': 0.1*i, 'bsize': i},
                      {'hparam/accuracy': 10*i, 'hparam/loss': 10*i})

在这里插入图片描述

flush()

close()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值