根据下面代码用python编程完成:以LeNet为基础实现如下几种改进。6与7为扩展任务并给出最终代码。1激活函数的改进:将LeNet中的激活函数替换为ReLU。2池化方式:平均池化改为最大池化。3卷积核大小:将其中一个55的卷积核修改为774正则化方法1:在全连接层后加入Dropout层中间的全连接层可增加维度5正则化方法2:卷积层后加入BatchNorm层6将卷积核从55修改为33但增加网络的
改进1:使用ReLU激活函数
将LeNet中的激活函数替换为ReLU。
class LeNet(nn.Module): def init(self): super(LeNet, self).init() self.conv1 = nn.Conv2d(1, 6, 5) self.pool1 = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(6, 16, 5) self.pool2 = nn.MaxPool2d(2, 2) self.fc1 = nn.Linear(16 * 4 * 4, 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.pool1(F.relu(self.conv1(x)))
x = self.pool2(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 4 * 4)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
改进2:使用最大池化
将LeNet中的平均池化改为最大池化。
class LeNet(nn.Module): def init(self): super(LeNet, self).init() self.conv1 = nn.Conv2d(1, 6, 5) self.pool1 = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(6, 16, 5) self.pool2 = nn.MaxPool2d(2, 2) self.fc1 = nn.Linear(16 * 4 * 4, 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.pool1(F.relu(self.conv1(x)))
x = self.pool2(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 4 * 4)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
改进3:使用7*7的卷积核
将LeNet中的一个55的卷积核修改为77。
class LeNet(nn.Module): def init(self): super(LeNet, self).init() self.conv1 = nn.Conv2d(1, 6, 5) self.pool1 = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(6, 16, 7) # 修改卷积核大小为7*7 self.pool2 = nn.MaxPool2d(2, 2) self.fc1 = nn.Linear(16 * 4 * 4, 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.pool1(F.relu(self.conv1(x)))
x = self.pool2(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 4 * 4)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
改进4:使用Dropout层进行正则化
在LeNet中的全连接层后加入Dropout层,中间的全连接层可增加维度。
class LeNet(nn.Module): def init(self): super(LeNet, self).init() self.conv1 = nn.Conv2d(1, 6, 5) self.pool1 = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(6, 16, 5) self.pool2 = nn.MaxPool2d(2, 2) self.fc1 = nn.Linear(16 * 4 * 4, 256) # 增加维度 self.dropout = nn.Dropout(0.5) # 加入Dropout层,丢弃概率为0.5 self.fc2 = nn.Linear(256, 128) # 增加维度 self.fc3 = nn.Linear(128, 10)
def forward(self, x):
x = self.pool1(F.relu(self.conv1(x)))
x = self.pool2(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 4 * 4)
x = F.relu(self.fc1(x))
x = self.dropout(x) # Dropout层
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
改进5:使用BatchNorm层进行正则化
在LeNet中的卷积层后加入BatchNorm层。
class LeNet(nn.Module): def init(self): super(LeNet, self).init() self.conv1 = nn.Conv2d(1, 6, 5) self.bn1 = nn.BatchNorm2d(6) # 加入BatchNorm层 self.pool1 = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(6, 16, 5) self.bn2 = nn.BatchNorm2d(16) # 加入BatchNorm层 self.pool2 = nn.MaxPool2d(2, 2) self.fc1 = nn.Linear(16 * 4 * 4, 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.pool1(F.relu(self.bn1(self.conv1(x)))) # BatchNorm层
x = self.pool2(F.relu(self.bn2(self.conv2(x)))) # BatchNorm层
x = x.view(-1, 16 * 4 * 4)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
改进6:使用3*3的卷积核并增加网络层数
将LeNet中的卷积核大小从55修改为33,并增加网络层数。
class LeNet(nn.Module): def init(self): super(LeNet, self).init() self.conv1 = nn.Conv2d(1, 6, 3) # 修改卷积核大小为33 self.bn1 = nn.BatchNorm2d(6) self.conv2 = nn.Conv2d(6, 16, 3) # 修改卷积核大小为33 self.bn2 = nn.BatchNorm2d(16) self.conv3 = nn.Conv2d(16, 32, 3) # 增加网络层数 self.bn3 = nn.BatchNorm2d(32) self.conv4 = nn.Conv2d(32, 64, 3) # 增加网络层数 self.bn4 = nn.BatchNorm2d(64) self.pool = nn.MaxPool2d(2, 2) self.fc1 = nn.Linear(64 * 2 * 2, 256) self.dropout = nn.Dropout(0.5) self.fc2 = nn.Linear(256, 128) self.fc3 = nn.Linear(128, 10)
def forward(self, x):
x = self.pool(F.relu(self.bn1(self.conv1(x))))
x = self.pool(F.relu(self.bn2(self.conv2(x))))
x = F.relu(self.bn3(self.conv3(x)))
x = F.relu(self.bn4(self.conv4(x)))
x = x.view(-1, 64 * 2 * 2)
x = F.relu(self.fc1(x))
x = self.dropout(x)
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
改进7:加入残差连接
在LeNet中选择一条跨层的路径(跨一层或跨多层均可),加入残差连接。注意需要用1*1卷积使维度相匹配。
class LeNet(nn.Module): def init(self): super(LeNet, self).init() self.conv1 = nn.Conv2d(1, 6, 3) self.bn1 = nn.BatchNorm2d(6) self.conv2 = nn.Conv2d(6, 16, 3) self.bn2 = nn.BatchNorm2d(16) self.conv3 = nn.Conv2d(16, 32, 3) self.bn3 = nn.BatchNorm2d(32) self.conv4 = nn.Conv2d(32, 64, 3) self.bn4 = nn.BatchNorm2d(64) self.pool = nn.MaxPool2d(2, 2) self.fc1 = nn.Linear(64 * 2 * 2, 256) self.dropout = nn.Dropout(0.5) self.fc2 = nn.Linear(256, 128) self.fc3 = nn.Linear(128, 10) # 加入残差连接 self.residual_conv = nn.Conv2d(16, 64, 1) self.residual_bn = nn.BatchNorm2d(64)
def forward(self, x):
residual = x
x = self.pool(F.relu(self.bn1(self.conv1(x))))
x = self.pool(F.relu(self.bn2(self.conv2(x))))
residual = self.residual_bn(self.residual_conv(x) + residual) # 残差连接
x = F.relu(self.bn3(self.conv3(x)))
x = F.relu(self.bn4(self.conv4(x)))
x = x.view(-1, 64 * 2 * 2)
x = F.relu(self.fc1(x))
x = self.dropout(x)
x = F.relu(self.fc2(x))
x = self.fc3(x)
return
原文地址: https://www.cveoy.top/t/topic/ffi3 著作权归作者所有。请勿转载和采集!