tensorboard打印pytorch网络结构测试

打印环境:pytorch1.4.0,CUDA10.0,tensorflow1.15.0,tensorboardX 2.1

1、安装tensorboardX

pip install tensorboardX

2、测试代码:

代码来源:https://blog.csdn.net/xiaoxifei/article/details/82735355

import sys

import torch
import torch.nn as nn
from tensorboardX import SummaryWriter


# 设计的一个网络结构
class LeNet(nn.Module):
    def __init__(self):
        super(LeNet, self).__init__()
        self.conv1 = nn.Sequential(     #input_size=(1*28*28)
            nn.Conv2d(1, 6, 5, 1, 2),
            nn.ReLU(),      #(6*28*28)
            nn.MaxPool2d(kernel_size=2, stride=2),  #output_size=(6*14*14)
        )
        self.conv2 = nn.Sequential(
            nn.Conv2d(6, 16, 5),
            nn.ReLU(),      #(16*10*10)
            nn.MaxPool2d(2, 2)  #output_size=(16*5*5)
        )
        self.fc1 = nn.Sequential(
            nn.Linear(16 * 5 * 5, 120),
            nn.ReLU()
        )
        self.fc2 = nn.Sequential(
            nn.Linear(120, 84),
            nn.ReLU()
        )
        self.fc3 = nn.Linear(84, 10)

    # 定义前向传播过程,输入为x
    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        # nn.Linear()的输入输出都是维度为一的值,所以要把多维度的tensor展平成一维
        x = x.view(x.size()[0], -1)
        x = self.fc1(x)
        x = self.fc2(x)
        x = self.fc3(x)
        return x

#实例化一个网络模型
model = LeNet()

#假设输入13张1*28*28的图片
dummy_input = torch.rand(13, 1, 28, 28)


with SummaryWriter(comment='LeNet') as w:
    w.add_graph(model, (dummy_input, ))

    # 写一个新的数值序列到logs内的文件里,比如sin正弦波。
    for i in range(100):
        x = torch.tensor(i / 10, dtype=torch.float)
        y = torch.sin(x)
        # 写入数据的标注指定为 data/sin, 写入数据是y, 当前已迭代的步数是i。
        w.add_scalar('data/sin', y, i)

运行测试代码会生成一个runs文件夹

tensorboard打印pytorch网络结构测试_第1张图片 

3、查看网络结构

终端运行

tensorboard --logdir E:\Codes\PyCharm\test\runs\Jan27_11-39-13_DESKTOP-UMSTLGMLeNet --host 127.0.0.1 --port 80

 tensorboard打印pytorch网络结构测试_第2张图片

你可能感兴趣的:(环境配置,python,pytorch,人工智能,深度学习)