Pytorch学习-网络可视化
建立一个神经网络
导包,虽然现在还不知道所有包的具体用途,但跟着书来就完事了
import torch
import torch.nn as nn
import torchvision
import torchvision.utils as vutils
import torch.utils.data as Data
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
import hiddenlayer as hl
from torchviz import make_dot
from tensorboardX import SummaryWriter
import os
import time
建立一个卷积神经网络
class ConvNet(nn.Module):
def __init__(self):
super(ConvNet, self).__init__()
# 第一层卷积
# 图片尺寸 f=28
self.conv1 = nn.Sequential(
nn.Conv2d(
in_channels=1,
out_channels=16,
kernel_size=3,
stride=1,
padding=1,
),
nn.ReLU(),
# 卷积之后,图片的尺寸 f=(28+2*1-3)/1 + 1 = 28
nn.AvgPool2d(
kernel_size=2,
stride=2,
)
# 经过平均池化后,图片的尺寸 f=(28-2)/2 + 1 = 14
)
# 第二层卷积
self.conv2 = nn.Sequential(
nn.Conv2d(16, 32, 3, 1, 1),
# 卷积之后,图片的尺寸 f=(14+2*1-3)/1 + 1 = 14
nn.ReLU(),
nn.AvgPool2d(2, 2)
# 经过平均池化后,图片的尺寸 f=(14-2)/2 + 1 = 7
)
# 全连接层
self.fc = nn.Sequential(
nn.Linear(
in_features=32*7*7,
out_features=128
),
nn.ReLU(),
nn.Linear(128, 64),
nn.ReLU()
)
self.out = nn.Linear(64, 10)
# 定义网络的前向传播路径
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
output = self.out(x)
return output
在草稿纸上试着画了一下这个网络(不带偏置项)
其实在想全连接层之后为什么没有softmax,记得看吴恩达老师的课里说,全连接层后面是要加一个softmax来获得一个概率最大的输出值的。
可视化网络
# # 可视化卷积神经网络
# hl_graph = hl.build_graph(MyConvNet, torch.zeros([1, 1, 28, 28]).to(device))
# hl_graph.theme = hl.graph.THEMES["blue"].copy()
# 将可视化的网络保存为图片
# hl_graph.save("data/chap4/MyConvNet_h1.png", format="png")
这里是用hiddenlayer库得到的网络可视化图象。
# x = torch.randn(1, 1, 28, 28).requires_grad_(True)
# y = MyConvNet(x)
# MyConvNetVis = make_dot(y, params=dict(list(MyConvNet.named_parameters()) + [('x', x)]))
# MyConvNetVis.format = "png"
# MyConvNetVis.directory = "data/chap4/MyConvNetVis.png"
# MyConvNetVis.view()
这里是用torchviz得到的网络可视化
个人感觉用torchviz得到的网络更详细,因为他标出了通道数和偏置项。