Appearance
torch.nn 原理及重构
原理
以单层线性回归模型为例:
Python
net = nn.Sequential(nn.Linear(2, 1))对该函数进行拆解: ① nn.Linear(2, 1): 其为一个输入维度为2,输出维度为1的全连接层,即公式:
即每个样本包含2个特征输出1个预测数值,而PyTorch则会自动初始化其中的参数:
- 权重 (Weights,
):一个形状为 (1, 2)的矩阵。 - 偏置 (Bias,
):一个形状为 (1,)的常量。 ②nn.Sequential(...):构建容器 可以通过Python来直接查看该函数各参数:
Python
import torch
import torch.nn as nn
# 1. 定义网络
net = nn.Sequential(nn.Linear(2, 1))
# 2. 模拟输入数据
# 注意:PyTorch 的输入通常是二维张量,形状为 (样本数, 特征数)
# 这里假设我们有 3 个样本,每个样本有 2 个特征
X = torch.tensor([[1.0, 2.0],
[3.0, 4.0],
[5.0, 6.0]]) # shape: (3, 2)
# 3. 直接调用网络进行前向传播(Forward Pass)
output = net(X) # 就像调用函数一样
# 4. 查看结果
print("输入形状:", X.shape)
print("输出形状:", output.shape)
print("预测结果:\n", output)
# 查看全连接层的权重和偏置
print("权重 (Weights):", net[0].weight) # net[0] 代表 Sequential 里的第一个层
print("偏置 (Bias):", net[0].bias)重构TwoLayerMLP(nn.Module)
nn.Module可以通过继承 nn.Module 自定义类重构:
Python
class MyLinearModel(nn.Module):
def __init__(self):
super().__init__() # 必须调用父类的初始化函数!用来激活基类功能
# 在这里定义你需要的“积木”(网络层)
self.layer1 = nn.Linear(in_features=10, out_features=5)
self.layer2 = nn.Linear(in_features=5, out_features=1)
# 第二步:必须实现 forward 函数,定义数据的流动方向
def forward(self, x):
# 描述输入数据 x 是如何一步步通过这些层的
x = self.layer1(x)
x = torch.relu(x) # 加上激活函数
x = self.layer2(x)
return x或:
Python
class LinearReg(nn.Module):
def __init__(self):
super().__init__()
self.fc = nn.Linear(2, 1)
def forward(self, X):
return self.fc(X)