pytorch 实现 lenet5 并用于 mnist 分类需要注意哪些问题?

首先,结合下面这个图,使用 pytorch 实现 letnet5 模型。
在这里插入图片描述计算每一层的 channelkernel_sizestride 数值,得到如下所示:

卷积层in_channelout_channelkernel_sizestridepadding
Conv16512
MaxPool6622
Conv61651
MaxPool161622
Conv1612051
全连接层输入输出bias
fc112084True
fc28410True

然后开始编码,下面是需要注意的点,因为仅仅从上述模型图中看不出来这些 trick,参考 vgg.py

1、每一个卷积层 Conv 后面都需要跟一个激活函数,默认使用ReLu

output = self.conv1(input)
output = self.relu(output)

2、第一个全连接层 Linear 后面,需要跟上一个激活函数,默认使用 ReLu

output = self.fc1(output)
output = self.relu(output)

3、最后一个全连接层 Linear 后面,不能跟上激活函数 ReLu,因为 ReLu 会将小于0 的数重置为 0。可以跟上 softmax 激活函数,将输出限制为 [0,1] 的概率值。也可以不添加 softmax 激活函数,此时经过交叉熵公式后变为 l o s s = y l n y ^ = y l n max ⁡ ( y ) loss = y\mathrm{ln}\hat{y} = y\mathrm{ln}\max{(y)} loss=ylny^=ylnmax(y)。参考 vgg.py

4、初始化参数

for m in self.modules():
	if isinstance(m, nn.Conv2d):
	    nn.init.kaiming_normal_(m.weight)
	elif isinstance(m, nn.Linear):
	    nn.init.normal_(m.weight, 0, 0.01)
	    nn.init.constant_(m.bias, 0)

主要是初始化 卷积层全连接层 中的权重参数

经过以上配置后,经过以下步骤训练,可以很快得收敛。

net = Lenet()
optimizer = torch.optim.SGD(net.parameters(), 0.01)
criterion = nn.CrossEntropyLoss()

for epoch in range(0, 10):
    
    epoch_loss = 0.0
    
    for i, (image, target) in enumerate(zip(train_images, train_targets)):
        
        optimizer.zero_grad()
        preds = net(image)
        loss = criterion(preds, target)
        epoch_loss += loss.item()
        
        loss.backward()
        optimizer.step()
        
    print("epoch - {}, loss - {:.4f}".format(epoch, epoch_loss / iter_count / batch_size))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值