知识点:
nn.Module
是PyTorch中的一个基类,用于定义神经网络模型。
train()
: 将模型设为训练模式,启用Batch Normalization和Dropout。eval()
: 将模型设为评估模式,冻结Batch Normalization和Dropout。zero_grad()
: 将所有模型参数的梯度置零。backward()
: 执行反向传播,计算参数的梯度。forward()
: 定义模型的前向计算逻辑。__call__()
: 调用模型对象,会自动调用forward()
方法。
1.nn.Sequential()
nn.Flatten()
被添加到一个神经网络模型 net
的第一层,用于将输入数据展平为一维向量。
import torch.nn as nn
# 定义一个具有两个隐藏层的神经网络
model = nn.Sequential(
nn.Linear(2, 64), # 第一个隐藏层,接受大小为2的输入特征,输出大小为64
nn.ReLU(), # ReLU激活函数
nn.Linear(64, 128), # 第二个隐藏层,接受大小为64的输入特征,输出大小为128
nn.ReLU(), # ReLU激活函数
nn.Linear(128, 1) # 输出层,接受大小为128的输入特征,输出大小为1
)
print(model)
每一层的参数是权重矩阵和偏置向量,它们将在训练过程中进行更新以适应数据
内部
Sequential(
(0): Linear(in_features=2, out_features=64, bias=True)
(1): ReLU()
(2): Linear(in_features=64, out_features=128, bias=True)
(3): ReLU()
(4): Linear(in_features=128, out_features=1, bias=True)
)
loss = nn.MSELoss()
计算均方误差使用的是MSELoss类,也称为平方L 2 范数。默认情况下,它返回所有样本损失的平均值。
l = loss(net(X) ,y)
nn.Parameter()
nn.Parameter()
是PyTorch中用于定义可学习参数(权重)的类。它是torch.Tensor
的子类,并且会被自动注册为模块的可学习参数。
可学习参数是深度学习模型中需要通过反向传播进行优化的变量。在模型训练的过程中,模型会根据损失函数对这些参数进行调整,从而使模型能够更好地拟合训练数据。
nn.Parameter()
可以在模型的初始化方法中使用,将其作为类的属性或模块的属性。当使用nn.Parameter()
创建可学习参数时,你可以指定初始数值或者使用默认参数。这样,这些参数就可以被模型自动识别和优化。
import torch
import torch.nn as nn
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
# 创建可学习参数
self.weight = nn.Parameter(torch.randn(2, 3))
self.bias = nn.Parameter(torch.zeros(2))
def forward(self, x):
# 使用可学习参数
out = torch.matmul(x, self.weight.t()) + self.bias
return out
# 创建模型实例
model = MyModel()
# 随机输入
input_tensor = torch.randn(3, 2)
# 前向计算
output = model(input_tensor)
print(output)
nn.Linear()
nn.Linear()
是 PyTorch 中的一个类,用于创建一个全连接(fully connected)的线性层。它可以用于构建神经网络的一部分
import torch
import torch.nn as nn
# 定义输入特征维度和输出特征维度
input_dim = 10
output_dim = 5
# 创建线性变换层
linear_layer = nn.Linear(input_dim, output_dim)
# 定义输入向量
input_vector = torch.randn(1, input_dim)
# 前向计算
output_vector = linear_layer(input_vector)
print(output_vector)