人工智能

PyTorch基本操作

本章节介绍 PyTorch 的基本操作,包括张量创建、数学运算和常用函数的使用方法。

1. 张量操作

1.1 张量创建

张量是PyTorch的核心数据结构,可表示为:

  • 标量:x∈Rx \in \mathbb{R}xR
  • 向量:x∈Rn\mathbf{x} \in \mathbb{R}^nxRn
  • 矩阵:X∈Rm×nX \in \mathbb{R}^{m \times n}XRm×n
  • 高阶张量:X∈Rd1×d2×⋯×dk\mathcal{X} \in \mathbb{R}^{d_1 \times d_2 \times \cdots \times d_k}XRd1×d2××dk
import torch

# 推荐使用PyTorch工厂函数
x = torch.tensor([1, 2, 3])  # 从列表创建张量。
print("张量:\n", x)

# 显式指定数据类型
x_float = torch.tensor([1, 2, 3], dtype=torch.float32)
print("指定数据类型张量:\n", x_float)

# 创建特定形状的张量
zeros = torch.zeros(2, 3)  # 2x3的零张量。等价于数学中的零矩阵$O_{2×3}$
ones = torch.ones(2, 3)   # 2x3的全1张量。等价于数学中的全1矩阵$I_{2×3}$
rand = torch.rand(2, 3)   # 标准正态分布张量。等价于数学中的随机矩阵$R_{2×3}$
print("全零张量:\n", zeros)
print("全1张量:\n", ones)
print("随机张量:\n", rand)

# 创建序列张量 arange 和 linspace
x_arange = torch.arange(0, 10, 2)  # 步长为2。[0, 2, 4, 6, 8]
x_lin = torch.linspace(0, 1, 5)  # 均匀分布的5个点。[0.0, 0.25, 0.5, 0.75, 1.0]
print("序列张量:\n", x_arange)
print("均匀分布:\n", x_lin)

1.2 张量维度操作

# 形状操作对比
x = torch.rand(3, 4)
print("内存连续性:", x.is_contiguous())  # 通常为True

# view()需要连续内存,reshape()自动处理非连续情况
x_view = x.view(4, 3)  # 原始数据必须连续
x_reshape = x.reshape(2, 6)  # 自动复制数据保证连续性
print("视图张量:\n", x_view)
print("重塑张量:\n", x_reshape)

# 维度扩展与压缩
x = torch.rand(3, 4)
x_3d = x.unsqueeze(0)  # 形状变为1×3×4
x_flat = x_3d.squeeze()  # 移除非必要维度
print("原始张量:\n", x)
print("扩展维度:\n", x_3d)
print("压缩维度:\n", x_flat)


# 矩阵转置(数学表达式:$X^T$)
x_t = x.t()  # 仅适用于2D张量
x_high = torch.rand(2, 3, 4)
x_trans = x_high.transpose(1, 2)  # 交换第1和第2维度 → 2×4×3
print("转置张量:\n", x_t)
print("高维张量:\n", x_high)
print("高维转置:\n", x_trans)

2. 数学运算

2.1 基本运算

a = torch.tensor([1., 2., 3.])
b = torch.tensor([4., 5., 6.])

# 逐元素运算(Hadamard积)
print("逐元素乘:\n", a * b)  # 数学符号$\odot$

# 矩阵乘法(多种实现方式)
x = torch.randn(2, 3)
y = torch.randn(3, 2)
z1 = torch.mm(x, y)     # 专门矩阵乘法
z2 = x @ y              # Python运算符
z3 = torch.matmul(x, y) # 支持广播的通用乘法

# 向量点积(数学定义:$\mathbf{a} \cdot \mathbf{b} = \sum_i a_i b_i$)
dot_product = torch.dot(a, b)  # 1*4 + 2*5 + 3*6 = 32

2.2 统计运算

x = torch.randn(4, 5)

# 维度参数含义(dim=0表示压缩行,保留列)
print("所有元素求和:\n", x.sum())  # 结果为一个标量
print("每列求和:\n", x.sum(dim=0))  # 结果形状(5,)
print("每行求平均:\n", x.mean(dim=1)) # 结果形状(4,)

# 极值索引获取
values, indices = x.max(dim=1)  # 返回每行最大值及其位置索引
print("最大值:", values)
print("索引:", indices)

3. 神经网络基础组件

3.1 激活函数

激活函数是深度学习中常用的非线性函数,用于引入非线性特性,使得模型可以拟合更复杂的函数关系。PyTorch提供了多种激活函数,包括ReLUSigmoidTanh等。

常用激活函数说明:
ReLURectified Linear Unit):将所有负数置为0,正数保持不变。
Sigmoid:将输入值映射到0到1之间,常用于二分类问题。
Tanh:将输入值映射到-1到1之间,常用于多分类问题。

import torch.nn.functional as F

x = torch.tensor([-1.0, 0.0, 1.0])

# ReLU函数:$f(x) = \max(0, x)$
relu_x = F.relu(x)
print("ReLU结果:\n", relu_x)  # tensor([0., 0., 1.])

# Sigmoid函数:$\sigma(x) = \frac{1}{1+e^{-x}}$
sigmoid_x = torch.sigmoid(x)
print("Sigmoid结果:\n", sigmoid_x)  # ≈ [0.2689, 0.5, 0.7311]

# Tanh函数:$\tanh(x) = \frac{e^x - e^{-x}}{e^x + e^{-x}}$
tanh_x = torch.tanh(x)
print("Tanh结果:\n", tanh_x)     # ≈ [-0.7616, 0.0, 0.7616]

3.2 损失函数

# MSE损失:$L = \frac{1}{n}\sum_{i=1}^n (y_i - \hat{y}_i)^2$
pred = torch.randn(3)
target = torch.randn(3)
mse_loss = F.mse_loss(pred, target)

# 交叉熵损失:$L = -\sum_{c=1}^C y_c \log(p_c)$
logits = torch.randn(3, 5)  # 未归一化的预测值
labels = torch.randint(5, (3,))
ce_loss = F.cross_entropy(logits, labels)  # 内部进行softmax

3.3 实用函数

# 设备转移
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
x = torch.rand(3)
x_gpu = x.to(device)  # 转移到GPU
x_cpu = x_gpu.cpu()   # 移回CPU

# 自动微分演示
x = torch.tensor(2.0, requires_grad=True)
y = x**3 + 2*x
y.backward()  # 计算梯度
print(x.grad) # dy/dx = 3x² + 2 = 14 (当x=2时)

4. 实践示例:线性回归(完整实现)

数学原理

线性回归模型:y^=wx+b\hat{y} = w\mathbf{x} + by^=wx+b
目标:最小化均方误差 L=1n∑i=1n(yi−y^i)2L = \frac{1}{n}\sum_{i=1}^n (y_i - \hat{y}_i)^2L=n1i=1n(yiy^i)2

import matplotlib.pyplot as plt

# 数据生成(真实参数w=2, b=1)
x = torch.linspace(-3, 3, 100).unsqueeze(1)
y = 2 * x + 1 + torch.randn_like(x) * 0.5

# 模型定义
model = torch.nn.Linear(1, 1)  # 隐含参数w和b
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
criterion = torch.nn.MSELoss()

# 训练循环
loss_history = []
for epoch in range(100):
    optimizer.zero_grad()
    pred = model(x)
    loss = criterion(pred, y)
    loss.backward()
    optimizer.step()
    loss_history.append(loss.item())

# 结果可视化
plt.scatter(x.numpy(), y.numpy(), label='Original Data')
plt.plot(x.numpy(), pred.detach().numpy(), 'r', label='Fitted Line')
plt.legend()
plt.show()

# 参数比较
print(f"True: w=2.0, b=1.0 | Pred: w={model.weight.item():.2f}, b={model.bias.item():.2f}")

这个示例展示了如何使用 PyTorch 的基本操作来实现一个简单的线性回归模型。通过这些基础操作,我们可以构建更复杂的深度学习模型。

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐