搭建小实战和Sequential的使用
模型搭建
以CIFAR 10 model结构为例搭建网络
CIFAR 10 model结构
torch.nn.Flatten
是PyTorch中的一个模块,用于将多维的输入张量转换为一维的输出张量。它可以被用作神经网络模型中的一层,用于将输入张量展平后作为全连接层的输入。比如输入张量的形状是 [10, 3, 32, 32]
,即批大小为 10,通道数为 3,高度和宽度分别为 32。经过 nn.Flatten
模块处理后,输出张量的形状变为 [10, 3072]
,即批大小为 10,一维展开后的长度为 3072(3 * 32 * 32)。
卷积层中的stride
和padding
是由卷积的输入输出计算公式得到(dilation默认为1)
import torch
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
class Baseseq(nn.Module):
def __init__(self):
super(Baseseq, self).__init__()
# 根据卷积的输入输出公式计算padding和stride
self.conv1 = Conv2d(in_channels=3, out_channels=32, kernel_size=5, stride=1, padding=2)
self.maxpool1 = MaxPool2d(kernel_size=2)
self.conv2 = Conv2d(in_channels=32, out_channels=32, kernel_size=5, stride=1, padding=2)
self.maxpool2 = MaxPool2d(kernel_size=2)
self.conv3 = Conv2d(in_channels=32, out_channels=64, kernel_size=5, padding=2)
self.maxpool3 = MaxPool2d(kernel_size=2)
# 与torch.flatten的作用相同
self.flatten = Flatten()
# 展平后为64*4*4=1024,当不知道时可以先返回flatten之后的结果查看展平大小
self.linear1 = Linear(1024, 64)
self.linear2 = Linear(64, 10)
def forward(self, x):
x = self.conv1(x)
x = self.maxpool1(x)
x = self.conv2(x)
x = self.maxpool2(x)
x = self.conv3(x)
x = self.maxpool3(x)
x = self.flatten(x)
x = self.linear1(x)
x = self.linear2(x)
return x
baseseq = Baseseq()
print(baseseq)
# 对搭建的网络进行验证
input = torch.ones((64, 3, 32, 32))
output = baseseq(input)
print(output.shape)
Sequential
Sequential是一个时序容器。Modules 会以他们传入的顺序被添加到容器中。包含在PyTorch官网中torch.nn模块中的Containers中,在神经网络搭建的过程中如果使用Sequential,代码更简洁。使用Sequential
搭建上述模型,并借助Tensorboard
显示计算图的详细信息。
import torch
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
from torch.utils.tensorboard import SummaryWriter
class Baseseq(nn.Module):
def __init__(self):
super(Baseseq, self).__init__()
self.model1 = Sequential(
Conv2d(in_channels=3, out_channels=32, kernel_size=5, stride=1, padding=2),
MaxPool2d(kernel_size=2),
Conv2d(32, 32, 5, padding=2),
MaxPool2d(2),
Conv2d(32, 64, 5, padding=2),
MaxPool2d(2),
Flatten(),
Linear(1024, 64),
Linear(64, 10)
)
def forward(self, x):
x = self.model1(x)
return x
baseseq = Baseseq()
print(baseseq)
# 对搭建的网络进行验证
input = torch.ones((64, 3, 32, 32))
output = baseseq(input)
print(output.shape)
writer = SummaryWriter("logs")
writer.add_graph(baseseq, input)
writer.close()
得到的结果和上面的结果相同,同时还为每一步添加了序号
TensorBoard得到的计算图:
对于每一个部分双击可以看到详细的信息:
标签:实战,nn,32,self,torch,Sequential,64,搭建 From: https://www.cnblogs.com/yq-ydky/p/17632474.html