Pytorch容器

文章目录


1.torch.nn.Parameter()

  torch.nn.Parameter() 是 PyTorch 中的一个类,用于将张量包装成可训练的参数。

  在神经网络中,我们需要定义可训练的参数,例如模型的权重和偏置。torch.nn.Parameter() 允许我们将张量包装成一个特殊的参数对象,该对象会被注册为模型的一部分,并且可以自动进行梯度计算和更新。

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

# 定义一个线性模型类
class LinearModel(nn.Module):
    def __init__(self):
        super(LinearModel, self).__init__()
        # 创建一个可训练的参数
        self.weights = nn.Parameter(torch.randn(3, 2))
        self.bias = nn.Parameter(torch.zeros(3))

    def forward(self, x):
        # 使用参数进行线性变换
        return torch.matmul(x, self.weights) + self.bias

# 创建一个线性模型对象
model = LinearModel()

# 获取模型的参数
parameters = list(model.parameters())

print(parameters)
print("==============")
print(model.parameters)
python 复制代码
[Parameter containing:
tensor([[-1.0570, -0.7752],
        [-1.1010,  0.0697],
        [ 1.3795,  0.5130]], requires_grad=True), Parameter containing:
tensor([0., 0., 0.], requires_grad=True)]
==============
<bound method Module.parameters of LinearModel()>

2.torch.nn.Module()

  torch.nn.Module() 是 PyTorch 中的一个基类,用于定义神经网络模型的基本结构。torch.nn.Module 是一个可扩展的类,用于构建神经网络模型。当我们定义自己的神经网络模型时,通常会继承 torch.nn.Module 类,并重写其中的方法,以定义模型的结构和前向传播逻辑。

代码如下(示例):

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

# 定义一个简单的全连接神经网络模型
class SimpleNet(nn.Module):
    def __init__(self):
        super(SimpleNet, self).__init__()
        # 定义模型的层
        self.fc1 = nn.Linear(10, 20)
        self.fc2 = nn.Linear(20, 10)

    def forward(self, x):
        # 定义模型的前向传播逻辑
        x = self.fc1(x)
        x = torch.relu(x)
        x = self.fc2(x)
        return x

# 创建一个模型实例
model = SimpleNet()

# 打印模型结构
print(model)
python 复制代码
SimpleNet(
  (fc1): Linear(in_features=10, out_features=20, bias=True)
  (fc2): Linear(in_features=20, out_features=10, bias=True)
)

3.torch.nn.Sequential()

  torch.nn.Sequential() 是 PyTorch 中的一个类,用于按顺序组合多个层或模块,构建神经网络模型。torch.nn.Sequential 允许我们将多个层或模块按照顺序连接起来,形成一个串联的神经网络模型。我们可以通过传入层或模块的列表来定义网络的结构,或者在创建 Sequential 实例后使用 .add() 方法逐个添加层或模块。

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

# 使用 Sequential 定义一个简单的全连接神经网络模型
model = nn.Sequential(
    nn.Linear(10, 20),
    nn.ReLU(),
    nn.Linear(20, 10)
)

# 打印模型结构
print(model)
python 复制代码
Sequential(
  (0): Linear(in_features=10, out_features=20, bias=True)
  (1): ReLU()
  (2): Linear(in_features=20, out_features=10, bias=True)
)

4.torch.nn.ModuleList()

torch.nn.ModuleList() 是 PyTorch 中的一个类,用于将多个模块组合成一个模块列表。torch.nn.ModuleList 提供了一种容器,可以用于存储和管理多个模块,类似于 Python 中的列表。与普通的 Python 列表不同,ModuleList 将被视为模型的一部分,并且在模型中注册为子模块,以便在模型的其他部分中使用。

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

# 定义一个模型类,其中包含多个线性层
class MyModel(nn.Module):
    def __init__(self):
        super(MyModel, self).__init__()
        self.layers = nn.ModuleList([
            nn.Linear(10, 20),
            nn.ReLU(),
            nn.Linear(20, 10)
        ])

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

# 创建一个模型实例
model = MyModel()

# 打印模型结构
print(model)
python 复制代码
MyModel(
  (layers): ModuleList(
    (0): Linear(in_features=10, out_features=20, bias=True)
    (1): ReLU()
    (2): Linear(in_features=20, out_features=10, bias=True)
  )
)

5.torch.nn.ModuleDict()

torch.nn.ModuleDict() 是 PyTorch 中的一个类,用于将多个模块组合成一个模块字典。torch.nn.ModuleDict 提供了一种容器,可以用于存储和管理多个模块,并按照键值对的形式进行访问。类似于 Python 中的字典,ModuleDict 允许通过键来访问存储的模块。

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

# 定义一个模型类,其中包含多个线性层,并以字典形式存储
class MyModel(nn.Module):
    def __init__(self):
        super(MyModel, self).__init__()
        self.layers = nn.ModuleDict({
            'linear1': nn.Linear(10, 20),
            'relu': nn.ReLU(),
            'linear2': nn.Linear(20, 10)
        })

    def forward(self, x):
        x = self.layers['linear1'](x)
        x = self.layers['relu'](x)
        x = self.layers['linear2'](x)
        return x

# 创建一个模型实例
model = MyModel()

# 打印模型结构
print(model)
python 复制代码
MyModel(
  (layers): ModuleDict(
    (linear1): Linear(in_features=10, out_features=20, bias=True)
    (relu): ReLU()
    (linear2): Linear(in_features=20, out_features=10, bias=True)
  )
)

6.torch.nn.ParameterList()

torch.nn.ParameterList() 是 PyTorch 中的一个类,用于将多个参数(torch.nn.Parameter)组合成一个参数列表。torch.nn.ParameterList 提供了一种容器,可以用于存储和管理多个参数,并按照列表的形式进行访问。它通常用于将一组可学习的参数组织在一起,以便在模型中使用或进行优化。

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

# 定义一个模型类,其中包含多个可学习的参数并以列表形式存储
class MyModel(nn.Module):
    def __init__(self):
        super(MyModel, self).__init__()
        self.parameters = nn.ParameterList([
            nn.Parameter(torch.randn(10, 20)),
            nn.Parameter(torch.randn(20, 10))
        ])

    def forward(self, x):
        # 在前向传播中使用参数
        return torch.matmul(x, self.parameters[0]) + self.parameters[1]

# 创建一个模型实例
model = MyModel()

# 打印模型结构
print(model)
python 复制代码
MyModel(
  (parameters): ParameterList(
      (0): Parameter containing: [torch.float32 of size 10x20]
      (1): Parameter containing: [torch.float32 of size 20x10]
  )
)

7.torch.nn.ParameterDict()

torch.nn.ParameterDict() 是 PyTorch 中的一个类,用于将多个参数(torch.nn.Parameter)组合成一个参数字典。

torch.nn.ParameterDict 提供了一种容器,可以用于存储和管理多个参数,并按照键值对的形式进行访问。类似于 Python 中的字典,ParameterDict 允许通过键来访问存储的参数。

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

# 定义一个模型类,其中包含多个可学习的参数,并以字典形式存储
class MyModel(nn.Module):
    def __init__(self):
        super(MyModel, self).__init__()
        self.parameters = nn.ParameterDict({
            'weight1': nn.Parameter(torch.randn(10, 20)),
            'weight2': nn.Parameter(torch.randn(20, 10))
        })

    def forward(self, x):
        # 在前向传播中使用参数
        return torch.matmul(x, self.parameters['weight1']) + self.parameters['weight2']

# 创建一个模型实例
model = MyModel()

# 打印模型结构
print(model)
python 复制代码
MyModel(
  (parameters): ParameterDict(
      (weight1): Parameter containing: [torch.FloatTensor of size 10x20]
      (weight2): Parameter containing: [torch.FloatTensor of size 20x10]
  )
)
相关推荐
罗西的思考1 小时前
AI 时代的 “房谋杜断”:如何培养能力、品味与判断力
人工智能·算法·机器学习
在世修行1 小时前
智学文枢:页面切换与跨页数据预填
python·桌面应用·signal·1024程序员节·pyside·跨页跳转·状态预填
字节跳动视频云技术团队2 小时前
720P 直升 8K,解密 2026 抖音美好奇妙夜巨幕超分黑科技
人工智能·音视频开发
2601_962885722 小时前
如何用 Python 把 K 线重采样成任意周期?
开发语言·python
打工仔折腾 AI2 小时前
在 Ubuntu ARM64 部署 Q1 V3.0 MuJoCo 模型:加载、单步推理与执行器响应解析
linux·运维·人工智能·python·深度学习·ubuntu·性能优化
逐米时代2 小时前
从试点验证到协同深化:制造业AI转型三年路线图设计方法
人工智能
GlobalInfo2 小时前
全球及中国风电叶片检测机器人行业研究报告(2026版):市场现状、行业前景与占有率分析
运维·网络·人工智能·机器人
信誓旦旦的程序猿3 小时前
【Python量化实战宝典 #21】北向资金跟随手册:聪明钱择时信号(Python 可跑)
python·1024程序员节·股票数据api·股票数据api接口·股票api数据接口·股票量化数据api·股票量化数据接口
严谨的麻辣烫3 小时前
静态 ISP 代理请求超时怎么排查?Python 与 curl 实战
python·http·requests·1024程序员节·网络排障
stormzhangV3 小时前
AI 越强,失业越严重
人工智能·aigc