DAY08:【pytorch】模型容器

1、三大容器

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

1.1 Sequential

1.1.1 概念

nn.Sequential 是 nn.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.ModuleList 是 nn.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.ModuleDict 是 nn.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

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

相关推荐
chh56313 小时前
从 BERT 到大模型:让模型从“会接龙”到“会回答
人工智能·深度学习·bert
TomEval13 小时前
【测AI】第26篇:线上模型监控与告警体系 — 测试不是终点
人工智能·python·功能测试·ai
prince_zxill14 小时前
PEP 751 pylock.toml 实测:pip 26.3 装 6 个包版本全钉死,但 [web] 选不中任何东西
python·pip·依赖管理·pep 751
ZhengEnCi14 小时前
AI 总模仿代码库里的过时写法怎么办:先清坏样例,再用类型与测试让 AI 无法犯错
人工智能·ai编程
一只爱撸猫的程序猿14 小时前
五周、两个开源仓库、九次拦截之后:AI 时代程序员缺的到底是什么
人工智能·后端·开源
GitCode官方15 小时前
开源鸿蒙跨平台直播|AI 时代,用 cj-tauri 开发鸿蒙 PC 应用
人工智能·华为·harmonyos·atomgit
老金带你玩AI15 小时前
GPT-6.2更强更便宜了,为什么还有人骂?
人工智能
Thanks_ks15 小时前
Python 的垃圾回收——引用计数撑起日常,分代回收收拾残局
python·内存管理·垃圾回收·循环引用·引用计数·gc 模块·weakref
CyrusYiVPP15 小时前
实测 uv 0.13.7:同一套包,pip 装 94 秒,它只用 19 秒
人工智能·python
泯泷16 小时前
OpenClaw 退潮背后,是 AI 圈“技术自嗨”的终结
人工智能·chatgpt·llm