CIFAR 10 模型结构:
通过图片可以知道,输入为3通道的32*32大小数据,第一步卷积,通过5*5的一个卷积核,得到32通道的32*32的大小,接着通过2*2的最大池化层得到32通道的16*16...得到64通道的4*4数据,经过flatten展开为1024个一行的数据,通过线性层得到隐藏层的64个数据,再经过一个线性层得到输出。
第一层的卷积层,可以知道参数in_channels为3,out_channels为32,kernel_size为5,而大小32*32没有变化,则由官方文档的公式:
带入数据得到:padding=2,stride=1。
编写网络:
from torch import nn
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.conv1 = nn.Conv2d(3, 32, 5, 1, 2)
self.maxpool1 = nn.MaxPool2d(2)
self.conv2 = nn.Conv2d(32, 32, 5, padding=2)
self.maxpool2 = nn.MaxPool2d(2)
self.conv3 = nn.Conv2d(32, 64, 5, padding=2)
self.maxpool3 = nn.MaxPool2d(2)
self.flatten = nn.Flatten()
self.linear1 = nn.Linear(1024, 64)
self.linear2 = nn.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
创建网络后需要验证网络的正确性,创建一个假象的输入:
model = Model()
input = torch.ones((64, 3, 32, 32))#根据卷积层输入参数决定
print(model(input).shape)
输出为:
torch.Size([64, 10])
我们可以使用Sequential函数可以使得代码更简洁:
from torch import nn
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.model = nn.Sequential(
nn.Conv2d(3, 32, 5, 1, 2),
nn.MaxPool2d(2),
nn.Conv2d(32, 32, 5, padding=2),
nn.MaxPool2d(2),
nn.Conv2d(32, 64, 5, padding=2),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(1024, 64),
nn.Linear(64, 10),
)
def forward(self, x):
x = self.model(x)
return x
model = Model()
print(model)
输出为:
Model(
(model): Sequential(
(0): Conv2d(3, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(2): Conv2d(32, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(4): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(5): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(6): Flatten(start_dim=1, end_dim=-1)
(7): Linear(in_features=1024, out_features=64, bias=True)
(8): Linear(in_features=64, out_features=10, bias=True)
)
)
我们也可以使用Tensorboard进行结构的可视化:
from torch.utils.tensorboard import SummaryWriter
model = Model()
input = torch.ones([64, 3, 32, 32])
writer = SummaryWriter("./logs")#可视化模型计算图
writer.add_graph(model, input)
writer.close()
打开Tensorboard:
双击打开可以看见结构,箭头上也有数据的大小: