(1)导入tensorboard,要先实例化SummaryWriter类,指明记录日志的路径信息:
from torch.utils.tensorboard import SummaryWriter
(2)先实例化SummaryWriter类,指明记录日志的路径信息:
writer = SummaryWriter(log_dir='路径地址')
(3)调用相应的API接口,一般格式:
add_xxx(标签名,对象,迭代次数)
(4)启动tensorboard服务:
在anaconda prompt窗口中指令:
tensorboard --logdir = '路径地址' --port 6006
(5)将地址输入浏览器打开即可,(点击刷新)
构建网络:
class Net(nn.Module):
def __init__(self):
super(Net,self).__init__()
self.conv1 = nn.Conv2d(1,10,kernel_size=5)
self.conv2 = nn.Conv2d(10,20,kernel_size=5)
self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(320,50)
self.fc2 = nn.Linear(50,10)
self.bn = nn.BatchNorm2d(20)
def forward(self,x):
x = F.max_pool2d(self.conv1(x),2)
x = F.relu(x)+F.relu(-x)
x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)),2))
x = self.bn(x)
x = x.view(-1,320)
x = F.relu(self.fc1(x))
x = F.dropout(x,training=self.training)
x = self.fc2(x)
x = F.softmax(x,dim=1)
return x
writer = SummaryWriter(log_dir=r'E:\Notebook\TensorboardX可视化处理\logs')
input = torch.rand(32,1,28,28)
model = Net()
writer.add_graph(model,input) #展示模型图
总结来说:作为一名初学者,个人对tensorboard的使用较少,主要就是对模型add_graph()的展示和对损失值等图像变化的展示add_scalar(),少数时候会用到图形展示add_image(),其余的API几乎没有使用过。