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 分钟前
Bianfchheng (Liuu) 《边城(六)》字母标调拼音拼写实测案例
人工智能·自然语言处理·人机交互·语音识别·学习方法
凯丨23 分钟前
AI 蠕虫来了:恶意文档如何通过 Copilot for Word 自我传播?
人工智能·c#·copilot
武子康26 分钟前
GPT-5.6 Luna 降价 80%:Agent 真正该重算的是单次成功成本
人工智能·chatgpt·llm
Gu Gu Study28 分钟前
ScoutLoop开放域深度研究引擎(agent的初步设计想法)
人工智能·python
HyperAI超神经1 小时前
15亿参数挑战机器人控制,MiniCPM-RobotManip正式开源;覆盖文本/图/视频/动作序列,英伟达发布全模态模型Cosmos3-Edge
人工智能·深度学习·机器人·多模态·图像生成·具身智能·智能体
搞科研的小刘选手1 小时前
【国际生态学协会主办】2026年生态环境与人工智能国际学术会议(ICAIEE 2026)
人工智能·生态环境·学术会议·会议推荐
笨鸟先飞,勤能补拙1 小时前
AI 赋能网络安全领域深度剖析
网络·人工智能·windows·安全·web安全·网络安全·github
小鸟你好啊1 小时前
搭建基于 Solon AI 的 Streamable MCP 服务并部署至阿里云百炼
人工智能·阿里云·云计算
Summer-Bright1 小时前
深度 | Agent框架大洗牌:AutoGen退场后的新秩序
人工智能·ai·语言模型·ai软件
ThsPool1 小时前
【遥感学习整理 02】ENVI遥感图像处理基础:从数据读取到遥感信息产品
图像处理·人工智能·学习