PyTorch学习笔记 2. 运行官网训练、推理的入门示例
一、加载数据
首先引用必要的库:
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision.transforms import ToTensor, Lambda, Compose
import matplotlib.pyplot as plt
与其它机器深度学习类似,Pytorch运行时需要数据集和标签。
Pytorch提供了文本、语音、视频等多个领域的数据库,本文使用的是FashionMNIST视频库数据集。
每个Torch视频库包含两个重要的参数:样本和标签。
# 从开放库下载训练集
training_data = datasets.FashionMNIST(
root="data",
train=True,
download=True,
transform=ToTensor(),
)
# 从开放库下载测试数据集
test_data = datasets.FashionMNIST(
root="data",
train=False,
download=True,
transform=ToTensor(),
)
下面在数据集上进行迭代,可以打印一些数据信息:
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("Shape of X [N, C, H, W]: ", X.shape)
print("Shape of y: ", y.shape, y.dtype)
break
输出结果如下,我们定义的批大小64,迭代器每批返回64个特征和标签。
Shape of X [N, C, H, W]: torch.Size([64, 1, 28, 28])
Shape of y: torch.Size([64]) torch.int64
二、创建模型
使用nn.Module
用来在PyTorch里创建神经网络。这里定义的是顺序网络,
下面定义了神经网络层,可以使用GPU来加速运算。
# Get cpu or gpu device for training.
device = "cuda" if torch.cuda.is_available() else "cpu"
print("Using {} device".format(device))
# Define model
class NeuralNetwork(nn.Module):
def __init__(self):
super(NeuralNetwork, self).__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)
-
torch.nn.Linear(input_data,hidden_layer)
完成从输入层到隐藏层的线性变换; -
torch.nn.ReLU()
为激活函数; -
torch.nn.Linear(hidden_layer, output_data)
完成从隐藏层到输出层的线性变换;
torch.nn.Sequential介绍:
torch.nn.Sequential 类是 torch.nn 中的一种序列容器,可以在其中嵌套各种实现神经网络中来完成对神经网络模型的搭建,而参数会按照我们定义好的序列自动传递下去。
另一种参数传递的方式是orderdict,
torch.nn.Linear
torch.nn.Linear 类用于定义模型的线性层,即完成前面提到的不同的层之间的线性变换。
3. torch.nn.ReLU
torch.nn.ReLU 类属于非线性激活分类,在定义时默认不需要传入参数。
这一步打印网络结构如下:
三、调整模型参数
为了训练模型,这里要定义损失函数和优化器。
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
optimizer.zero_grad()
loss.backward()
optimizer.step()
if batch % 100 == 0:
loss, current = loss.item(), batch * 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 = 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!")
四、保存模型
通常会把内部的状态词典序列化后作为模型保存下来。
torch.save(model.state_dict(), "model.pth")
print("Saved PyTorch Model State to model.pth")
五、加载模型
model = NeuralNetwork()
model.load_state_dict(torch.load("model.pth"))
下面使用模型执行推理:
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():
pred = model(x)
predicted, actual = classes[pred[0].argmax(0)], classes[y]
print(f'Predicted: "{predicted}", Actual: "{actual}"')
推理结果:
完整源代码:
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision.transforms import ToTensor, Lambda, Compose
import matplotlib.pyplot as plt
# 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(),
)
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("Shape of X [N, C, H, W]: ", X.shape)
print("Shape of y: ", y.shape, y.dtype)
break
# Get cpu or gpu device for training.
device = "cuda" if torch.cuda.is_available() else "cpu"
print("Using {} device".format(device))
# Define model
class NeuralNetwork(nn.Module):
def __init__(self):
super(NeuralNetwork, self).__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)
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
optimizer.zero_grad()
loss.backward()
optimizer.step()
if batch % 100 == 0:
loss, current = loss.item(), batch * 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 = 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!")
torch.save(model.state_dict(), "model.pth")
print("Saved PyTorch Model State to model.pth")
model = NeuralNetwork()
model.load_state_dict(torch.load("model.pth"))
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():
pred = model(x)
predicted, actual = classes[pred[0].argmax(0)], classes[y]
print(f'Predicted: "{predicted}", Actual: "{actual}"')
运行结果:
(pytorch) appledeMacBook-Pro:pytorch apple$ python3 learn1.py
/opt/miniconda3/envs/pytorch/lib/python3.7/site-packages/torchvision/datasets/mnist.py:498: UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tensors. This means you can write to the underlying (supposedly non-writeable) NumPy array using the tensor. You may want to copy the array to protect its data or make it writeable before converting it to a tensor. This type of warning will be suppressed for the rest of this program. (Triggered internally at ../torch/csrc/utils/tensor_numpy.cpp:180.)
return torch.from_numpy(parsed.astype(m[2], copy=False)).view(*s)
Shape of X [N, C, H, W]: torch.Size([64, 1, 28, 28])
Shape of y: torch.Size([64]) torch.int64
Using cpu 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)
)
)
Epoch 1
-------------------------------
loss: 2.314974 [ 0/60000]
loss: 2.301318 [ 6400/60000]
loss: 2.279395 [12800/60000]
loss: 2.260673 [19200/60000]
loss: 2.256194 [25600/60000]
loss: 2.230177 [32000/60000]
loss: 2.232735 [38400/60000]
loss: 2.197281 [44800/60000]
loss: 2.199913 [51200/60000]
loss: 2.165345 [57600/60000]
Test Error:
Accuracy: 46.0%, Avg loss: 2.161378
Epoch 2
-------------------------------
loss: 2.181149 [ 0/60000]
loss: 2.164842 [ 6400/60000]
loss: 2.110326 [12800/60000]
loss: 2.114110 [19200/60000]
loss: 2.074363 [25600/60000]
loss: 2.027263 [32000/60000]
loss: 2.043373 [38400/60000]
loss: 1.965748 [44800/60000]
loss: 1.974563 [51200/60000]
loss: 1.901750 [57600/60000]
Test Error:
Accuracy: 58.3%, Avg loss: 1.897587
Epoch 3
-------------------------------
loss: 1.938917 [ 0/60000]
loss: 1.899398 [ 6400/60000]
loss: 1.785908 [12800/60000]
loss: 1.814409 [19200/60000]
loss: 1.720331 [25600/60000]
loss: 1.681701 [32000/60000]
loss: 1.687258 [38400/60000]
loss: 1.586764 [44800/60000]
loss: 1.616351 [51200/60000]
loss: 1.508570 [57600/60000]
Test Error:
Accuracy: 60.8%, Avg loss: 1.523732
Epoch 4
-------------------------------
loss: 1.598114 [ 0/60000]
loss: 1.553433 [ 6400/60000]
loss: 1.402492 [12800/60000]
loss: 1.467502 [19200/60000]
loss: 1.366623 [25600/60000]
loss: 1.368079 [32000/60000]
loss: 1.371022 [38400/60000]
loss: 1.290068 [44800/60000]
loss: 1.333592 [51200/60000]
loss: 1.231188 [57600/60000]
Test Error:
Accuracy: 62.9%, Avg loss: 1.253851
Epoch 5
-------------------------------
loss: 1.339722 [ 0/60000]
loss: 1.310711 [ 6400/60000]
loss: 1.143826 [12800/60000]
loss: 1.242881 [19200/60000]
loss: 1.134900 [25600/60000]
loss: 1.168011 [32000/60000]
loss: 1.180709 [38400/60000]
loss: 1.111995 [44800/60000]
loss: 1.158805 [51200/60000]
loss: 1.070515 [57600/60000]
Test Error:
Accuracy: 64.0%, Avg loss: 1.088987
Done!
Saved PyTorch Model State to model.pth
Predicted: "Ankle boot", Actual: "Ankle boot"