LeNet

LeCun 1998年 <<Gradient-Based Learning Applied to Document Recognition>>
image
image
加上下采样共7层

class LeNet(nn.Module):
    def __init__(self):
        super(LeNet, self).__init__()
        self.conv1 = nn.Sequential(    
            nn.Conv2d(1, 6, 5), 
            nn.ReLU(inplace=True),      
            nn.MaxPool2d(kernel_size=2, stride=2),
        )
        self.conv2 = nn.Sequential(
            nn.Conv2d(6, 16, 5),
            nn.ReLU(inplace=True),      
            nn.MaxPool2d(2, 2)  
        )
        self.fc1 = nn.Sequential(
            nn.Linear(16 * 5 * 5, 120),
            nn.ReLU(inplace=True)
        )
        self.fc2 = nn.Sequential(
            nn.Linear(120, 84),
            nn.ReLU(inplace=True)
        )
        self.fc3 = nn.Linear(84, 10)

    # 定义前向传播过程,输入为x
    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        # nn.Linear()的输入输出都是维度为1的值,所以要把多维度的tensor展平成1维
        x = x.view(x.size()[0], -1)
        x = self.fc1(x)
        x = self.fc2(x)
        x = self.fc3(x)
        return x

inplace为True,将会改变输入的数据,否则不会改变原输入,只会产生新的输出
posted @ 2021-06-01 20:21  梦梦wm  阅读(93)  评论(0编辑  收藏  举报