【Pytorch】pytorch模型可视化

step1:转载来自:https://blog.csdn.net/TTdreamloong/article/details/83107110

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

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

dummy_input = Variable(torch.rand(1, 3, 224, 224)) #假设输入13张1*28*28的图片
#model = LeNet()
with SummaryWriter(comment='resnet34') as w:
    w.add_graph(model,input_to_model=dummy_input)

step2:上一步会在工程目录下生成runs文件夹,接着执行以下代码:

tensorboard --logdir runs

step3:然后用浏览器打开上一步骤生成的网址http://Zhen:6006

你可能感兴趣的:(【Pytorch】)