深度学习基础知识 nn.Sequential | nn.ModuleList | nn.ModuleDict

深度学习基础知识 nn.Sequential | nn.ModuleList | nn.ModuleDict

  • [1、nn.Sequential 、 nn.ModuleList 、 nn.ModuleDict 类都继承自 Module 类。](#1、nn.Sequential 、 nn.ModuleList 、 nn.ModuleDict 类都继承自 Module 类。)
  • [2、nn.Sequential、nn.ModuleList 和 nn.ModuleDict语法](#2、nn.Sequential、nn.ModuleList 和 nn.ModuleDict语法)
  • [3、Sequential 、ModuleDict、 ModuleList 的区别](#3、Sequential 、ModuleDict、 ModuleList 的区别)
  • [4、ModuleDict、 ModuleList 的区别](#4、ModuleDict、 ModuleList 的区别)
  • [5、nn.ModuleList 、 nn.ModuleDict 与 Python list、Dict 的区别](#5、nn.ModuleList 、 nn.ModuleDict 与 Python list、Dict 的区别)

1、nn.Sequential 、 nn.ModuleList 、 nn.ModuleDict 类都继承自 Module 类。

2、nn.Sequential、nn.ModuleList 和 nn.ModuleDict语法

net = nn.Sequential(nn.Linear(32, 64), nn.ReLU()) →→只需要将定义的层按照顺序写入括号内就可以了
net = nn.ModuleList([nn.Linear(32, 6)4, nn.ReLU()]) →→在定义式需要加上中括号[],将定义的层写入到中括号内
net = nn.ModuleDict({'linear': nn.Linear(32, 64), 'act': nn.ReLU()}) →→需要大括号,将定义的层以键值对的形式写入

代码

python 复制代码
import torch
import torch.nn as nn

net1 = nn.Sequential(nn.Linear(32, 64), nn.ReLU())
net2 = nn.ModuleList([nn.Linear(32, 64), nn.ReLU()])
net3 = nn.ModuleDict({'linear': nn.Linear(32, 64), 'act': nn.ReLU()})

print(net1)
print(net2)
print(net3)

3、Sequential 、ModuleDict、 ModuleList 的区别

1、 ModuleList 仅仅是一个储存各种模块的列表,这些模块之间没有联系也没有顺序(所以不用保证相邻层的输入输出维度匹配),而且没有实现 forward 功能需要自己实现
2、和 ModuleList 一样, ModuleDict 实例仅仅是存放了一些模块的字典,并没有定义 forward 函数需要自己定义
3、而 Sequential 内的模块需要按照顺序排列,要保证相邻层的输入输出大小相匹配,内部 forward 功能已经实现,所以,直接如下写模型,是可以直接调用的,不再需要写forward,sequential 内部已经有 forward

代码:

python 复制代码
import torch
import torch.nn as nn

net1 = nn.Sequential(nn.Linear(32, 64), nn.ReLU())
net2 = nn.ModuleList([nn.Linear(32, 64), nn.ReLU()])
net3 = nn.ModuleDict({'linear': nn.Linear(32, 64), 'act': nn.ReLU()})


x = torch.randn(8, 3, 32)
print(net1(x).shape)    # 输出内容: torch.Size([8, 3, 64])
# print(net2(x).shape)  # 会报错,提示缺少forward
# print(net3(x).shape)   # 会报错,提示缺少forward

为 nn.ModuleList 写 forward 函数
代码:

python 复制代码
import torch
import torch.nn as nn


class My_Model(nn.Module):
    def __init__(self):
        super(My_Model, self).__init__()
        self.layers = nn.ModuleList([nn.Linear(32, 64),nn.ReLU()])

    def forward(self, x):
        for layer in self.layers:
            x = layer(x)
        return x

net = My_Model()

x = torch.randn(8, 3, 32)
out = net(x)
print(out.shape)

输出结果:

为 nn.ModuleDict 写 forward 函数

python 复制代码
import torch
import torch.nn as nn


class My_Model(nn.Module):
    def __init__(self):
        super(My_Model, self).__init__()
        self.layers = nn.ModuleDict({'linear': nn.Linear(32, 64), 'act': nn.ReLU()})

    def forward(self, x):
        for layer in self.layers.values():
            x = layer(x)
        return x

net = My_Model()
x = torch.randn(8, 3, 32)
out = net(x)
print(out.shape)

将 nn.ModuleList 转换成 nn.Sequential

python 复制代码
import torch
import torch.nn as nn

module_list = nn.ModuleList([nn.Linear(32, 64), nn.ReLU()])
net = nn.Sequential(*module_list)
x = torch.randn(8, 3, 32)
print(net(x).shape)

输出如下:

将 nn.ModuleDict 转换成 nn.Sequential

python 复制代码
import torch
import torch.nn as nn

module_dict = nn.ModuleDict({'linear': nn.Linear(32, 64), 'act': nn.ReLU()})
net = nn.Sequential(*module_dict.values())
x = torch.randn(8, 3, 32)
print(net(x).shape)

输出如下:

4、ModuleDict、 ModuleList 的区别

1、ModuleDict 可以给每个层定义名字,ModuleList 不会
2、ModuleList 可以通过索引读取,并且使用 append 添加元素

python 复制代码
import torch.nn as nn

net = nn.ModuleList([nn.Linear(32, 64), nn.ReLU()])
net.append(nn.Linear(64, 10))
print(net)

3、ModuleDict 可以通过 key 读取,并且可以像 字典一样添加元素

python 复制代码
import torch.nn as nn

net = nn.ModuleDict({'linear1': nn.Linear(32, 64), 'act': nn.ReLU()})
net['linear2'] = nn.Linear(64, 128)
print(net)

5、nn.ModuleList 、 nn.ModuleDict 与 Python list、Dict 的区别

python 复制代码
import torch.nn as nn

net = nn.ModuleList([nn.Linear(32, 64), nn.ReLU()])

for name, param in net.named_parameters():
    print(name, param)


print("-----------------------------")
for name, param in net.named_parameters():
    print(name, param.size())

显示结果如下:

python 复制代码
import torch.nn as nn

net = nn.ModuleDict({'linear': nn.Linear(32, 64), 'act': nn.ReLU()})

for name, param in net.named_parameters():
    print(name, param.size())
print("--------------------------")

for name, param in net.named_parameters():
    print(name, param.size())

显示结果:

相关推荐
正脉科工 CAE仿真14 分钟前
抗震计算 | 基于随机振动理论的结构地震响应计算
人工智能
看到我,请让我去学习16 分钟前
OpenCV编程- (图像基础处理:噪声、滤波、直方图与边缘检测)
c语言·c++·人工智能·opencv·计算机视觉
码字的字节18 分钟前
深度解析Computer-Using Agent:AI如何像人类一样操作计算机
人工智能·computer-using·ai操作计算机·cua
冬天给予的预感1 小时前
DAY 54 Inception网络及其思考
网络·python·深度学习
说私域1 小时前
互联网生态下赢家群体的崛起与“开源AI智能名片链动2+1模式S2B2C商城小程序“的赋能效应
人工智能·小程序·开源
董厂长5 小时前
langchain :记忆组件混淆概念澄清 & 创建Conversational ReAct后显示指定 记忆组件
人工智能·深度学习·langchain·llm
G皮T8 小时前
【人工智能】ChatGPT、DeepSeek-R1、DeepSeek-V3 辨析
人工智能·chatgpt·llm·大语言模型·deepseek·deepseek-v3·deepseek-r1
九年义务漏网鲨鱼8 小时前
【大模型学习 | MINIGPT-4原理】
人工智能·深度学习·学习·语言模型·多模态
元宇宙时间8 小时前
Playfun即将开启大型Web3线上活动,打造沉浸式GameFi体验生态
人工智能·去中心化·区块链
开发者工具分享9 小时前
文本音频违规识别工具排行榜(12选)
人工智能·音视频