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]
  )
)
相关推荐
Cxiaomu几秒前
AI 聊天流式交互基础:SSE、EventSource 与 ReadableStream
人工智能·交互
Chase_______2 分钟前
【Python基础 | 第5章】面向对象与异常处理:一文搞懂类、对象、封装、继承、多态
开发语言·python
啦啦啦!2 分钟前
项目环境的搭建,项目的初步使用和deepseek的初步认识
开发语言·c++·人工智能·算法
YanDDDeat2 分钟前
【大模型微调】基于 Llama3-8B 的 LoRA 微调专有领域QA 问答对生成模型
python·语言模型·llama
小李云雾3 分钟前
Python Web 路由详解:核心知识点全覆盖
开发语言·前端·python·路由
Westward-sun.4 分钟前
OpenCV实战:摄像头实时文档扫描与透视矫正
人工智能·opencv·计算机视觉
V搜xhliang02464 分钟前
生成式人工智能、大语言模型在医学教育教学中的前沿探讨
人工智能
枫叶林FYL4 分钟前
【自然语言处理 NLP】7.1 机制可解释性(Mechanistic Interpretability)
人工智能·自然语言处理
任小栗5 分钟前
【实战干货】Vue3 + WebRTC + SIP + AI 实现全自动语音接警系统(远程流获取+实时ASR+TTS回播)
人工智能·webrtc
qq_348231858 分钟前
OpenClaw 完整安装教程
人工智能