注意
点击 这里 下载完整示例代码
学习基础知识 || 快速入门 || 张量 || 数据集和数据加载器 || 转换 || 构建模型 || Autograd || 优化 || 保存和加载模型
快速入门¶
本节将介绍机器学习中常见任务的 API。请参阅每个部分中的链接以深入了解。
使用数据¶
PyTorch 有两个 用于处理数据的原语:torch.utils.data.DataLoader
和 torch.utils.data.Dataset
。Dataset
存储样本及其对应的标签,而 DataLoader
在 Dataset
周围包装一个可迭代对象。
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision.transforms import ToTensor
PyTorch 提供特定于领域的库,例如 TorchText、TorchVision 和 TorchAudio,它们都包含数据集。在本教程中,我们将使用 TorchVision 数据集。
torchvision.datasets
模块包含用于许多现实世界视觉数据的 Dataset
对象,例如 CIFAR、COCO(完整列表在此)。在本教程中,我们将使用 FashionMNIST 数据集。每个 TorchVision Dataset
包含两个参数:transform
和 target_transform
,分别用于修改样本和标签。
# Download training data from open datasets.
training_data = datasets.FashionMNIST(
root="data",
train=True,
download=True,
transform=ToTensor(),
)
# Download test data from open datasets.
test_data = datasets.FashionMNIST(
root="data",
train=False,
download=True,
transform=ToTensor(),
)
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz to data/FashionMNIST/raw/train-images-idx3-ubyte.gz
0%| | 0.00/26.4M [00:00<?, ?B/s]
0%| | 65.5k/26.4M [00:00<01:12, 365kB/s]
1%| | 229k/26.4M [00:00<00:38, 683kB/s]
3%|3 | 885k/26.4M [00:00<00:10, 2.53MB/s]
7%|7 | 1.93M/26.4M [00:00<00:05, 4.10MB/s]
25%|##4 | 6.55M/26.4M [00:00<00:01, 15.3MB/s]
38%|###7 | 9.96M/26.4M [00:00<00:00, 17.1MB/s]
58%|#####8 | 15.4M/26.4M [00:01<00:00, 26.2MB/s]
72%|#######1 | 18.9M/26.4M [00:01<00:00, 24.1MB/s]
92%|#########1| 24.2M/26.4M [00:01<00:00, 31.0MB/s]
100%|##########| 26.4M/26.4M [00:01<00:00, 19.4MB/s]
Extracting data/FashionMNIST/raw/train-images-idx3-ubyte.gz to data/FashionMNIST/raw
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz to data/FashionMNIST/raw/train-labels-idx1-ubyte.gz
0%| | 0.00/29.5k [00:00<?, ?B/s]
100%|##########| 29.5k/29.5k [00:00<00:00, 329kB/s]
Extracting data/FashionMNIST/raw/train-labels-idx1-ubyte.gz to data/FashionMNIST/raw
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz to data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz
0%| | 0.00/4.42M [00:00<?, ?B/s]
1%|1 | 65.5k/4.42M [00:00<00:12, 362kB/s]
5%|5 | 229k/4.42M [00:00<00:06, 681kB/s]
19%|#9 | 852k/4.42M [00:00<00:01, 1.94MB/s]
53%|#####2 | 2.33M/4.42M [00:00<00:00, 4.38MB/s]
89%|########8 | 3.93M/4.42M [00:00<00:00, 5.99MB/s]
100%|##########| 4.42M/4.42M [00:00<00:00, 4.87MB/s]
Extracting data/FashionMNIST/raw/t10k-images-idx3-ubyte.gz to data/FashionMNIST/raw
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz to data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz
0%| | 0.00/5.15k [00:00<?, ?B/s]
100%|##########| 5.15k/5.15k [00:00<00:00, 44.6MB/s]
Extracting data/FashionMNIST/raw/t10k-labels-idx1-ubyte.gz to data/FashionMNIST/raw
我们将 Dataset
作为参数传递给 DataLoader
。这将包装数据集上的一个可迭代对象,并支持自动批处理、采样、洗牌和多进程数据加载。这里我们定义一个批次大小为 64,即数据加载器可迭代对象中的每个元素将返回一个包含 64 个特征和标签的批次。
batch_size = 64
# Create data loaders.
train_dataloader = DataLoader(training_data, batch_size=batch_size)
test_dataloader = DataLoader(test_data, batch_size=batch_size)
for X, y in test_dataloader:
print(f"Shape of X [N, C, H, W]: {X.shape}")
print(f"Shape of y: {y.shape} {y.dtype}")
break
Shape of X [N, C, H, W]: torch.Size([64, 1, 28, 28])
Shape of y: torch.Size([64]) torch.int64
阅读更多关于 在 PyTorch 中加载数据。
创建模型¶
为了在 PyTorch 中定义一个神经网络,我们创建一个继承自 nn.Module 的类。我们在 __init__
函数中定义网络的层,并在 forward
函数中指定数据如何通过网络传递。为了加速神经网络中的操作,我们将它移动到 GPU 或 MPS(如果可用)。
# Get cpu, gpu or mps device for training.
device = (
"cuda"
if torch.cuda.is_available()
else "mps"
if torch.backends.mps.is_available()
else "cpu"
)
print(f"Using {device} device")
# Define model
class NeuralNetwork(nn.Module):
def __init__(self):
super().__init__()
self.flatten = nn.Flatten()
self.linear_relu_stack = nn.Sequential(
nn.Linear(28*28, 512),
nn.ReLU(),
nn.Linear(512, 512),
nn.ReLU(),
nn.Linear(512, 10)
)
def forward(self, x):
x = self.flatten(x)
logits = self.linear_relu_stack(x)
return logits
model = NeuralNetwork().to(device)
print(model)
Using cuda device
NeuralNetwork(
(flatten): Flatten(start_dim=1, end_dim=-1)
(linear_relu_stack): Sequential(
(0): Linear(in_features=784, out_features=512, bias=True)
(1): ReLU()
(2): Linear(in_features=512, out_features=512, bias=True)
(3): ReLU()
(4): Linear(in_features=512, out_features=10, bias=True)
)
)
阅读更多关于 在 PyTorch 中构建神经网络。
优化模型参数¶
loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=1e-3)
在一个训练循环中,模型对训练数据集(以批次形式馈送到它)进行预测,并将预测误差反向传播以调整模型的参数。
def train(dataloader, model, loss_fn, optimizer):
size = len(dataloader.dataset)
model.train()
for batch, (X, y) in enumerate(dataloader):
X, y = X.to(device), y.to(device)
# Compute prediction error
pred = model(X)
loss = loss_fn(pred, y)
# Backpropagation
loss.backward()
optimizer.step()
optimizer.zero_grad()
if batch % 100 == 0:
loss, current = loss.item(), (batch + 1) * len(X)
print(f"loss: {loss:>7f} [{current:>5d}/{size:>5d}]")
我们还会检查模型对测试数据集的性能,以确保它正在学习。
def test(dataloader, model, loss_fn):
size = len(dataloader.dataset)
num_batches = len(dataloader)
model.eval()
test_loss, correct = 0, 0
with torch.no_grad():
for X, y in dataloader:
X, y = X.to(device), y.to(device)
pred = model(X)
test_loss += loss_fn(pred, y).item()
correct += (pred.argmax(1) == y).type(torch.float).sum().item()
test_loss /= num_batches
correct /= size
print(f"Test Error: \n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \n")
训练过程在几个迭代(epochs)中进行。在每个 epoch 期间,模型学习参数以做出更好的预测。我们在每个 epoch 打印模型的准确性和损失;我们希望看到准确性随着每个 epoch 的增加而增加,而损失随着每个 epoch 的增加而降低。
epochs = 5
for t in range(epochs):
print(f"Epoch {t+1}\n-------------------------------")
train(train_dataloader, model, loss_fn, optimizer)
test(test_dataloader, model, loss_fn)
print("Done!")
Epoch 1
-------------------------------
loss: 2.303494 [ 64/60000]
loss: 2.294637 [ 6464/60000]
loss: 2.277102 [12864/60000]
loss: 2.269977 [19264/60000]
loss: 2.254235 [25664/60000]
loss: 2.237146 [32064/60000]
loss: 2.231055 [38464/60000]
loss: 2.205037 [44864/60000]
loss: 2.203240 [51264/60000]
loss: 2.170889 [57664/60000]
Test Error:
Accuracy: 53.9%, Avg loss: 2.168588
Epoch 2
-------------------------------
loss: 2.177787 [ 64/60000]
loss: 2.168083 [ 6464/60000]
loss: 2.114910 [12864/60000]
loss: 2.130412 [19264/60000]
loss: 2.087473 [25664/60000]
loss: 2.039670 [32064/60000]
loss: 2.054274 [38464/60000]
loss: 1.985457 [44864/60000]
loss: 1.996023 [51264/60000]
loss: 1.917241 [57664/60000]
Test Error:
Accuracy: 60.2%, Avg loss: 1.920374
Epoch 3
-------------------------------
loss: 1.951705 [ 64/60000]
loss: 1.919516 [ 6464/60000]
loss: 1.808730 [12864/60000]
loss: 1.846550 [19264/60000]
loss: 1.740618 [25664/60000]
loss: 1.698733 [32064/60000]
loss: 1.708889 [38464/60000]
loss: 1.614436 [44864/60000]
loss: 1.646475 [51264/60000]
loss: 1.524308 [57664/60000]
Test Error:
Accuracy: 61.4%, Avg loss: 1.547092
Epoch 4
-------------------------------
loss: 1.612695 [ 64/60000]
loss: 1.570870 [ 6464/60000]
loss: 1.424730 [12864/60000]
loss: 1.489542 [19264/60000]
loss: 1.367256 [25664/60000]
loss: 1.373464 [32064/60000]
loss: 1.376744 [38464/60000]
loss: 1.304962 [44864/60000]
loss: 1.347154 [51264/60000]
loss: 1.230661 [57664/60000]
Test Error:
Accuracy: 62.7%, Avg loss: 1.260891
Epoch 5
-------------------------------
loss: 1.337803 [ 64/60000]
loss: 1.313278 [ 6464/60000]
loss: 1.151837 [12864/60000]
loss: 1.252142 [19264/60000]
loss: 1.123048 [25664/60000]
loss: 1.159531 [32064/60000]
loss: 1.175011 [38464/60000]
loss: 1.115554 [44864/60000]
loss: 1.160974 [51264/60000]
loss: 1.062730 [57664/60000]
Test Error:
Accuracy: 64.6%, Avg loss: 1.087374
Done!
阅读更多关于 训练你的模型。
保存模型¶
保存模型的一种常见方法是序列化内部状态字典(包含模型参数)。
torch.save(model.state_dict(), "model.pth")
print("Saved PyTorch Model State to model.pth")
Saved PyTorch Model State to model.pth
加载模型¶
加载模型的过程包括重新创建模型结构并将状态字典加载到其中。
model = NeuralNetwork().to(device)
model.load_state_dict(torch.load("model.pth", weights_only=True))
<All keys matched successfully>
该模型现在可用于进行预测。
classes = [
"T-shirt/top",
"Trouser",
"Pullover",
"Dress",
"Coat",
"Sandal",
"Shirt",
"Sneaker",
"Bag",
"Ankle boot",
]
model.eval()
x, y = test_data[0][0], test_data[0][1]
with torch.no_grad():
x = x.to(device)
pred = model(x)
predicted, actual = classes[pred[0].argmax(0)], classes[y]
print(f'Predicted: "{predicted}", Actual: "{actual}"')
Predicted: "Ankle boot", Actual: "Ankle boot"
阅读更多关于 保存和加载你的模型。
脚本的总运行时间: ( 1 分钟 1.611 秒)