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

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

相关推荐
爱吃泡芙的小白白1 天前
深入浅出:卷积神经网络(CNN)池化层全解析——从MaxPool到前沿发展
人工智能·神经网络·cnn·池化层·最大值池化·平均值池化
2601_948374571 天前
商用电子秤怎么选
大数据·python
jigsaw_zyx1 天前
提示词工程
人工智能·算法
Volunteer Technology1 天前
Sentinel的限流算法
java·python·算法
jdyzzy1 天前
什么是 JIT 精益生产模式?它与传统的生产管控方式有何不同?
java·大数据·人工智能·jit
LittroInno1 天前
TVMS视频管理平台 —— 多种目标跟踪模式
人工智能·计算机视觉·目标跟踪
查无此人byebye1 天前
突破性图像分词技术TiTok:32个Token实现高效图像重建与生成
人工智能
Niuguangshuo1 天前
DALL-E 2:从CLIP潜变量到高质量图像生成的突破
人工智能·深度学习·transformer
偷吃的耗子1 天前
【CNN算法理解】:基于训练好的MNIST CNN模型进行预测
人工智能·算法·cnn
Elastic 中国社区官方博客1 天前
跳过 MLOps:通过 Cloud Connect 使用 EIS 为自管理 Elasticsearch 提供托管云推理
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索