前面用的网络是pytorch官方给的一个实例网络,本文参照书本换了一个网络,如下:
代码如下:
class CNNnet(nn.Module):
def __init__(self):
super(CNNnet,self).__init__()
self.conv1 = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=5,stride=1)
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv2 = nn.Conv2d(in_channels=16, out_channels=36,kernel_size=3,stride=1)
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.fc1 = nn.Linear(1296, 128) # 1296 = 36 * 6 *6
self.fc2 = 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, 36*6*6)
x = F.relu(self.fc2(F.relu(self.fc1(x))))
return x
其中36*6*6怎么计算来的,c*H*W,H和W都是用如下链接给的计算方式得到的:
【pytorch】卷积层输出尺寸的计算公式和分组卷积的weight尺寸的计算https://mp.youkuaiyun.com/console/editor/html/107954603
结果如何呢?
当用了如下显示初始化方式后,结果为:
for m in net.modules():
if isinstance(m, nn.Conv2d):
nn.init.normal_(m.weight)
nn.init.xavier_normal_(m.weight)
nn.init.kaiming_normal_(m.weight) # 卷积层初始化
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
nn.init.normal_(m.weight) # 全连接层参数初始化
可以看出,其好像陷入了鞍点。其损失没有下降了,那我还是把这个显式初始化参数去掉试一下。
还真有效果,终于loss有值了,但是基本稳定在2.多,2个epoch时:
Accuracy of the network on the 10000 test images: 9 %
Accuracy of plane : 0 %
Accuracy of car : 89 %
Accuracy of bird : 0 %
Accuracy of cat : 0 %
Accuracy of deer : 0 %
Accuracy of dog : 0 %
Accuracy of frog : 4 %
Accuracy of horse : 0 %
Accuracy of ship : 0 %
Accurac