import torch.nn as nn # 神经网络的层的实现:卷积层 import torch.nn.functional as fu class LeNet(nn.Module): def __init__(self, cls_num=10): super(LeNet, self).__init__() self.conv1 = nn.Conv2d(1, 6, 5, padding=2) self.conv2 = nn.Conv2d(6, 16, 5) self.fc1 = nn.Linear(16 * 5 * 5, 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, cls_num) def forward(self, x): y = fu.max_pool2d(fu.relu(self.conv1(x)), (2, 2)) y = fu.max_pool2d(fu.relu(self.conv2(y)), (2, 2)) # 格式转换 y = y.view(y.size()[0], -1) y = fu.relu(self.fc1(y)) y = fu.relu(self.fc2(y)) y = self.fc3(y) return y