PyTorch 入门教程:60 分钟掌握深度学习基础
2025-06-18 12:06 更新
在开始学习 PyTorch 之前,你需要了解 PyTorch 是一个开源的 Python 优先深度学习框架,由 Torch7 团队开发,它提供强大的 GPU 加速 Tensor 计算功能以及基于 tape 的自动求导系统,可用于构建深度神经网络。你可以重用喜欢的 Python 包,如 numpy、scipy 和 Cython 来扩展 PyTorch。
一、环境搭建
确保已安装 Python,推荐使用编程狮平台提供的 Python 安装包进行安装。然后可以通过以下命令安装 PyTorch:
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117
此命令会安装 PyTorch 及其相关库。如果你的电脑没有 NVIDIA GPU,可以使用以下命令安装 CPU 版本:
pip3 install torch torchvision torchaudio
二、基础概念
(一)Tensor(张量)
Tensor 是 PyTorch 中的基本数据结构,类似于 numpy 的 ndarray,但它可以在 GPU 上进行计算,从而加速计算过程。
代码示例:
import torch
## 创建一个未初始化的张量
x = torch.empty(2, 3)
print(x)
## 创建一个随机初始化的张量
x = torch.rand(2, 3)
print(x)
## 创建一个全为 0 的张量
x = torch.zeros(2, 3, dtype=torch.long)
print(x)
## 创建一个从数据初始化的张量
x = torch.tensor([5.5, 3])
print(x)
## 创建一个张量并指定数据类型
x = torch.ones(2, 3, dtype=torch.double)
print(x)
print(x.size())
## 添加操作
y = torch.rand(2, 3)
print(x + y)
## 或者
print(torch.add(x, y))
## 提供输出张量作为参数
result = torch.empty(2, 3)
torch.add(x, y, out=result)
print(result)
## 添加:提供 _ 来就地添加
## y.add_(x)
## print(y)
## 索引操作与 numpy 类似
print(x[:, 1])
## 调整大小 / 重塑张量
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)
print(x.size(), y.size(), z.size())
## 如果你有一个包含一个元素的张量,使用 item() 来获取这个值作为 Python 数字
x = torch.randn(1)
print(x)
print(x.item())
(二)Autograd(自动求导)
Autograd 是 PyTorch 中实现自动求导的模块,它能自动计算张量操作的梯度,这是深度学习中反向传播算法的关键。
代码示例:
## 创建一个张量并设置 requires_grad=True 来跟踪其计算历史
x = torch.ones(2, 2, requires_grad=True)
print(x)
## 进行张量操作
y = x + 2
print(y)
## y 被创建于操作中,因此它有一个 grad_fn
print(y.grad_fn)
## 更多操作
z = y * y * 3
out = z.mean()
print(z, out)
## 反向传播
out.backward()
## 现在调用张量的 .grad 属性来获取梯度
print(x.grad)
## 通常在开始新的梯度计算时,需要将梯度置零
x = torch.randn(3, requires_grad=True)
y = x * 2
while y.data.norm() < 1000:
y = y * 2
print(y)
gradients = torch.tensor([0.1, 1.0, 0.0001], dtype=torch.float)
x.backward(gradients)
print(x.grad)
三、构建神经网络
在 PyTorch 中构建神经网络通常涉及定义模型类,该类继承自 nn.Module
,并实现 forward
方法。
(一)定义模型
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# 1 输入图像通道,6 输出通道,5x5 卷积核
self.conv1 = nn.Conv2d(1, 6, 5)
self.conv2 = nn.Conv2d(6, 16, 5)
# 仿射操作:y=Wx+b
self.fc1 = nn.Linear(16 * 5 * 5, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
# 最大池化,窗口大小为 2
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
x = F.max_pool2d(F.relu(self.conv2(x)), 2)
x = x.view(-1, self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
def num_flat_features(self, x):
size = x.size()[1:] # 除去批次维度
num_features = 1
for s in size:
num_features *= s
return num_features
net = Net()
print(net)
(二)模型参数
params = list(net.parameters())
print(len(params))
print(params[0].size()) # 卷积核的权重
(三)输入数据
input = torch.randn(1, 1, 32, 32)
out = net(input)
print(out)
(四)损失函数
output = net(input)
target = torch.randn(10) # 示例目标
target = target.view(1, -1) # 使目标与输出形状相同
criterion = nn.MSELoss()
loss = criterion(output, target)
print(loss)
print(loss.grad_fn) # MSELoss
print(loss.grad_fn.next_functions[0][0]) # Linear
print(loss.grad_fn.next_functions[0][0].next_functions[0][0]) # ReLU
(五)反向传播
net.zero_grad() # 清空所有参数的梯度缓存
print('conv1.bias.grad before backward')
print(net.conv1.bias.grad)
loss.backward()
print('conv1.bias.grad after backward')
print(net.conv1.bias.grad)
(六)更新权重
learning_rate = 0.01
for f in net.parameters():
f.data.sub_(f.grad.data * learning_rate)
或者使用优化器:
import torch.optim as optim
## 创建优化器
optimizer = optim.SGD(net.parameters(), lr=0.01)
## 在训练循环中
optimizer.zero_grad() # 清空梯度
output = net(input)
loss = criterion(output, target)
loss.backward()
optimizer.step() # 更新权重
四、训练循环
一个典型的训练循环如下:
## 数据加载器
## 假设 train_loader 是你的训练数据加载器
## for epoch in range(2): # 2 次完整训练
## for i, data in enumerate(train_loader, 0):
## # 获取输入及其标签
## inputs, labels = data
## ## # 零梯度
## optimizer.zero_grad()
## ## # 前向传播 + 反向传播 + 优化
## outputs = net(inputs)
## loss = criterion(outputs, labels)
## loss.backward()
## optimizer.step()
## ## # 每 2000 批次打印一次统计信息
## if i % 2000 == 1999: # 打印 2000 批次
## print(f'[Epoch {epoch + 1}, Batch {i + 1}] loss: {loss.item():.3f}')
## print('完成训练')
## 保存模型
PATH = './programminglion_net.pth'
torch.save(net.state_dict(), PATH)
## 加载模型
## net.load_state_dict(torch.load(PATH))
以上内容是否对您有帮助:
更多建议: