神经网络学习之卷积神经网络

第1关 卷积运算

  1. 已知输入和卷积核,求下列图片中卷积运算的输出结果:
    image.png

    [ [ 130 , 253 ] , [ 157 , 189 ] ]

  2. 已知输入是10∗10的矩阵,卷积核高宽都为5,请问,结果的高和宽分别为多少?
    高为6,宽为6。

第2关 填充(Padding)

  1. 已知输入形状为:10∗10,卷积核的形状为5∗5,求当需要输出形状为10∗10时,需要填充的大小为:
    2
  2. 已知输入形状为:10∗10,卷积核的形状为5∗5,是Valid卷积,则需要填充的大小为:
    0

第3关 步幅(Stride)

  1. 已知输入形状为:10∗10,卷积核的形状为5∗5,填充大小为1,求当需要输出形状为8∗8时,需要的步幅大小为:
    1
  2. 已知输入的形状为n∗n,卷积核形状为3∗3,填充大小为2,步幅为1,输出大小为15∗15,请问,输入的形状为:
    13*13

第4关 多通道输入与多通道输出

  1. 已知输入的形状为3∗24∗24,想要输出的形状为16∗24∗24,请问卷积核的形状应为:
    3 * 16 * 3 * 3,填充为1,步幅为1

第5关 池化层(Pooling)

  1. 已知池化层的输入为3∗224∗224,池化层形状为2∗2,步幅为2,求输出结果的形状为:
    3∗112∗112

第6关 简单的卷积网络的搭建—— LeNet 模型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import torch
from torch import nn
class LeNet(nn.Module):
    def __init__(self):
        super(LeNet, self).__init__()
        '''
        这里搭建卷积层,需要按顺序定义卷积层、
        激活函数、最大池化层、卷积层、激活函数、最大池化层,
        具体形状见测试说明
        '''
        self.conv = nn.Sequential(
            ########## Begin ##########
            nn.Conv2d(1, 6, 5),
            nn.Sigmoid(),
            nn.MaxPool2d(2, 2),
            nn.Conv2d(6, 16, 5),
            nn.Sigmoid(),
            nn.MaxPool2d(2, 2)
            ########## End ##########
        )
        '''
        这里搭建全连接层,需要按顺序定义全连接层、
        激活函数、全连接层、激活函数、全连接层,
        具体形状见测试说明
        '''
        self.fc = nn.Sequential(
            ########## Begin ##########
            nn.Linear(256, 120),
            nn.Sigmoid(),
            nn.Linear(120, 84),
            nn.Sigmoid(),
            nn.Linear(84, 10)
            ########## End ##########
        )
    def forward(self, img):
        '''
        这里需要定义前向计算
        '''
        ########## Begin ##########
        out = self.conv(img)
        out = out.view(out.size(0), -1)
        out = self.fc(out)
        return out
        ########## End ##########

第7关 卷积神经网络—— AlexNet 模型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import torch
from torch import nn
class AlexNet(nn.Module):
    def __init__(self):
        super(AlexNet, self).__init__()
        '''
        这里搭建卷积层,需要按顺序定义卷积层、
        激活函数、最大池化层、卷积层、激活函数、
        最大池化层、卷积层、激活函数、卷积层、
        激活函数、卷积层、激活函数、最大池化层,
        具体形状见测试说明
        '''
        self.conv = nn.Sequential(
            ########## Begin ##########
            nn.Conv2d(1, 96, kernel_size=(11, 11), stride=(4, 4)),  
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False),  
            nn.Conv2d(96, 256, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2)),  
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False),  
            nn.Conv2d(256, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),  
            nn.ReLU(),  
            nn.Conv2d(384, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),  
            nn.ReLU(),  
            nn.Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),  
            nn.ReLU(),  
            nn.MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False),  
            ########## End ##########
        )
        '''
        这里搭建全连接层,需要按顺序定义
        全连接层、激活函数、丢弃法、
        全连接层、激活函数、丢弃法、全连接层,
        具体形状见测试说明
        '''
        self.fc = nn.Sequential(
            ########## Begin ##########
            nn.Linear(in_features=6400, out_features=4096, bias=True),
            nn.ReLU(),  
            nn.Dropout(p=0.5),  
            nn.Linear(in_features=4096, out_features=4096, bias=True),  
            nn.ReLU(),  
            nn.Dropout(p=0.5),  
            nn.Linear(in_features=4096, out_features=10, bias=True),  
            ########## End ##########
        )
    def forward(self, img):
        '''
        这里需要定义前向计算
        '''
        ########## Begin ##########
        feature = self.conv(img)     # 卷积层  
        output = self.fc(feature.view(img.shape[0], -1))     # 全连接层  
        return output
        ########## End ##########