pytorch实现mnist手写集识别,学习记录。
一.导入库
import torch
from torchvision import transforms
from torchvision import datasets
from torch.utils.data import DataLoader
import torch.nn.functional as F
import torch.optim as optim
transforms 用于图像处理的库
datasets 与 dataloader是用于导入数据包以及构建mini-batch
import torch.nn.functional as F 为了使用 relu()作为激活函数
import torch.optim as optim 用来加入优化器
二. 数据导入与处理
batch_size = 64
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307, ),(0.3081, ))
])
train_dataset = datasets.MNIST(root='../dataset/mnist/',
train=True,
download=True,
transform=transform)
train_loader = DataLoader(train_dataset,
shuffle=True,
batch_size= batch_size)
test_dataset = datasets.MNIST(root='../dataset/mnist/',
train=False,
download=True,
transform=transform)
test_loader = DataLoader(test_dataset,
shuffle=False,
batch_size= batch_size)
再读入图像数据时,是通过python的pillow读入的。对于神经网络处理,理想输入为服从正态分布的小数值,位于[0,1]。此时读入的数据为
pillow读入的图像张量的表述形式为 W*H*C(宽高通道),而在pytorch中需要将图像的描述变为C*W*H,便于更高效的转换。
基于以上两个原因:
1.{0,..,255}的像素值转变为[0,1]浮点数
2.将单通道变为多通道
采用transforms.ToTensor() 进行转换。
第二步进行归一化,均值和标准差的取值是计算所有像素得出的。
transform = transforms.Compose() 的作用是将两步数据处理进行结合。
train_dataset = datasets.MNIST(root='../dataset/mnist/',
train=True,
download=True,
transform=transform)
train_loader = DataLoader(train_dataset,
shuffle=True,
batch_size= batch_size)
test_dataset = datasets.MNIST(root='../dataset/mnist/',
train=False,
download=True,
transform=transform)
test_loader = DataLoader(test_dataset,
shuffle=False,
batch_size= batch_size)
epoch:全部训练样本进行一次前向与反向传播
Batch-size:一次前向反向传播的样本数量
iteration:进行一次epoch时,batch-size循环的次数
对于cnn的输入,采用mini-batch可以在训练的性能和时间上进行折中。
shuffle:是否打乱
三.搭建网络结构
1.全连接层
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.l1 = torch.nn.Linear(784, 512)
self.l2 = torch.nn.Linear(512, 256)
self.l3 = torch.nn.Linear(256, 128)
self.l4 = torch.nn.Linear(128, 64)
self.l5 = torch.nn.Linear(64, 10)
def forward(self, x):
x = x.view(-1, 784)
x = F.relu(self.l1(x))
x = F.relu(self.l2(x))
x = F.relu(self.l3(x))
x = F.relu(self.l4(x))
return self.l5(x) # 最后一层不做激活,不进行非线性变换
model=Net()
linear构造对象linear unit,包括w和b两个参数。
对于torch.nn.linear 中,包含三个参数,输入的维度、输出的维度、是否要偏置量
x = x.view(-1,784): 在全连接神经网络中,输入为矩阵。此时的输入为(N,1,28,28)为四阶张量,需要将后面的(1,28,28)变为三阶,因此采用该语句。784=1*28*28,图像的每一行拿出来首尾相接,此时为784列。-1 代表计算机自己算数值,代表输入的量--N。
最后一层不做激活是因为后续的损失函数接入softmax转变为概率。
2.CNN
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = torch.nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = torch.nn.Conv2d(10, 20, kernel_size=5)
self.pooling = torch.nn.MaxPool2d(2)
self.fc = torch.nn.Linear(320, 10)
def forward(self, x):
# flatten data from (n,1,28,28) to (n, 784)
batch_size = x.size(0)
x = F.relu(self.pooling(self.conv1(x)))
x = F.relu(self.pooling(self.conv2(x)))
x = x.view(batch_size, -1) # -1 此处自动算出的是320
x = self.fc(x)
return x
model = Net()
# 模型迁移到gpu
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device)
输入的图像大小为N*1*28*28,经过第一层卷积(N*10*24*24)与池化变成N*10*12*12;
经过第二层的卷积(N*20*8*8)与池化变为N*20*4*4
所以在最后的全连接层,第一个参数为20*4*4=320。
四、损失函数与优化器
criterion = torch.nn.CrossEntropyLoss() optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5)
损失函数采用的是交叉熵函数,对于分类问题,最后通过softmax层输出每一种类别的概率,交叉熵函数可以表示预测类别的概率与实际类别之间的差距,即两种概率分布之间的差距大小。
优化器采用随机梯度下降,与MSE相比,可以跨越鞍点,提高性能,但在并行性上较差,所以采用mini-batch小批量训练,进行性能与时间的折中。
model.parametes是自动寻找需要更新的权重,便于之后的更新。
五、训练与测试
单轮训练封装为函数
def train(epoch):
running_loss = 0.0
for batch_idx, data in enumerate(train_loader, 0):
# 获得一个批次的数据和标签
inputs, target = data
# 输入输出放到和模型同一块显卡上
inputs, target = inputs.to(device), target.to(device)
optimizer.zero_grad()
# 获得模型预测结果(64, 10)
outputs = model(inputs)
# 交叉熵代价函数outputs(64,10),target(64)
loss = criterion(outputs, target)
loss.backward()
optimizer.step()
running_loss += loss.item()
if batch_idx % 300 == 299:
print('[%d, %5d] loss: %.3f' % (epoch+1, batch_idx+1, running_loss/300))
running_loss = 0.0
enunmerate是在训练集中进行 遍历
梯度清零是避免每一轮的梯度相加,影响后续计算
runningloss每次累加的目的是便于记录每轮训练的损失值,观察训练情况
def test():
correct = 0
total = 0
with torch.no_grad():
for data in test_loader:
images, labels = data
# 输入输出放到和模型同一块显卡上
images, labels = images.to(device), labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, dim=1) # dim = 1 列是第0个维度,行是第1个维度
total += labels.size(0)
correct += (predicted == labels).sum().item() # 张量之间的比较运算
print('accuracy on test set: %d %% ' % (100*correct/total))
with torch.no_grad 是因为在测试过程中不需要进行反向梯度传播的过程。
_, predicted = torch.max(outputs.data, dim=1) 是寻找预测的概率分布中最大的概率值对应的下标,该下标同时也等于预测的分类
total += labels.size(0) 记录测试的数量
correct += (predicted == labels).sum().item() 将预测的分类与实际分类进行对比,进行布尔运算,得到预测正确的数量。
if __name__ == '__main__':
for epoch in range(10):
train(epoch)
test()
x1 = range(0, 10)
x2 = range(0, 30)
y1 = Accuracy_list
y2 = Loss_list
plt.subplot(2, 1, 1)
plt.plot(x1, y1, 'o-')
plt.title('Test accuracy vs. epoches')
plt.ylabel('Test accuracy')
plt.subplot(2, 1, 2)
plt.plot(x2, y2, '.-')
plt.xlabel('Test loss vs. epoches')
plt.ylabel('Test loss')
plt.show()
进行循环训练测试,并对loss和acc变化进行绘制
完整代码
import torch
from torchvision import transforms
from torchvision import datasets
from torch.utils.data import DataLoader
import torch.nn.functional as F
import torch.optim as optim
import matplotlib.pyplot as plt
# prepare dataset
batch_size = 64
transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))])
train_dataset = datasets.MNIST(
root='../dataset/mnist/', train=True, download=True, transform=transform)
train_loader = DataLoader(train_dataset, shuffle=True, batch_size=batch_size)
test_dataset = datasets.MNIST(
root='../dataset/mnist/', train=False, download=True, transform=transform)
test_loader = DataLoader(test_dataset, shuffle=False, batch_size=batch_size)
# design model using class
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = torch.nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = torch.nn.Conv2d(10, 20, kernel_size=5)
self.pooling = torch.nn.MaxPool2d(2)
self.fc = torch.nn.Linear(320, 10)
def forward(self, x):
# flatten data from (n,1,28,28) to (n, 784)
batch_size = x.size(0)
x = F.relu(self.pooling(self.conv1(x)))
x = F.relu(self.pooling(self.conv2(x)))
x = x.view(batch_size, -1) # -1 此处自动算出的是320
x = self.fc(x)
return x
model = Net()
# 模型迁移到gpu
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device)
# construct loss and optimizer
criterion = torch.nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5) # SGD原理
# training cycle forward, backward, update
Loss_list = []
Accuracy_list = []
def train(epoch):
running_loss = 0.0
for batch_idx, data in enumerate(train_loader, 0):
inputs, target = data
# 输入输出放到和模型同一块显卡上
inputs, target = inputs.to(device), target.to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, target)
loss.backward()
optimizer.step()
running_loss += loss.item()
if batch_idx % 300 == 299:
print('[%d, %5d] loss: %.3f' %
(epoch+1, batch_idx+1, running_loss/300))
Loss_list.append(running_loss / 300)
running_loss = 0.0
def test():
correct = 0
total = 0
with torch.no_grad():
for data in test_loader:
images, labels = data
# 输入输出放到和模型同一块显卡上
images, labels = images.to(device), labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, dim=1)
total += labels.size(0) # labels.shape
correct += (predicted == labels).sum().item()
print('accuracy on test set: %d %% ' % (100*correct/total))
Accuracy_list.append(100 * correct/total)
if __name__ == '__main__':
for epoch in range(10):
train(epoch)
test()
x1 = range(0, 10)
x2 = range(0, 30)
y1 = Accuracy_list
y2 = Loss_list
plt.subplot(2, 1, 1)
plt.plot(x1, y1, 'o-')
plt.title('Test accuracy vs. epoches')
plt.ylabel('Test accuracy')
plt.subplot(2, 1, 2)
plt.plot(x2, y2, '.-')
plt.xlabel('Test loss vs. epoches')
plt.ylabel('Test loss')
plt.show()