前引


2026/1/31日
今天想学完很多东西啊 希望把卷积神经网络学完
LeNet AlexNet VGG NiN GoogLeNet 批量归一化 ResNet

现在开始写 LeNet的推导


从零开始的推荐系统学习之路(十四)---- 动手学深度学习系列 卷积神经网络 CNN(LeNet & AlexNet & VGG & NIN)



1、LeNet


这里推导模型也很简单 参考这张图 直接去写就可以了~

在这里插入图片描述


只不过我的是mac 单独针对mac去写了一些函数

import os

os.environ["PYTORCH_MPS_LOW_WATERMARK_RATIO"] = "0.0"  # 低水位标记
os.environ["PYTORCH_MPS_HIGH_WATERMARK_RATIO"] = "0.0"  # 高水位标记

import torch
from torch import nn

torch.device("mps")

torch.mps.device_count(), torch.cuda.device_count()

import torch
from torch import nn
import numpy as np
from d2l import torch as d2l

def try_gpu_mps(i=0):
    if torch.cuda.device_count() >= i + 1:
        return torch.device(f"cuda:{i + 1}")
    elif torch.mps.device_count() >= i + 1:
        return torch.device(f"mps:{i + 1}")
    else:
        return torch.device("cpu")

# def try_gpu_mps():
#     if torch.backends.mps.is_available():
#         return torch.device("mps")
#     return torch.device("cpu")
try_gpu_mps()


X = torch.zeros(1, 2, device=try_gpu_mps())
X


import torch
from torch import nn
from d2l import torch as d2l

batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size=batch_size)

x, y = next(iter(train_iter))
x.shape


# 默认是 28向量 成卷积层 padding = 2
net = nn.Sequential(
    nn.Conv2d(1, 6, kernel_size=2, padding=2),
    nn.AvgPool2d(2, stride=2),
    nn.Conv2d(6, 16, kernel_size=5),
    nn.AvgPool2d(2, stride=2),
    nn.Flatten(),
    nn.Linear(16 * 25, 120), nn.Sigmoid(),
    nn.Linear(120, 84), nn.Sigmoid(),
    nn.Linear(84, 10))
net


X = torch.rand(size=(1, 1, 28, 28), dtype=torch.float32)
for layer in net:
    X = layer(X)
    print(layer.__class__.__name__,'output shape: \t',X.shape)


def evaluate_accuracy_gpu(net, data_iter, device=None): #@save
    """使用GPU计算模型在数据集上的精度"""
    if isinstance(net, nn.Module):
        net.eval()  # 设置为评估模式
        if not device:
            device = next(iter(net.parameters())).device

    # 正确预测的数量,总预测的数量
    metric = d2l.Accumulator(2)
    with torch.no_grad():
        for X, y in data_iter:
            if isinstance(X, list):
                # BERT微调所需的(之后将介绍)
                X = [x.to(device) for x in X]
            else:
                X = X.to(device)
            y = y.to(device)
            metric.add(d2l.accuracy(net(X), y), y.numel())
    return metric[0] / metric[1]


#@save
def train_ch6(net, train_iter, test_iter, num_epochs, lr, device, activation='sigmoid'):
    """用GPU训练模型(在第六章定义)"""
    def init_weights(m):
        if type(m) == nn.Linear or type(m) == nn.Conv2d:
            if activation == 'relu':
                nn.init.kaiming_uniform_(m.weight)
            else:
                nn.init.xavier_uniform_(m.weight)
                
    net.apply(init_weights)
    print('training on', device)
    net.to(device)
    optimizer = torch.optim.SGD(net.parameters(), lr=lr)
    loss = nn.CrossEntropyLoss()
    animator = d2l.Animator(xlabel='epoch', xlim=[1, num_epochs],
                            legend=['train loss', 'train acc', 'test acc'])
    timer, num_batches = d2l.Timer(), len(train_iter)
    for epoch in range(num_epochs):
        # 训练损失之和,训练准确率之和,样本数
        metric = d2l.Accumulator(3)
        net.train()
        for i, (X, y) in enumerate(train_iter):
            timer.start()
            optimizer.zero_grad()
            X, y = X.to(device), y.to(device)
            y_hat = net(X)
            l = loss(y_hat, y)
            l.backward()
            optimizer.step()
            with torch.no_grad():
                metric.add(l * X.shape[0], d2l.accuracy(y_hat, y), X.shape[0])
            timer.stop()
            train_l = metric[0] / metric[2]
            train_acc = metric[1] / metric[2]
            if (i + 1) % (num_batches // 5) == 0 or i == num_batches - 1:
                animator.add(epoch + (i + 1) / num_batches,
                             (train_l, train_acc, None))
        test_acc = evaluate_accuracy_gpu(net, test_iter)
        animator.add(epoch + 1, (None, None, test_acc))
        print(f"epoch:{epoch + 1}, test_acc:{test_acc:.2f}, train_acc:{train_acc:.2f}")
    print(f'loss {train_l:.3f}, train acc {train_acc:.3f}, '
          f'test acc {test_acc:.3f}')
    print(f'{metric[2] * num_epochs / timer.sum():.1f} examples/sec '
          f'on {str(device)}')


lr, num_epochs = 0.1, 10
train_ch6(net, train_iter, test_iter, num_epochs, lr, try_gpu_mps())

在这里插入图片描述

在这里插入图片描述


2、AlexNet


加入了 dropout Relu 且更大更深

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

# 默认是 224 向量 
net = nn.Sequential(
    nn.Conv2d(1, 96, kernel_size=11, stride=4, padding=1), nn.ReLU(),
    nn.MaxPool2d(3, stride=2),
    nn.Conv2d(96, 256, kernel_size=5, padding=2), nn.ReLU(),
    nn.MaxPool2d(3, stride=2),
    nn.Conv2d(256, 384, kernel_size=3, padding=1), nn.ReLU(),
    nn.Conv2d(384, 384, kernel_size=3, padding=1), nn.ReLU(),
    nn.Conv2d(384, 256, kernel_size=3, padding=1), nn.ReLU(),
    nn.MaxPool2d(3, stride=2), nn.Flatten(),
    nn.Linear(6400, 4096), nn.ReLU(), nn.Dropout(p=0.5),
    nn.Linear(4096, 4096), nn.ReLU(), nn.Dropout(p=0.5),
    nn.Linear(4096, 10))
net


X = torch.rand(size=(1, 1, 224, 224), dtype=torch.float32)
for layer in net:
    X = layer(X)
    print(layer.__class__.__name__,'output shape: \t',X.shape)


batch_size = 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size=batch_size, resize=224)

x, y = next(iter(train_iter))
x.shape



lr, num_epochs = 0.1, 10
train_ch6(net, train_iter, test_iter, num_epochs, lr, try_gpu_mps())



在这里插入图片描述

在这里插入图片描述


3、VGG


更大更深 核心思想 长宽缩减 通道数变多
且以相同架构 块为设计

还有核心的几点 VGG之间的卷积层 也是用Relu连接的

在这里插入图片描述
在这里插入图片描述


# num_convs 块中间有多少层卷积层
# 尝试后 发现kernel_size 为 3效果更好
def vgg_block(num_convs, in_channels, out_channels):
    layers = []

    for _ in range(num_convs):
        conv_layer = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1)
        layers.append(conv_layer)
        layers.append(nn.ReLU())
        in_channels = out_channels

    # 长宽 / 2
    layers.append(nn.MaxPool2d(kernel_size=2, stride=2)) 
    return nn.Sequential(*layers)

vgg_block(3, 256, 1000)


conv_arch = ((1, 64), (1, 128), (2, 256), (2, 512), (2, 512))

def vgg(conv_arch):
    conv_blocks = []
    in_channels = 1
    for (num_convs, out_channels) in conv_arch:
        conv_blocks.append(vgg_block(num_convs, in_channels, out_channels))
        in_channels = out_channels

    # 5个MaxPool 每次一过 长宽缩减
    return nn.Sequential(*conv_blocks, nn.Flatten(),
                         nn.Linear(out_channels * (224 // pow(2, 5)) * (224 // pow(2, 5)), 4096), nn.ReLU(), nn.Dropout(p=0.5),
                         nn.Linear(4096, 4096), nn.ReLU(), nn.Dropout(p=0.5),
                         nn.Linear(4096, 10))


X = torch.zeros((1, 1, 224, 224))

for layer in vgg(conv_arch):
    X = layer(X)
    print(f"{layer.__class__.__name__}\t output shape:{X.shape}")



# 小数据
ratio = 4
small_conv_arch = [(pair[0], pair[1] // ratio) for pair in conv_arch]
net = vgg(small_conv_arch)

X = torch.zeros((1, 1, 224, 224))

for layer in net:
    X = layer(X)
    print(f"{layer.__class__.__name__}\t output shape:{X.shape}")


lr, num_epochs, batch_size = 0.1, 10, 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size=batch_size, resize=224)
train_ch6(net, train_iter, test_iter, num_epochs, lr, try_gpu_mps(), "relu")

在这里插入图片描述

在这里插入图片描述


4、NIN


本质上 是AlexNet的升级版 把很多层替换成了 1X1的卷积层 最后也替换了成了 全局池化

在这里插入图片描述


net = nn.Sequential(
    nin_block(1, 96, kernel_size=11, strides=4, padding=0), 
    nn.MaxPool2d(kernel_size=3, stride=2),
    
    nin_block(96, 256, kernel_size=5, strides=1, padding=1),
    nn.MaxPool2d(kernel_size=3, stride=2),
    
    nin_block(256, 384, kernel_size=3, strides=1, padding=1),
    nn.MaxPool2d(kernel_size=3, stride=2),
    nn.Dropout(0.5),

    nin_block(384, 10, kernel_size=3, strides=1, padding=1),
    nn.AdaptiveAvgPool2d((1, 1)),
    nn.Flatten())



X = torch.zeros((1, 1, 224, 224))

for layer in net:
    X = layer(X)
    print(f"layer_name:{layer.__class__.__name__}, next shape:{X.shape}")

img = torch.arange(24,dtype=torch.float).reshape(1,1,4,6)
pool_1 = nn.AdaptiveAvgPool2d(1)
img, pool_1(torch.concat([img, img + 1], 1))


lr, num_epochs, batch_size = 0.1, 10, 256
train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size=batch_size, resize=224)
train_ch6(net, train_iter, test_iter, num_epochs, lr, try_gpu_mps(), "relu")

在这里插入图片描述

在这里插入图片描述

更多推荐