完整代码见我的github
pytorch handbook
官方介绍tensorboard官方turtorial

显示图片

cat_img = Image.open('cat.jpg')
transform = transforms.Compose([
    transforms.Resize(224),
    transforms.CenterCrop(224),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor()
])
cat_img_224 = transform(cat_img)

writer = SummaryWriter(log_dir='logs/1', comment='cat_image')
writer.add_image('cat', cat_img_224)
writer.close()

显示标量

x = torch.FloatTensor([1])
y = torch.FloatTensor([1])

for epoch in range(30):
    new_x = x + epoch
    new_y = y + 2*epoch
    loss = new_y - new_x
    with SummaryWriter(log_dir='./logs/2', comment='train') as writer: #可以直接使用python的with语法,自动调用close方法
        # writer.add_histogram('his/x', x, epoch)
        # writer.add_histogram('his/y', y, epoch)
        writer.add_scalar('x', new_x, epoch)
        writer.add_scalar('y', new_y, epoch)
        writer.add_scalar('loss', loss, epoch)
        # writer.add_scalars('data/data_group', {'x': x,
                                                # 'y': y}, epoch)
x = torch.FloatTensor([5])
y = torch.FloatTensor([5])

for epoch in range(30):
    new_x = x + epoch
    new_y = y + 2*epoch
    loss = new_y - new_x
    with SummaryWriter(log_dir='./logs/3', comment='train') as writer: #可以直接使用python的with语法,自动调用close方法
        # writer.add_histogram('his/x', x, epoch)
        # writer.add_histogram('his/y', y, epoch)
        writer.add_scalar('x', new_x, epoch)
        writer.add_scalar('y', new_y, epoch)
        writer.add_scalar('loss', loss, epoch)
        # writer.add_scalars('data/data_group', {'x': x,
                                                # 'y': y}, epoch)