(pytorch-深度学习系列)网络中的网络(NiN)

网络中的网络(NiN)

LeNet、AlexNet和VGG在设计上的共同之处是:先以由卷积层构成的模块充分抽取空间特征,再以由全连接层构成的模块来输出分类结果。其中,AlexNet和VGG对LeNet的改进主要在于如何对这两个模块加宽(增加通道数)和加深

网络中的网络(NiN)提出了另外一个思路,即串联多个由卷积层和“全连接”层构成的小网络来构建一个深层网络。

NiN块

卷积层的输入和输出通常是四维数组(样本,通道,高,宽),而全连接层的输入和输出则通常是二维数组(样本,特征)。如果想在全连接层后再接上卷积层,则需要将全连接层的输出变换为四维。NiN使用 1 × 1 1\times 1 1×1卷积层来替代全连接层,从而使空间信息能够自然传递到后面的层中去。

NiN块是NiN中的基础块。它由一个卷积层加两个充当全连接层的 1 × 1 1\times 1 1×1卷积层串联而成。其中第一个卷积层的超参数可以自行设置,而第二和第三个卷积层的超参数一般是固定的。

import time
import torch
from torch import nn, optim

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

def nin_block(in_channels, out_channels, kernel_size, stride, padding):
    blk = nn.Sequential(nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding),
                        nn.ReLU(),
                        nn.Conv2d(out_channels, out_channels, kernel_size=1),
                        nn.ReLU(),
                        nn.Conv2d(out_channels, out_channels, kernel_size=1),
                        nn.ReLU())
    return blk

NiN模型

NiN使用卷积窗口形状分别为 11 × 11 11\times 11 11×11 5 × 5 5\times 5 5×5 3 × 3 3\times 3 3×3的卷积层,每个NiN块后接一个步幅为2、窗口形状为 3 × 3 3\times 3 3×3的最大池化层。

NiN去掉了AlexNet最后的3个全连接层,取而代之使用了输出通道数等于标签类别数的NiN块,然后使用全局平均池化层对每个通道中所有元素求平均并直接用于分类。(这里的全局平均池化层即窗口形状等于输入空间维形状的平均池化层).

NiN的这个设计的好处是可以显著减小模型参数尺寸,从而缓解过拟合。然而,该设计有时会造成获得有效模型的训练时间的增加。

import torch.nn.functional as F
class GlobalAvgPool2d(nn.Module):
    # 全局平均池化层可通过将池化窗口形状设置成输入的高和宽实现
    def __init__(self):
        super(GlobalAvgPool2d, self).__init__()
    def forward(self, x):
        return F.avg_pool2d(x, kernel_size=x.size()[2:])
        
class FlattenLayer(torch.nn.Module):
    def __init__(self):
        super(FlattenLayer, self).__init__()
    def forward(self, x): # x shape: (batch, *, *, ...)
        return x.view(x.shape[0], -1)
        
net = nn.Sequential(
    nin_block(1, 96, kernel_size=11, stride=4, padding=0),
    nn.MaxPool2d(kernel_size=3, stride=2),
    nin_block(96, 256, kernel_size=5, stride=1, padding=2),
    nn.MaxPool2d(kernel_size=3, stride=2),
    nin_block(256, 384, kernel_size=3, stride=1, padding=1),
    nn.MaxPool2d(kernel_size=3, stride=2), 
    nn.Dropout(0.5),
    # 标签类别数是10
    nin_block(384, 10, kernel_size=3, stride=1, padding=1),
    GlobalAvgPool2d(), 
    # 将四维的输出转成二维的输出,其形状为(批量大小, 10)
    FlattenLayer())

简单说网络结构就是:
卷 积 ( 96 个 11 ∗ 11 的 核 ) ( 步 长 为 4 ) ( p a d d i n g 为 0 ) → 卷 积 ( 96 个 1 ∗ 1 的 核 ) ( 步 长 为 1 ) → 卷 积 ( 96 个 1 ∗ 1 的 核 ) ( 步 长 为 1 ) → . 降 采 样 ( 最 大 池 化 ) ( 3 ∗ 3 的 核 , 步 长 2 ) → . 卷 积 ( 256 个 5 ∗ 5 的 核 ) ( 步 长 为 1 ) ( p a d d i n g 为 2 ) → 卷 积 ( 256 个 1 ∗ 1 的 核 ) ( 步 长 为 1 ) → 卷 积 ( 256 个 1 ∗ 1 的 核 ) ( 步 长 为 1 ) → . 降 采 样 ( 最 大 池 化 ) ( 3 ∗ 3 的 核 , 步 长 2 ) → . 卷 积 ( 384 个 3 ∗ 3 的 核 ) ( 步 长 为 1 ) ( p a d d i n g 为 1 ) → 卷 积 ( 384 个 1 ∗ 1 的 核 ) ( 步 长 为 1 ) → 卷 积 ( 384 个 1 ∗ 1 的 核 ) ( 步 长 为 1 ) → . 降 采 样 ( 最 大 池 化 ) ( 3 ∗ 3 的 核 , 步 长 2 ) → . 卷 积 ( 10 个 3 ∗ 3 的 核 ) ( 步 长 为 1 ) ( p a d d i n g 为 1 ) → 卷 积 ( 10 个 1 ∗ 1 的 核 ) ( 步 长 为 1 ) → 卷 积 ( 10 个 1 ∗ 1 的 核 ) ( 步 长 为 1 ) → . 降 采 样 ( 平 均 池 化 ) ( x . s i z e 的 核 , 步 长 1 ) \begin{matrix}卷积 \\ (96个11*11的核) \\(步长为4) \\(padding为0)\end{matrix} \rightarrow \begin{matrix}卷积 \\ (96个1*1的核) \\(步长为1)\end{matrix} \rightarrow \begin{matrix}卷积 \\ (96个1*1的核) \\(步长为1)\end{matrix} \rightarrow \\.\\ \begin{matrix}降采样(最大池化) \\ (3*3的核,步长2) \end{matrix}\rightarrow \\.\\ \begin{matrix}卷积 \\ (256个5*5的核) \\(步长为1)\\(padding为2)\end{matrix} \rightarrow \begin{matrix}卷积 \\ (256个1*1的核) \\(步长为1)\end{matrix} \rightarrow \begin{matrix}卷积 \\ (256个1*1的核) \\(步长为1)\end{matrix} \rightarrow \\.\\ \begin{matrix}降采样(最大池化) \\ (3*3的核,步长2) \end{matrix}\rightarrow \\.\\ \begin{matrix}卷积 \\ (384个3*3的核) \\(步长为1)\\(padding为1)\end{matrix} \rightarrow \begin{matrix}卷积 \\ (384个1*1的核) \\(步长为1)\end{matrix} \rightarrow \begin{matrix}卷积 \\ (384个1*1的核) \\(步长为1)\end{matrix} \rightarrow \\.\\ \begin{matrix}降采样(最大池化) \\ (3*3的核,步长2) \end{matrix}\rightarrow \\.\\ \begin{matrix}卷积 \\ (10个3*3的核) \\(步长为1)\\(padding为1)\end{matrix} \rightarrow \begin{matrix}卷积 \\ (10个1*1的核) \\(步长为1)\end{matrix} \rightarrow \begin{matrix}卷积 \\ (10个1*1的核) \\(步长为1)\end{matrix} \rightarrow \\.\\ \begin{matrix}降采样(平均池化) \\ (x.size的核,步长1) \end{matrix} (961111)(4)(padding0)(9611)(1)(9611)(1).()(332).(25655)(1)(padding2)(25611)(1)(25611)(1).()(332).(38433)(1)(padding1)(38411)(1)(38411)(1).()(332).(1033)(1)(padding1)(1011)(1)(1011)(1).()(x.size1)

构建一个数据样本来查看每一层的输出形状。

X = torch.rand(1, 1, 224, 224)
for name, blk in net.named_children(): 
    X = blk(X)
    print(name, 'output shape: ', X.shape)
0 output shape:  torch.Size([1, 96, 54, 54])
1 output shape:  torch.Size([1, 96, 26, 26])
2 output shape:  torch.Size([1, 256, 26, 26])
3 output shape:  torch.Size([1, 256, 12, 12])
4 output shape:  torch.Size([1, 384, 12, 12])
5 output shape:  torch.Size([1, 384, 5, 5])
6 output shape:  torch.Size([1, 384, 5, 5])
7 output shape:  torch.Size([1, 10, 5, 5])
8 output shape:  torch.Size([1, 10, 1, 1])
9 output shape:  torch.Size([1, 10])

训练:

def train(net, train_iter, test_iter, batch_size, optimizer, device, num_epochs):
    net = net.to(device)
    print("training on ", device)
    loss = torch.nn.CrossEntropyLoss()
    for epoch in range(num_epochs):
        train_l_sum, train_acc_sum, n, batch_count, start = 0.0, 0.0, 0, 0, time.time()
        for X, y in train_iter:
            X = X.to(device)
            y = y.to(device)
            y_hat = net(X)
            l = loss(y_hat, y)
            optimizer.zero_grad()
            l.backward()
            optimizer.step()
            train_l_sum += l.cpu().item()
            train_acc_sum += (y_hat.argmax(dim=1) == y).sum().cpu().item()
            n += y.shape[0]
            batch_count += 1
        test_acc = evaluate_accuracy(test_iter, net)
        print('epoch %d, loss %.4f, train acc %.3f, test acc %.3f, time %.1f sec'
              % (epoch + 1, train_l_sum / batch_count, train_acc_sum / n, test_acc, time.time() - start))
batch_size = 128
# 如出现“out of memory”的报错信息,可减小batch_size或resize
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=224)

lr, num_epochs = 0.002, 5
optimizer = torch.optim.Adam(net.parameters(), lr=lr)
train(net, train_iter, test_iter, batch_size, optimizer, device, num_epochs)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值