Pytorch Tutorial 学习笔记 (五)模型训练过程
Pytorch官方文档:https://pytorch.org/tutorials/
数据集
以FashionMNIST数据集为例,Pytorch中自带下载链接
import torch
import torchvision
import torchvision.transforms as transforms
# PyTorch TensorBoard
from torch.utils.tensorboard import SummaryWriter
from datetime import datetime
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))])
# 创建训练集和验证集, 如果目录下没有该数据集就进行下载
training_set = torchvision.datasets.FashionMNIST('./data', train=True, transform=transform, download=True)
validation_set = torchvision.datasets.FashionMNIST('./data', train=False, transform=transform, download=True)
# 创建数据加载,shuffle是进行打乱操作
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)
# 数据标签
classes = ('T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle Boot')
# 看整个数据集被分成几份
print('Training set has {} instances'.format(len(training_set)))
print('Validation set has {} instances'.format(len(validation_set)))
可视化
import matplotlib.pyplot as plt
import numpy as np
# 将加载进来的数据进行显示
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()
# 创建网格并进行展示,并输出标签
img_grid = torchvision.utils.make_grid(images)
matplotlib_imshow(img_grid, one_channel=True)
print(' '.join(classes[labels[j]] for j in range(4)))
在实际训练模型过程中也并不一定需要这一步,可以检查图片是否被加载进来或者训练结束进行可视化。
训练模型
import torch.nn as nn
import torch.nn.functional as F
# PyTorch 模型是内嵌在 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_fn = torch.nn.CrossEntropyLoss()
# 示例使用的损失函数是交叉熵损失函数,一次加载四张图片,10个类别进行分类
# Represents the model's confidence in each of the 10 classes for a given input
dummy_outputs = torch.rand(4, 10)
# 代表正确类别
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()))
优化器
# 优化器是torch.optim...自带的,SGD是随机梯度下降
optimizer = torch.optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
定义训练过程
def train_one_epoch(epoch_index, tb_writer):
running_loss = 0.
last_loss = 0.
# 加载数据
for i, data in enumerate(training_loader):
# 需要图片数据和标签信息
inputs, labels = data
# 每一次优化结束进行梯度归零,详情见笔记三
optimizer.zero_grad()
# 预测输出
outputs = model(inputs)
# 计算损失,梯度回传
loss = loss_fn(outputs, labels)
loss.backward()
# 更新权重
optimizer.step()
# 收集训练相关信息
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
训练过程以及验证
# 训练分为好几个epoch,同一批次数据进行训练好几次
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
writer = SummaryWriter('runs/fashion_trainer_{}'.format(timestamp))
epoch_number = 0
EPOCHS = 5
best_vloss = 1_000_000.
for epoch in range(EPOCHS):
print('EPOCH {}:'.format(epoch_number + 1))
# 进入训练模式,也就是会梯度回传更新模型参数
model.train(True)
avg_loss = train_one_epoch(epoch_number, writer)
# 进入测试模式,检测训练效果,梯度不会回传
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))
# 训练和测试的损失进行可视化,见学习笔记四
writer.add_scalars('Training vs. Validation Loss',
{ 'Training' : avg_loss, 'Validation' : avg_vloss },
epoch_number + 1)
writer.flush()
# 记录效果最好的模型
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
保存和加载模型
saved_model = GarmentClassifier()
saved_model.load_state_dict(torch.load(PATH))