这个函数用于创建一个tensorboard文件,其中常用参数有
add_scalar(tag, scalar_value, global_step=None,walltime=None)
tag ( string ) – 数据标识符
scalar_value ( float或string/blobname ) – 要保存的值:相当于是x的值
global_step ( int ) – 要记录的全局步长值:相当于是y的值
walltime ( float ) – 记录训练的时间,默认 walltime (time.time()) 秒
new_style ( boolean ) – 是使用新样式(张量字段)还是旧样式(simple_value 字段)。新样式可能会导致更快的数据加载。
writer = SummaryWriter(comment='_scalars', filename_suffix="12345678")
for x in range(100):
writer.add_scalar('y=pow_2_x', 2 ** x, x)
writer.close()
add_scalars(main_tag, tag_scalar_dict,global_step=None, walltime=None)
main_tag ( string ) – 标签的父名称
tag_scalar_dict ( dict ) – 存储标签和对应值的键值对:相当于y的值。
global_step ( int ) – 要记录的全局步长值:相当于x的值
walltime ( float ) – 记录训练的时间,默认 walltime (time.time()) 秒
max_epoch = 100
writer = SummaryWriter(comment='test_comment', filename_suffix="test_suffix")
for x in range(max_epoch):
writer.add_scalar('y=2x', x * 2, x)
writer.add_scalar('y=pow_2_x', 2 ** x, x)
writer.add_scalars('data/scalar_group', {"xsinx": x * np.sin(x),
"xcosx": x * np.cos(x)}, x)
writer.close()
add_histogram(tag, values, global_step=None,bins='tensorflow', walltime=None)
• tag:图像的标签名,图的唯一标识
• values:要统计的参数
• global_step:y轴
• bins:取直方图的bins
writer = SummaryWriter("histogram_logs")
for i in range(10):
x = np.random.random(1000)
writer.add_histogram('distribution centers', x + i, i)
writer.close()
add_image(tag, img_tensor, global_step=None,walltime=None, dataformats='CHW')
• tag:图像的标签名,图的唯一标识
• img_tensor:图像数据,注意尺度
• global_step:x轴
• dataformats:数据形式,CHW,HWC,HW
writer = SummaryWriter(comment='test_your_comment', filename_suffix="_test_your_filename_suffix")
fake_img = torch.randn(3, 512, 512)
writer.add_image("fake_img", fake_img, 1)
make_grid(tensor, nrow=8, padding=2,normalize=False, range=None, scale_each=False,pad_value=0)
• tensor:图像数据, B*C*H*W形式
• nrow:行数(列数自动计算)
• padding:图像间距(像素单位)
• normalize:是否将像素值标准化
• range:标准化范围
• scale_each:是否单张图维度标准化
• pad_value:padding的像素值
writer = SummaryWriter(comment='test_your_comment', filename_suffix="_test_your_filename_suffix")
split_dir = os.path.join("..", "..", "data", "rmb_split")
train_dir = os.path.join(split_dir, "train")
transform_compose = transforms.Compose([transforms.Resize((32, 64)), transforms.ToTensor()])
train_data = RMBDataset(data_dir=train_dir, transform=transform_compose)
train_loader = DataLoader(dataset=train_data, batch_size=16, shuffle=True)
data_batch, label_batch = next(iter(train_loader))
img_grid = vutils.make_grid(data_batch, nrow=4, normalize=True, scale_each=True)
# img_grid = vutils.make_grid(data_batch, nrow=4, normalize=False, scale_each=False)
writer.add_image("input img", img_grid, 0)
add_graph(model, input_to_model=None, verbose=False)
• model:模型,必须是 nn.Module
• input_to_model:输出给模型的数据
• verbose:是否打印计算图结构信息
model( torch.nn.Module ) – 要绘制的模型。
input_to_model ( torch.Tensor or list of torch.Tensor ) – 要输入的变量或变量元组
verbose(bool)– 是否在控制台中打印图形结构。
use_strict_trace ( bool ) – 是否将关键字参数严格传递给 torch.jit.trace。当您希望跟踪器记录您的可变容器类型(列表、字典)时传递 False.
writer = SummaryWriter(comment='test_your_comment', filename_suffix="_test_your_filename_suffix")
# 模型
fake_img = torch.randn(1, 3, 32, 32)
lenet = LeNet(classes=2)
writer.add_graph(lenet, fake_img)
summary(model, input_size, batch_size=-1,device="cuda")
• model:pytorch模型
• input_size:模型输入size
• batch_size:batch size
• device:“cuda” or “cpu”
summary(lenet, (3, 32, 32), device="cpu")
参考链接