# Pytorch Practice - LeNet for cifar10 ```python import torch.nn as nn import torch.nn.functional as F ``` ```python class LeNet(nn.Module): def __init__(self): super(LeNet, self).__init__() # input channels: 输入特征矩阵的深度, 例如RGB图像的in_channels = 3 # output channels: 使用的卷积核的个数 = 输出特征矩阵的深度 # kernel size: 卷积核的大小 self.conv1 = nn.Conv2d(3, 16, 5) # 特征矩阵的长宽都缩小一半 self.pool1 = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(16, 32, 5) self.pool2 = nn.MaxPool2d(2, 2)pshb self.fc1 = nn.Linear(32*5*5, 120) # 因为conv2卷积后特征矩阵的形状为(32,5,5) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 10) def forward(self, x): x = F.relu(self.conv1(x)) # input(3, 32, 32) output(16, 28, 28) x = self.pool1(x) # output(16, 14, 14) x = F.relu(self.conv2(x)) # output(32, 10, 10) x = self.pool2(x) # output(32, 5, 5) x = x.view(-1, 32*5*5) # output(32*5*5) x = F.relu(self.fc1(x)) # output(120) x = F.relu(self.fc2(x)) # output(84) x = self.fc3(x) # output(10) return x ``` **经卷积后的矩阵尺寸大小为** $$ N=(W-F+2 P) / S+1 $$ - 输入图片大小 W x W - Filter/kernel大小 F x F - 步长 S - padding的像素数P ```python ```