LeNet
LeNet网络在进行少数据集字符识别的时候有着很好的效果。
共六层网络结构:
输入层 - 卷积层 - 池化层 - 卷积层 - 池化层 - 输出层
网络框架代码
class LeNet(nn.Module):
def __init__(self):
super(LeNet, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(1, 6, 5), # in_channels, out_channels, kernel_size
nn.Sigmoid(), # 激活函数
nn.MaxPool2d(2, 2), # kernel_size, stride
nn.Conv2d(6, 16, 5), # 5*5卷积 6->16通道
nn.Sigmoid(),
nn.MaxPool2d(2, 2) # 最大池化
)
self.fc = nn.Sequential(
nn.Linear(16*4*4, 120), # 全连接1.120个神经元
nn.Sigmoid(),
nn.Linear(120, 84), # 全连接2,84个神经元
nn.Sigmoid(),
nn.Linear(84, 10) # 全连接3,输出层,10分类
)
def forward(self, img):
feature = self.conv(img) # 前向传播得出特征图
output = self.fc(feature.view(img.shape[0], -1)) # 全连接
return output
在全连接中,feature.view(img.shape[0], -1)
涉及.view
和.shape
用法。
下一篇博客中会说明。