0
0
0
1. 云栖社区>
2. 博客>
3. 正文

## 利用Pytorch进行CNN详细剖析

1. class Net(nn.Module):
2.     def __init__(self):
3.         super(Net, self).__init__()
4.         self.conv1 = nn.Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1), padding=0)
5.         self.conv2 = nn.Conv2d(20, 50, kernel_size=(5, 5), stride=(1, 1), padding=0)
6.         self.fc1 = nn.Linear(4*4*50, 500)
7.         self.fc2 = nn.Linear(500, 10)
8.
9.     def forward(self, x):
10.         x = F.max_pool2d(self.conv1(x), 2)
11.         x = F.max_pool2d(self.conv2(x), 2)
12.         x = x.view(-1, 4*4*50)
13.         x = F.relu(self.fc1(x))
14.         x = self.fc2(x)
15.         return F.log_softmax(x)

2.     dt = numpy.dtype(numpy.uint32).newbyteorder('>')    # 大端模式读取，最高字节在前(MSB first

1. Test set: Average loss: 0.0014, Accuracy: 9732/10000 (97.3%)

1. class Net(nn.Module):
2.     def __init__(self):
3.         super(Net, self).__init__()
4.         self.conv1 = nn.Conv2d(1, 20, kernel_size=(5, 5), stride=(1, 1), padding=0)
5.         self.conv2 = nn.Conv2d(20, 50, kernel_size=(5, 5), stride=(1, 1), padding=0)
6.         self.fc1 = nn.Linear(4*4*50, 500)
7.         self.fc2 = nn.Linear(500, 10)
8.         self.bn1 = nn.BatchNorm2d(20)
9.         self.bn2 = nn.BatchNorm2d(50)
10.         self.bn3 = nn.BatchNorm1d(500)
11.
12.     def forward(self, x):
13.         x = self.conv1(x)
14.         x = F.max_pool2d(self.bn1(x), 2)
15.         x = self.conv2(x)
16.         x = F.max_pool2d(self.bn2(x), 2)
17.         x = x.view(-1, 4*4*50)
18.         x = self.fc1(x)
19.         x = F.relu(self.bn3(x))
20.         x = self.fc2(x)
21.         return F.log_softmax(x)

1. Test set: Average loss: 0.0009, Accuracy: 9817/10000 (98.2%)

1. BASE：Test set: Average loss: 0.0011, Accuracy: 9769/10000 (97.7%)
2. BN：  Test set: Average loss: 0.0014, Accuracy: 9789/10000 (97.9%)

SK model: Stacking two 3x3 conv. layers to replace 5x5 conv. layer

1. class Net(nn.Module):
2.     def __init__(self):
3.         super(Net, self).__init__()
4.         self.conv1_1 = nn.Conv2d(1, 20, kernel_size=(3, 3), stride=(1, 1), padding=0)
5.         self.conv1_2 = nn.Conv2d(20, 20, kernel_size=(3, 3), stride=(1, 1), padding=0)
6.         self.conv2 = nn.Conv2d(20, 50, kernel_size=(3, 3), stride=(1, 1), padding=0)
7.         self.fc1 = nn.Linear(5*5*50, 500)
8.         self.fc2 = nn.Linear(500, 10)
9.         self.bn1_1 = nn.BatchNorm2d(20)
10.         self.bn1_2 = nn.BatchNorm2d(20)
11.         self.bn2 = nn.BatchNorm2d(50)
12.         self.bn3 = nn.BatchNorm1d(500)
13.         self.drop = nn.Dropout(p=0.5)
14.
15.     def forward(self, x):
16.         x = F.relu(self.bn1_1(self.conv1_1(x)))
17.         x = F.relu(self.bn1_2(self.conv1_2(x)))
18.         x = F.max_pool2d(x, 2)
19.         x = self.conv2(x)
20.         x = F.max_pool2d(self.bn2(x), 2)
21.         x = x.view(-1, 5*5*50)
22.         x = self.fc1(x)
23.         x = F.relu(self.bn3(x))
24.         x = self.fc2(x)
25.         return F.log_softmax(x)

1. SK： Test set: Average loss: 0.0008, Accuracy: 9848/10000 (98.5%)

VGG中就使用了这种方法。

1. SK0.2：  97.7%
2. SK0.5：  98.2%
3. SK1：    98.5%
4. SK1.5：  98.6%
5. SK2：    98.5%  (max 98.7%)

2.                     help='use how many training data to train network'

1. 500：   84.2%
2. 1000：  92.0%
3. 2000：  94.3%
4. 5000：  95.5%
5. 10000： 96.6%
6. 20000： 98.4%
7. 60000： 99.1%

1.  0-10000： 98.0%
2. 10000-20000： 97.8%
3. 20000-30000： 97.8%
4. 30000-40000： 97.4%
5. 40000-50000： 97.5%
6. 50000-60000： 97.7%

1. Seed      0：  98.9%
2. Seed      1：  99.0%
3. Seed     12：  99.1%
4. Seed    123：  99.0%
5. Seed   1234：  99.1%
6. Seed  12345：  99.0%
7. Seed 123456：  98.9%

1. ReLU SK_0.2:  99.0%
2. igmoid SK_0.2:  98.6%

+ 关注