DAY08:【pytorch】模型容器

1、三大容器

  • nn.Sequential:按顺序包装多个网络层
  • nn.ModuleList:像 Python 中的 list 一样包装多个网络层
  • nn.ModuleDict:像 Python 中的 dict 一样包装多个网络层

1.1 Sequential

1.1.1 概念

nn.Sequentialnn.Module 的容器,用于按顺序包装一组网络层

1.1.2 特征

  • 顺序性:各网络层之间严格按顺序构建
  • 自带 forward():自带的 forward 里,通过 for 循环依次执行向前传播运算

1.3 代码框架

  • LeNetSequential()
python 复制代码
class LeNetSequential(nn.Module):
    def __init__(self, classes):
        super(LeNetSequential, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 6, 5),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2),
            nn.Conv2d(6, 16, 5),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2),)

        self.classifier = nn.Sequential(
            nn.Linear(16*5*5, 120),
            nn.ReLU(),
            nn.Linear(120, 84),
            nn.ReLU(),
            nn.Linear(84, classes),)

    def forward(self, x):
        x = self.features(x)
        x = x.view(x.size()[0], -1)
        x = self.classifier(x)
        return x
  • LeNetSequentialOrderDict()
python 复制代码
class LeNetSequentialOrderDict(nn.Module):
    def __init__(self, classes):
        super(LeNetSequentialOrderDict, self).__init__()

        self.features = nn.Sequential(OrderedDict({
            'conv1': nn.Conv2d(3, 6, 5),
            'relu1': nn.ReLU(inplace=True),
            'pool1': nn.MaxPool2d(kernel_size=2, stride=2),

            'conv2': nn.Conv2d(6, 16, 5),
            'relu2': nn.ReLU(inplace=True),
            'pool2': nn.MaxPool2d(kernel_size=2, stride=2),
        }))

        self.classifier = nn.Sequential(OrderedDict({
            'fc1': nn.Linear(16*5*5, 120),
            'relu3': nn.ReLU(),

            'fc2': nn.Linear(120, 84),
            'relu4': nn.ReLU(inplace=True),

            'fc3': nn.Linear(84, classes),
        }))

    def forward(self, x):
        x = self.features(x)
        x = x.view(x.size()[0], -1)
        x = self.classifier(x)
        return x

1.2 ModuleList

1.2.1 概念

nn.ModuleListnn.module 的容器,用于包装一组网络层,以索引方式调用网络层

1.2.2 主要方法

  • append():在 ModuleList 后面添加网络层
  • extend():拼接两个 ModuleList
  • insert():指定在 ModuleList 中某个位置插入网络层

1.2.3 代码框架

python 复制代码
class ModuleList(nn.Module):
    def __init__(self):
        super(ModuleList, self).__init__()
        self.linears = nn.ModuleList([nn.Linear(10, 10) for i in range(20)])

    def forward(self, x):
        for i, linear in enumerate(self.linears):
            x = linear(x)
        return x

1.3 ModuleDict

1.3.1 概念

nn.ModuleDictnn.module 的容器,用于包装一组网络层,以索引方式调用网络层

1.3.2 主要方法

  • clear():清空 ModuleDict
  • items():返回可迭代的键值对(key-value pairs)
  • keys():返回字典的键(key)
  • values():返回字典的值(value)
  • pop():返回一组键值对并从字典中删除

1.3.3 代码框架

python 复制代码
class ModuleDict(nn.Module):
    def __init__(self):
        super(ModuleDict, self).__init__()
        self.choices = nn.ModuleDict({
            'conv': nn.Conv2d(10, 10, 3),
            'pool': nn.MaxPool2d(3)
        })

        self.activations = nn.ModuleDict({
            'relu': nn.ReLU(),
            'prelu': nn.PReLU()
        })

    def forward(self, x, choice, act):
        x = self.choices[choice](x)
        x = self.activations[act](x)
        return x

1.4 小结

  • nn.Sequential顺序性,各网络层之间严格按照顺序执行,常用于 block 构建
  • nn.ModuleList迭代性,常用于大量重复网络构建,通过 for 循环实现重复构建
  • nn.ModuleDict字典性,冲用于可选择的网络层构建

2、AlexNet

2.1 背景介绍

2021年 AlextNet 以高出第二名10多个百分点的准确率获得 ImageNet 分类任务冠军,开创了卷积神经网络的新时代

2.2 特征

  1. 采用 ReLU 激活函数:替换了 sigmoid 函数,减轻梯度消失的问题
  2. 采用 LRN(Local Response Normalization):对数据进行归一化,抑制其对输出的影响,减轻梯度消失的问题
  3. 采用 Dropout:提高全连接层的鲁棒性,增加网络的泛化能力
  4. 采用 Data Augmentation:TenCrop 策略,色彩修改

2.3 代码框架

python 复制代码
class AlexNet(nn.Module):
    def __init__(self, num_classes: int = 1000, dropout: float = 0.5) -> None:
        super().__init__()
        _log_api_usage_once(self)
        self.features = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(64, 192, kernel_size=5, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(192, 384, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(384, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(256, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
        )
        self.avgpool = nn.AdaptiveAvgPool2d((6, 6))
        self.classifier = nn.Sequential(
            nn.Dropout(p=dropout),
            nn.Linear(256 * 6 * 6, 4096),
            nn.ReLU(inplace=True),
            nn.Dropout(p=dropout),
            nn.Linear(4096, 4096),
            nn.ReLU(inplace=True),
            nn.Linear(4096, num_classes),
        )

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        x = self.features(x)
        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.classifier(x)
        return x

微语录:熬过无人问津的日子,才能拥抱诗和远方。

相关推荐
飞Link2 分钟前
逆向兼容的桥梁:3to2 自动化降级工具实现全解析
运维·开发语言·python·自动化
管二狗赶快去工作!4 分钟前
体系结构论文(九十八):NPUEval: Optimizing NPU Kernels with LLMs and Open Source Compilers
人工智能·深度学习·自然语言处理·体系结构
zhangshuang-peta8 分钟前
通过 MCP 控制平面引入技能
人工智能·机器学习·ai agent·mcp·peta
曾阿伦11 分钟前
Python3 文件 (夹) 操作备忘录
开发语言·python
LX5677711 分钟前
传统编辑如何考取AI内容编辑师认证?学习路径详解
人工智能·学习
LaughingZhu13 分钟前
Product Hunt 每日热榜 | 2026-04-10
人工智能·经验分享·深度学习·神经网络·产品运营
数据知道14 分钟前
claw-code 源码分析:OmX `$team` / `$ralph`——把 AI 辅助开发从偶发灵感变成可重复流水线
数据库·人工智能·mysql·ai·claude code·claw code
manduic26 分钟前
告别传统编码器痛点!麦歌恩MT6701,重构位置检测选型新逻辑
人工智能·重构·磁性角度传感器
ai大模型中转api测评29 分钟前
告别文字堆砌:Gemini 交互 API 赋能垂直领域,开发者如何重构用户认知?
人工智能·重构·交互·api
陌殇殇33 分钟前
002 Spring AI Alibaba框架整合百炼大模型平台 — 聊天、文生图、语音、向量模型整合
人工智能·spring·ai