建立CNN模型1

建立CNN模型

1.import 相关模组

  • sequential
  • Conv2D
  • MaxPooling2D
  • Dropout

2. 用Sequential开始建模

3. convolution and pooling

3.1 卷积层

一个滤镜(矩阵),把本来的图片进行转换,转换之后可以代表之前的一些特征。(放大本来的特征)

长宽压缩,高度增加。

3.2 池化层

在每一次卷积时,网络可能无意丢失一些信息,这时pooling可以解决。

Conv2d 和 pooling 交替使用
relu = max(0, x)
sigmoid = 1/(1+e^(-x))

  • 抛弃层
  • 平坦层

隐藏层(暂时不考虑)

4. 训练模型

定义:

  • loss function
  • optimizer
  • metrics 评分方法

进行训练

  • 70%train, 30%test

5. 例子

莫烦

class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.conv1 = nn.Sequential(  # input shape (1, 28, 28)
            nn.Conv2d(
                in_channels=1,      # input height
                out_channels=16,    # n_filters
                kernel_size=5,      # filter size
                stride=1,           # filter movement/step
                padding=2,      # 如果想要 con2d 出来的图片长宽没有变化, padding=(kernel_size-1)/2 当 stride=1
            ),      # output shape (16, 28, 28)
            nn.ReLU(),    # activation
            nn.MaxPool2d(kernel_size=2),    # 在 2x2 空间里向下采样, output shape (16, 14, 14)
        )
        self.conv2 = nn.Sequential(  # input shape (16, 14, 14)
            nn.Conv2d(16, 32, 5, 1, 2),  # output shape (32, 14, 14)
            nn.ReLU(),  # activation
            nn.MaxPool2d(2),  # output shape (32, 7, 7)
        )
        self.out = nn.Linear(32 * 7 * 7, 10)   # fully connected layer, output 10 classes

    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = x.view(x.size(0), -1)   # 展平多维的卷积图成 (batch_size, 32 * 7 * 7)
        output = self.out(x)
        return output

cnn = CNN()
print(cnn)  # net architecture
"""
CNN (
  (conv1): Sequential (
    (0): Conv2d(1, 16, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
    (1): ReLU ()
    (2): MaxPool2d (size=(2, 2), stride=(2, 2), dilation=(1, 1))
  )
  (conv2): Sequential (
    (0): Conv2d(16, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
    (1): ReLU ()
    (2): MaxPool2d (size=(2, 2), stride=(2, 2), dilation=(1, 1))
  )
  (out): Linear (1568 -> 10)
)
"""

你可能感兴趣的:(torch,机器学习)