Pytorch Tutorial-Training Models

这是我学习Pytorch时记录的一些笔记 ,希望能对你有所帮助😊

Introduction

In past sections, we’ve discussed and demonstrated:

  • Building models with the neural network layers and functions of the torch.nn module
  • The mechanics of automated gradient computation, which is central to gradient-based model training
  • Using TensorBoard to visualize training progress and other activities

In this section, we’ll be adding some new tools to your inventory:

  • We’ll get familiar with the dataset and dataloader abstractions, and how they ease the process of feeding data to your model during a training loop
  • We’ll discuss specific loss functions and when to use them
  • We’ll look at PyTorch optimizers, which implement algorithms to adjust model weights based on the outcome of a loss function

Finally, we’ll pull all of these together and see a full PyTorch training loop in action.

Dataset and DataLoader

The Dataset and DataLoader classes encapsulate the process of pulling your data from storage and exposing it to your training loop in batches.

The Dataset is responsible for accessing and processing single instances of data.

The DataLoader pulls instances of data from the Dataset (either automatically or with a sampler that you define), collects them in batches, and returns them for consumption by your training loop. The DataLoader works with all kinds of datasets, regardless of the type of data they contain.

For this tutorial, we’ll be using the Fashion-MNIST dataset provided by TorchVision.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import torch
import torchvision
import torchvision.transforms as transforms

# PyTorch TensorBoard support
from torch.utils.tensorboard import SummaryWriter
from datetime import datetime

transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))])

# Create datasets for training & validation, download if necessary
training_set = torchvision.datasets.FashionMNIST('./data', train=True, transform=transform, download=True)
validation_set = torchvision.datasets.FashionMNIST('./data', train=False, transform=transform, download=True)

# Create data loaders for our datasets; shuffle for training, not for validation
training_loader = torch.utils.data.DataLoader(training_set, batch_size=4, shuffle=True, num_workers=2)
validation_loader = torch.utils.data.DataLoader(validation_set, batch_size=4, shuffle=False, num_workers=2)

# Class labels
classes = ('T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle Boot')

# Report split sizes
print('Training set has {} instances'.format(len(training_set)))
print('Validation set has {} instances'.format(len(validation_set)))
Training set has 60000 instances
Validation set has 10000 instances

As always, let’s visualize the data as a sanity check:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import matplotlib.pyplot as plt
import numpy as np

# Helper function for inline image display
def matplotlib_imshow(img, one_channel=False):
if one_channel:
img = img.mean(dim=0)
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
if one_channel:
plt.imshow(npimg, cmap="Greys")
else:
plt.imshow(np.transpose(npimg, (1, 2, 0)))

dataiter = iter(training_loader)
images, labels = dataiter.next()

# Create a grid from the images and show them
img_grid = torchvision.utils.make_grid(images)
matplotlib_imshow(img_grid, one_channel=True)
print(' '.join(classes[labels[j]] for j in range(4)))
  • make_grid 是 PyTorch 中用于将多张图像拼接成网格的工具,常用于可视化批次数据或特征图

The Model

The model we’ll use in this example is a variant of LeNet-5 - it should be familiar if you’ve watched the previous videos in this series.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import torch.nn as nn
import torch.nn.functional as F

# PyTorch models inherit from torch.nn.Module
class GarmentClassifier(nn.Module):
def __init__(self):
super(GarmentClassifier, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16 * 4 * 4, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)

def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 4 * 4)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x


model = GarmentClassifier()

Loss Funtion

For this example, we’ll be using a cross-entropy loss. For demonstration purposes, we’ll create batches of dummy output and label values, run them through the loss function, and examine the result.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
loss_fn = torch.nn.CrossEntropyLoss()

# NB: Loss functions expect data in batches, so we're creating batches of 4
# Represents the model's confidence in each of the 10 classes for a given input
dummy_outputs = torch.rand(4, 10)
# Represents the correct class among the 10 being tested
dummy_labels = torch.tensor([1, 5, 3, 7])

print(dummy_outputs)
print(dummy_labels)

loss = loss_fn(dummy_outputs, dummy_labels)
print('Total loss for this batch: {}'.format(loss.item()))

tensor([[0.7915, 0.4766, 0.3735, 0.5340, 0.0799, 0.9948, 0.1870, 0.0507, 0.1183,
0.9106],
[0.9666, 0.3765, 0.4324, 0.7354, 0.1953, 0.8906, 0.6882, 0.1925, 0.7076,
0.8777],
[0.4412, 0.0325, 0.4886, 0.9350, 0.9792, 0.5580, 0.6199, 0.2478, 0.3619,
0.8307],
[0.3287, 0.8571, 0.6046, 0.6719, 0.5982, 0.0540, 0.7193, 0.4764, 0.7451,
0.8345]])
tensor([1, 5, 3, 7])
Total loss for this batch: 2.196722984313965

Optimizer

For this example, we’ll be using simple stochastic gradient descent with momentum.

It can be instructive to try some variations on this optimization scheme:

  • Learning rate determines the size of the steps the optimizer takes. What does a different learning rate do to the your training results, in terms of accuracy and convergence time?
  • Momentum nudges the optimizer in the direction of strongest gradient over multiple steps. What does changing this value do to your results?
  • Try some different optimization algorithms, such as averaged SGD, Adagrad, or Adam. How do your results differ?
1
2
# Optimizers specified in the torch.optim package
optimizer = torch.optim.SGD(model.parameters(), lr=0.001, momentum=0.9)

torch.optim.SGD 是 PyTorch 中实现 随机梯度下降(Stochastic Gradient Descent) 的优化器,支持基础的 SGD 和带动量的 SGD(Momentum SGD)。以下是其核心参数、数学原理和使用方法。


1. 核心参数

参数名类型默认值说明
paramsiterable-待优化的模型参数(通常为 model.parameters())。
lrfloat-学习率(必须指定),控制参数更新步长。
momentumfloat0动量因子(0 表示普通 SGD),加速收敛并减少震荡。
dampeningfloat0动量阻尼(通常与 momentum 配合使用)。
weight_decayfloat0L2 正则化系数(防止过拟合)。
nesterovboolFalse是否启用 Nesterov 动量(需 momentum > 0)。

2. 数学原理

(1) 普通 SGD(无动量)

参数更新公式:
$$
\theta_{t+1} = \theta_t - \eta \cdot \nabla_\theta J(\theta_t)
$$

  • $\theta_t$:第 $t$ 步的参数。
  • $\eta$:学习率(lr)。
  • $\nabla_\theta J(\theta_t)$:损失函数对参数的梯度。

(2) 带动量的 SGD

引入动量项 $v_t$:
$$
\begin{align}v_{t+1} &= \mu \cdot v_t + \nabla_\theta J(\theta_t) \
\theta_{t+1} &= \theta_t - \eta \cdot v_{t+1}\end{align}
$$

  • $\mu$:动量系数(momentum,通常设为 0.9)。
  • 动量通过累积历史梯度方向,加速收敛并抑制震荡

(3) Nesterov 动量

在计算梯度时先进行“试探性”更新:
$$
v_{t+1} = \mu \cdot v_t + \nabla_\theta J(\theta_t - \eta \mu v_t) \
\theta_{t+1} = \theta_t - \eta \cdot v_{t+1}
$$

  • 相比普通动量,Nesterov 动量对梯度方向更敏感,收敛更快。

3. 适用场景

场景推荐配置说明
简单任务lr=0.01, momentum=0数据量小、模型简单时,普通 SGD 足够。
深层网络训练lr=0.1, momentum=0.9动量帮助加速收敛,避免陷入局部最优。
对抗训练lr=0.01, momentum=0.9, nesterov=TrueNesterov 动量提升对抗样本生成效果。
稀疏数据lr=0.001, weight_decay=1e-4L2 正则化防止过拟合。

6. 与其他优化器对比

优化器优点缺点
SGD理论收敛性好,调参简单。需手动调学习率,可能收敛慢。
Adam自适应学习率,适合大多数任务。可能在某些任务上泛化性差。
RMSprop适合非平稳目标(如 RNN)。对超参数敏感。

通过合理配置 torch.optim.SGD,你可以在训练速度和模型性能之间取得平衡。对于复杂任务,建议尝试 AdamSGD + 动量 并对比效果。

The Training Loop

Below, we have a function that performs one training epoch. It enumerates data from the DataLoader, and on each pass of the loop does the following:

  • Gets a batch of training data from the DataLoader
  • Zeros the optimizer’s gradients
  • Performs an inference - that is, gets predictions from the model for an input batch
  • Calculates the loss for that set of predictions vs. the labels on the dataset
  • Calculates the backward gradients over the learning weights
  • Tells the optimizer to perform one learning step - that is, adjust the model’s learning weights based on the observed gradients for this batch, according to the optimization algorithm we chose
  • It reports on the loss for every 1000 batches.
  • Finally, it reports the average per-batch loss for the last 1000 batches, for comparison with a validation run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def train_one_epoch(epoch_index, tb_writer):
running_loss = 0.
last_loss = 0.

# Here, we use enumerate(training_loader) instead of
# iter(training_loader) so that we can track the batch
# index and do some intra-epoch reporting
for i, data in enumerate(training_loader):
# Every data instance is an input + label pair
inputs, labels = data

# Zero your gradients for every batch!
optimizer.zero_grad()

# Make predictions for this batch
outputs = model(inputs)

# Compute the loss and its gradients
loss = loss_fn(outputs, labels)
loss.backward()

# Adjust learning weights
optimizer.step()

# Gather data and report
running_loss += loss.item()
if i % 1000 == 999:
last_loss = running_loss / 1000 # loss per batch
print(' batch {} loss: {}'.format(i + 1, last_loss))
tb_x = epoch_index * len(training_loader) + i + 1
tb_writer.add_scalar('Loss/train', last_loss, tb_x)
running_loss = 0.

return last_loss

Per-Epoch Activity

There are a couple of things we’ll want to do once per epoch:

  • Perform validation by checking our relative loss on a set of data that was not used for training, and report this
  • Save a copy of the model

Here, we’ll do our reporting in TensorBoard. This will require going to the command line to start TensorBoard, and opening it in another browser tab.

1
2
3
4
# Initializing in a separate cell so we can easily add more epochs to the same run
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
writer = SummaryWriter('runs/fashion_trainer_{}'.format(timestamp))
epoch_number = 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
EPOCHS = 5

best_vloss = 1_000_000.

for epoch in range(EPOCHS):
print('EPOCH {}:'.format(epoch_number + 1))

# Make sure gradient tracking is on, and do a pass over the data
model.train(True)
avg_loss = train_one_epoch(epoch_number, writer)

# We don't need gradients on to do reporting
model.train(False)

running_vloss = 0.0
for i, vdata in enumerate(validation_loader):
vinputs, vlabels = vdata
voutputs = model(vinputs)
vloss = loss_fn(voutputs, vlabels)
running_vloss += vloss

avg_vloss = running_vloss / (i + 1)
print('LOSS train {} valid {}'.format(avg_loss, avg_vloss))

# Log the running loss averaged per batch
# for both training and validation
writer.add_scalars('Training vs. Validation Loss',
{ 'Training' : avg_loss, 'Validation' : avg_vloss },
epoch_number + 1)
writer.flush()

# Track best performance, and save the model's state
if avg_vloss < best_vloss:
best_vloss = avg_vloss
model_path = 'model_{}_{}'.format(timestamp, epoch_number)
torch.save(model.state_dict(), model_path)

epoch_number += 1

To load a saved version of the model:

1
2
saved_model = GarmentClassifier()
saved_model.load_state_dict(torch.load(PATH))

Once you’ve loaded the model, it’s ready for whatever you need it for - more training, inference, or analysis.

Note that if your model has constructor parameters that affect model structure, you’ll need to provide them and configure the model identically to the state in which it was saved.

Other Resources


Pytorch Tutorial-Training Models
http://pzhwuhu.github.io/2025/08/12/Training Models/
本文作者
pzhwuhu
发布于
2025年8月12日
更新于
2025年8月16日
许可协议