PyTorch实现MNIST数据集手写数字识别

一、环境

OS:Ubuntu 18.04
Environment: PyTorch&Anaconda3
Editor: Spyder

二、代码部分

代码主体来自官方Demo,有一些根据我机器配置情况做的小改动
运行这段代码可能花费一些时间
代码中有关归一化的部分,请参考数据归一化常用的两种方法
解决下载数据集慢请参考PyTorch中MNIST数据集(附datasets.MNIST离线包)下载慢/安装慢的解决方案
其余异常问题请参考博文列表。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@ubuntu 18.04
@spyder editor
@author: ftimes
"""

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms

BATCH_SIZE=256 #大概需要1G的显存
EPOCHS=40# 总共训练批次
DEVICE = torch.device("cuda") 
# 个人建议,有GPU尽量用GPU,CPU可能会慢很多


train_loader = torch.utils.data.DataLoader(
        datasets.MNIST('data', train=True, download=True, 
                       transform=transforms.Compose([
                           transforms.ToTensor(),
                           transforms.Normalize((0.1307,), (0.3081,))
                       ])),
        batch_size=BATCH_SIZE, shuffle=True)




test_loader = torch.utils.data.DataLoader(
        datasets.MNIST('data', train=False, transform=transforms.Compose([
                           transforms.ToTensor(),
                           transforms.Normalize((0.1307,), (0.3081,))
                       ])),
        batch_size=BATCH_SIZE, shuffle=True)




class ConvNet(nn.Module):
    def __init__(self):
        super().__init__()
        # batch*1*28*28(每次会送入batch个样本,输入通道数1(黑白图像),图像分辨率是28x28)
        # 下面的卷积层Conv2d的第一个参数指输入通道数,第二个参数指输出通道数,第三个参数指卷积核的大小
        self.conv1 = nn.Conv2d(1, 10, 5) # 输入通道数1,输出通道数10,核的大小5
        self.conv2 = nn.Conv2d(10, 20, 3) # 输入通道数10,输出通道数20,核的大小3
        # 下面的全连接层Linear的第一个参数指输入通道数,第二个参数指输出通道数
        self.fc1 = nn.Linear(20*10*10, 500) # 输入通道数是2000,输出通道数是500
        self.fc2 = nn.Linear(500, 10) # 输入通道数是500,输出通道数是10,即10分类
    def forward(self,x):
        in_size = x.size(0) # 在本例中in_size=512,也就是BATCH_SIZE的值。输入的x可以看成是512*1*28*28的张量。
        out = self.conv1(x) # batch*1*28*28 -> batch*10*24*24(28x28的图像经过一次核为5x5的卷积,输出变为24x24)
        out = F.relu(out) # batch*10*24*24(激活函数ReLU不改变形状))
        out = F.max_pool2d(out, 2, 2) # batch*10*24*24 -> batch*10*12*12(2*2的池化层会减半)
        out = self.conv2(out) # batch*10*12*12 -> batch*20*10*10(再卷积一次,核的大小是3)
        out = F.relu(out) # batch*20*10*10
        out = out.view(in_size, -1) # batch*20*10*10 -> batch*2000(out的第二维是-1,说明是自动推算,本例中第二维是20*10*10)
        out = self.fc1(out) # batch*2000 -> batch*500
        out = F.relu(out) # batch*500
        out = self.fc2(out) # batch*500 -> batch*10
        out = F.log_softmax(out, dim=1) # 计算log(softmax(x))
        return out

model = ConvNet().to(DEVICE)
optimizer = optim.Adam(model.parameters())

def train(model, device, train_loader, optimizer, epoch):
    model.train()
    for batch_idx, (data, target) in enumerate(train_loader):
        data, target = data.to(device), target.to(device)
        optimizer.zero_grad()
        output = model(data)
        loss = F.nll_loss(output, target)
        loss.backward()
        optimizer.step()
        if(batch_idx+1)%30 == 0: 
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch, batch_idx * len(data), len(train_loader.dataset),
                100. * batch_idx / len(train_loader), loss.item()))

def test(model, device, test_loader):
    model.eval()
    test_loss = 0
    correct = 0
    with torch.no_grad():
        for data, target in test_loader:
            data, target = data.to(device), target.to(device)
            output = model(data)
            test_loss += F.nll_loss(output, target, reduction='sum').item() # 将一批的损失相加
            pred = output.max(1, keepdim=True)[1] # 找到概率最大的下标
            correct += pred.eq(target.view_as(pred)).sum().item()

    test_loss /= len(test_loader.dataset)
    print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
        test_loss, correct, len(test_loader.dataset),
        100. * correct / len(test_loader.dataset)))

for epoch in range(1, EPOCHS + 1):
    train(model, DEVICE, train_loader, optimizer, epoch)
    test(model, DEVICE, test_loader)

三、示例

通过下面的输出我们可以看出,在第6批就基本上是100%了,这应该得益于我安装的版本是1.4.0(官方Demo是基于1.0.0环境下测试的)。

Train Epoch: 1 [7424/45176 (16%)]       Loss: 0.286853
Train Epoch: 1 [15104/45176 (33%)]      Loss: 0.217282
Train Epoch: 1 [22784/45176 (50%)]      Loss: 0.164231
Train Epoch: 1 [30464/45176 (67%)]      Loss: 0.125242
Train Epoch: 1 [38144/45176 (84%)]      Loss: 0.092887

Test set: Average loss: 0.0733, Accuracy: 8759/8952 (98%)

Train Epoch: 2 [7424/45176 (16%)]       Loss: 0.066707
Train Epoch: 2 [15104/45176 (33%)]      Loss: 0.090857
Train Epoch: 2 [22784/45176 (50%)]      Loss: 0.075518
Train Epoch: 2 [30464/45176 (67%)]      Loss: 0.069935
Train Epoch: 2 [38144/45176 (84%)]      Loss: 0.048884

Test set: Average loss: 0.0415, Accuracy: 8845/8952 (99%)

Train Epoch: 3 [7424/45176 (16%)]       Loss: 0.036504
Train Epoch: 3 [15104/45176 (33%)]      Loss: 0.062592
Train Epoch: 3 [22784/45176 (50%)]      Loss: 0.073319
Train Epoch: 3 [30464/45176 (67%)]      Loss: 0.023953
Train Epoch: 3 [38144/45176 (84%)]      Loss: 0.046280

Test set: Average loss: 0.0285, Accuracy: 8877/8952 (99%)

Train Epoch: 4 [7424/45176 (16%)]       Loss: 0.039753
Train Epoch: 4 [15104/45176 (33%)]      Loss: 0.017759
Train Epoch: 4 [22784/45176 (50%)]      Loss: 0.025863
Train Epoch: 4 [30464/45176 (67%)]      Loss: 0.019005
Train Epoch: 4 [38144/45176 (84%)]      Loss: 0.031085

Test set: Average loss: 0.0220, Accuracy: 8889/8952 (99%)

Train Epoch: 5 [7424/45176 (16%)]       Loss: 0.042840
Train Epoch: 5 [15104/45176 (33%)]      Loss: 0.044694
Train Epoch: 5 [22784/45176 (50%)]      Loss: 0.027897
Train Epoch: 5 [30464/45176 (67%)]      Loss: 0.021535
Train Epoch: 5 [38144/45176 (84%)]      Loss: 0.021550

Test set: Average loss: 0.0149, Accuracy: 8919/8952 (100%)

Train Epoch: 6 [7424/45176 (16%)]       Loss: 0.020661
Train Epoch: 6 [15104/45176 (33%)]      Loss: 0.019496
Train Epoch: 6 [22784/45176 (50%)]      Loss: 0.023032
Train Epoch: 6 [30464/45176 (67%)]      Loss: 0.020590
Train Epoch: 6 [38144/45176 (84%)]      Loss: 0.016862

Test set: Average loss: 0.0131, Accuracy: 8915/8952 (100%)

Train Epoch: 7 [7424/45176 (16%)]       Loss: 0.009969
Train Epoch: 7 [15104/45176 (33%)]      Loss: 0.011267
Train Epoch: 7 [22784/45176 (50%)]      Loss: 0.003528
Train Epoch: 7 [30464/45176 (67%)]      Loss: 0.005013
Train Epoch: 7 [38144/45176 (84%)]      Loss: 0.003027

Test set: Average loss: 0.0243, Accuracy: 8885/8952 (99%)

Train Epoch: 8 [7424/45176 (16%)]       Loss: 0.007714
Train Epoch: 8 [15104/45176 (33%)]      Loss: 0.002605
Train Epoch: 8 [22784/45176 (50%)]      Loss: 0.004448
Train Epoch: 8 [30464/45176 (67%)]      Loss: 0.020806
Train Epoch: 8 [38144/45176 (84%)]      Loss: 0.017928

Test set: Average loss: 0.0074, Accuracy: 8928/8952 (100%)

Train Epoch: 9 [7424/45176 (16%)]       Loss: 0.001040
Train Epoch: 9 [15104/45176 (33%)]      Loss: 0.001996
Train Epoch: 9 [22784/45176 (50%)]      Loss: 0.002150
Train Epoch: 9 [30464/45176 (67%)]      Loss: 0.004615
Train Epoch: 9 [38144/45176 (84%)]      Loss: 0.001235

Test set: Average loss: 0.0069, Accuracy: 8936/8952 (100%)

Train Epoch: 10 [7424/45176 (16%)]      Loss: 0.002631
Train Epoch: 10 [15104/45176 (33%)]     Loss: 0.005682
Train Epoch: 10 [22784/45176 (50%)]     Loss: 0.002273
Train Epoch: 10 [30464/45176 (67%)]     Loss: 0.005724
Train Epoch: 10 [38144/45176 (84%)]     Loss: 0.005008

Test set: Average loss: 0.0069, Accuracy: 8933/8952 (100%)

Train Epoch: 11 [7424/45176 (16%)]      Loss: 0.000736
Train Epoch: 11 [15104/45176 (33%)]     Loss: 0.005763
Train Epoch: 11 [22784/45176 (50%)]     Loss: 0.003668
Train Epoch: 11 [30464/45176 (67%)]     Loss: 0.008036
Train Epoch: 11 [38144/45176 (84%)]     Loss: 0.002448

Test set: Average loss: 0.0025, Accuracy: 8946/8952 (100%)

Train Epoch: 12 [7424/45176 (16%)]      Loss: 0.003074
Train Epoch: 12 [15104/45176 (33%)]     Loss: 0.002368
Train Epoch: 12 [22784/45176 (50%)]     Loss: 0.002898
Train Epoch: 12 [30464/45176 (67%)]     Loss: 0.000921
Train Epoch: 12 [38144/45176 (84%)]     Loss: 0.005116

Test set: Average loss: 0.0050, Accuracy: 8937/8952 (100%)

Train Epoch: 13 [7424/45176 (16%)]      Loss: 0.000398
Train Epoch: 13 [15104/45176 (33%)]     Loss: 0.002453
Train Epoch: 13 [22784/45176 (50%)]     Loss: 0.000615
Train Epoch: 13 [30464/45176 (67%)]     Loss: 0.001292
Train Epoch: 13 [38144/45176 (84%)]     Loss: 0.000444

Test set: Average loss: 0.0067, Accuracy: 8937/8952 (100%)

Train Epoch: 14 [7424/45176 (16%)]      Loss: 0.000640
Train Epoch: 14 [15104/45176 (33%)]     Loss: 0.001205
Train Epoch: 14 [22784/45176 (50%)]     Loss: 0.018630
Train Epoch: 14 [30464/45176 (67%)]     Loss: 0.000680
Train Epoch: 14 [38144/45176 (84%)]     Loss: 0.016817

Test set: Average loss: 0.0039, Accuracy: 8937/8952 (100%)

Train Epoch: 15 [7424/45176 (16%)]      Loss: 0.009537
Train Epoch: 15 [15104/45176 (33%)]     Loss: 0.007632
Train Epoch: 15 [22784/45176 (50%)]     Loss: 0.001453
Train Epoch: 15 [30464/45176 (67%)]     Loss: 0.011552
Train Epoch: 15 [38144/45176 (84%)]     Loss: 0.002835

Test set: Average loss: 0.0030, Accuracy: 8947/8952 (100%)

Train Epoch: 16 [7424/45176 (16%)]      Loss: 0.004266
Train Epoch: 16 [15104/45176 (33%)]     Loss: 0.000700
Train Epoch: 16 [22784/45176 (50%)]     Loss: 0.000510
Train Epoch: 16 [30464/45176 (67%)]     Loss: 0.006810
Train Epoch: 16 [38144/45176 (84%)]     Loss: 0.001845

Test set: Average loss: 0.0016, Accuracy: 8949/8952 (100%)

Train Epoch: 17 [7424/45176 (16%)]      Loss: 0.000803
Train Epoch: 17 [15104/45176 (33%)]     Loss: 0.001625
Train Epoch: 17 [22784/45176 (50%)]     Loss: 0.001512
Train Epoch: 17 [30464/45176 (67%)]     Loss: 0.000789
Train Epoch: 17 [38144/45176 (84%)]     Loss: 0.001724

Test set: Average loss: 0.0009, Accuracy: 8950/8952 (100%)

Train Epoch: 18 [7424/45176 (16%)]      Loss: 0.010568
Train Epoch: 18 [15104/45176 (33%)]     Loss: 0.008273
Train Epoch: 18 [22784/45176 (50%)]     Loss: 0.001384
Train Epoch: 18 [30464/45176 (67%)]     Loss: 0.000414
Train Epoch: 18 [38144/45176 (84%)]     Loss: 0.016485

Test set: Average loss: 0.0012, Accuracy: 8951/8952 (100%)

Train Epoch: 19 [7424/45176 (16%)]      Loss: 0.000633
Train Epoch: 19 [15104/45176 (33%)]     Loss: 0.000268
Train Epoch: 19 [22784/45176 (50%)]     Loss: 0.004634
Train Epoch: 19 [30464/45176 (67%)]     Loss: 0.005396
Train Epoch: 19 [38144/45176 (84%)]     Loss: 0.001612

Test set: Average loss: 0.0009, Accuracy: 8950/8952 (100%)

Train Epoch: 20 [7424/45176 (16%)]      Loss: 0.000166
Train Epoch: 20 [15104/45176 (33%)]     Loss: 0.000435
Train Epoch: 20 [22784/45176 (50%)]     Loss: 0.000344
Train Epoch: 20 [30464/45176 (67%)]     Loss: 0.001667
Train Epoch: 20 [38144/45176 (84%)]     Loss: 0.000567

Test set: Average loss: 0.0032, Accuracy: 8943/8952 (100%)

Train Epoch: 21 [7424/45176 (16%)]      Loss: 0.015932
Train Epoch: 21 [15104/45176 (33%)]     Loss: 0.001688
Train Epoch: 21 [22784/45176 (50%)]     Loss: 0.000212
Train Epoch: 21 [30464/45176 (67%)]     Loss: 0.001248
Train Epoch: 21 [38144/45176 (84%)]     Loss: 0.008681

Test set: Average loss: 0.0015, Accuracy: 8946/8952 (100%)

Train Epoch: 22 [7424/45176 (16%)]      Loss: 0.004317
Train Epoch: 22 [15104/45176 (33%)]     Loss: 0.000227
Train Epoch: 22 [22784/45176 (50%)]     Loss: 0.000362
Train Epoch: 22 [30464/45176 (67%)]     Loss: 0.000808
Train Epoch: 22 [38144/45176 (84%)]     Loss: 0.000203

Test set: Average loss: 0.0102, Accuracy: 8919/8952 (100%)

Train Epoch: 23 [7424/45176 (16%)]      Loss: 0.000470
Train Epoch: 23 [15104/45176 (33%)]     Loss: 0.005329
Train Epoch: 23 [22784/45176 (50%)]     Loss: 0.017705
Train Epoch: 23 [30464/45176 (67%)]     Loss: 0.025205
Train Epoch: 23 [38144/45176 (84%)]     Loss: 0.001242

Test set: Average loss: 0.0036, Accuracy: 8942/8952 (100%)

Train Epoch: 24 [7424/45176 (16%)]      Loss: 0.001726
Train Epoch: 24 [15104/45176 (33%)]     Loss: 0.000334
Train Epoch: 24 [22784/45176 (50%)]     Loss: 0.000237
Train Epoch: 24 [30464/45176 (67%)]     Loss: 0.000577
Train Epoch: 24 [38144/45176 (84%)]     Loss: 0.001294

Test set: Average loss: 0.0013, Accuracy: 8945/8952 (100%)

Train Epoch: 25 [7424/45176 (16%)]      Loss: 0.000633
Train Epoch: 25 [15104/45176 (33%)]     Loss: 0.005084
Train Epoch: 25 [22784/45176 (50%)]     Loss: 0.000940
Train Epoch: 25 [30464/45176 (67%)]     Loss: 0.000348
Train Epoch: 25 [38144/45176 (84%)]     Loss: 0.036278

Test set: Average loss: 0.0029, Accuracy: 8944/8952 (100%)

Train Epoch: 26 [7424/45176 (16%)]      Loss: 0.000074
Train Epoch: 26 [15104/45176 (33%)]     Loss: 0.000362
Train Epoch: 26 [22784/45176 (50%)]     Loss: 0.000230
Train Epoch: 26 [30464/45176 (67%)]     Loss: 0.000011
Train Epoch: 26 [38144/45176 (84%)]     Loss: 0.000016

Test set: Average loss: 0.0009, Accuracy: 8951/8952 (100%)

Train Epoch: 27 [7424/45176 (16%)]      Loss: 0.001869
Train Epoch: 27 [15104/45176 (33%)]     Loss: 0.000115
Train Epoch: 27 [22784/45176 (50%)]     Loss: 0.000011
Train Epoch: 27 [30464/45176 (67%)]     Loss: 0.000203
Train Epoch: 27 [38144/45176 (84%)]     Loss: 0.000032

Test set: Average loss: 0.0001, Accuracy: 8952/8952 (100%)

Train Epoch: 28 [7424/45176 (16%)]      Loss: 0.000068
Train Epoch: 28 [15104/45176 (33%)]     Loss: 0.000016
Train Epoch: 28 [22784/45176 (50%)]     Loss: 0.000021
Train Epoch: 28 [30464/45176 (67%)]     Loss: 0.000120
Train Epoch: 28 [38144/45176 (84%)]     Loss: 0.000012

Test set: Average loss: 0.0028, Accuracy: 8942/8952 (100%)

Train Epoch: 29 [7424/45176 (16%)]      Loss: 0.000057
Train Epoch: 29 [15104/45176 (33%)]     Loss: 0.000062
Train Epoch: 29 [22784/45176 (50%)]     Loss: 0.000040
Train Epoch: 29 [30464/45176 (67%)]     Loss: 0.002825
Train Epoch: 29 [38144/45176 (84%)]     Loss: 0.000208

Test set: Average loss: 0.0001, Accuracy: 8952/8952 (100%)

Train Epoch: 30 [7424/45176 (16%)]      Loss: 0.000005
Train Epoch: 30 [15104/45176 (33%)]     Loss: 0.000686
Train Epoch: 30 [22784/45176 (50%)]     Loss: 0.001243
Train Epoch: 30 [30464/45176 (67%)]     Loss: 0.000206
Train Epoch: 30 [38144/45176 (84%)]     Loss: 0.000722

Test set: Average loss: 0.0125, Accuracy: 8914/8952 (100%)

Train Epoch: 31 [7424/45176 (16%)]      Loss: 0.002555
Train Epoch: 31 [15104/45176 (33%)]     Loss: 0.000763
Train Epoch: 31 [22784/45176 (50%)]     Loss: 0.003229
Train Epoch: 31 [30464/45176 (67%)]     Loss: 0.019716
Train Epoch: 31 [38144/45176 (84%)]     Loss: 0.013709

Test set: Average loss: 0.0043, Accuracy: 8940/8952 (100%)

Train Epoch: 32 [7424/45176 (16%)]      Loss: 0.018892
Train Epoch: 32 [15104/45176 (33%)]     Loss: 0.000457
Train Epoch: 32 [22784/45176 (50%)]     Loss: 0.001805
Train Epoch: 32 [30464/45176 (67%)]     Loss: 0.000281
Train Epoch: 32 [38144/45176 (84%)]     Loss: 0.000371

Test set: Average loss: 0.0022, Accuracy: 8945/8952 (100%)

Train Epoch: 33 [7424/45176 (16%)]      Loss: 0.000209
Train Epoch: 33 [15104/45176 (33%)]     Loss: 0.000011
Train Epoch: 33 [22784/45176 (50%)]     Loss: 0.000028
Train Epoch: 33 [30464/45176 (67%)]     Loss: 0.000031
Train Epoch: 33 [38144/45176 (84%)]     Loss: 0.000929

Test set: Average loss: 0.0001, Accuracy: 8952/8952 (100%)

Train Epoch: 34 [7424/45176 (16%)]      Loss: 0.000032
Train Epoch: 34 [15104/45176 (33%)]     Loss: 0.000014
Train Epoch: 34 [22784/45176 (50%)]     Loss: 0.000037
Train Epoch: 34 [30464/45176 (67%)]     Loss: 0.000078
Train Epoch: 34 [38144/45176 (84%)]     Loss: 0.000029

Test set: Average loss: 0.0000, Accuracy: 8952/8952 (100%)

Train Epoch: 35 [7424/45176 (16%)]      Loss: 0.000003
Train Epoch: 35 [15104/45176 (33%)]     Loss: 0.000030
Train Epoch: 35 [22784/45176 (50%)]     Loss: 0.000016
Train Epoch: 35 [30464/45176 (67%)]     Loss: 0.000027
Train Epoch: 35 [38144/45176 (84%)]     Loss: 0.000010

Test set: Average loss: 0.0000, Accuracy: 8952/8952 (100%)

Train Epoch: 36 [7424/45176 (16%)]      Loss: 0.000004
Train Epoch: 36 [15104/45176 (33%)]     Loss: 0.000014
Train Epoch: 36 [22784/45176 (50%)]     Loss: 0.000010
Train Epoch: 36 [30464/45176 (67%)]     Loss: 0.000011
Train Epoch: 36 [38144/45176 (84%)]     Loss: 0.000019

Test set: Average loss: 0.0000, Accuracy: 8952/8952 (100%)

Train Epoch: 37 [7424/45176 (16%)]      Loss: 0.000007
Train Epoch: 37 [15104/45176 (33%)]     Loss: 0.000031
Train Epoch: 37 [22784/45176 (50%)]     Loss: 0.000012
Train Epoch: 37 [30464/45176 (67%)]     Loss: 0.000010
Train Epoch: 37 [38144/45176 (84%)]     Loss: 0.000023

Test set: Average loss: 0.0000, Accuracy: 8952/8952 (100%)

Train Epoch: 38 [7424/45176 (16%)]      Loss: 0.000026
Train Epoch: 38 [15104/45176 (33%)]     Loss: 0.000017
Train Epoch: 38 [22784/45176 (50%)]     Loss: 0.000003
Train Epoch: 38 [30464/45176 (67%)]     Loss: 0.000007
Train Epoch: 38 [38144/45176 (84%)]     Loss: 0.000012

Test set: Average loss: 0.0000, Accuracy: 8952/8952 (100%)

Train Epoch: 39 [7424/45176 (16%)]      Loss: 0.000005
Train Epoch: 39 [15104/45176 (33%)]     Loss: 0.000003
Train Epoch: 39 [22784/45176 (50%)]     Loss: 0.000001
Train Epoch: 39 [30464/45176 (67%)]     Loss: 0.000009
Train Epoch: 39 [38144/45176 (84%)]     Loss: 0.000007

Test set: Average loss: 0.0000, Accuracy: 8952/8952 (100%)

Train Epoch: 40 [7424/45176 (16%)]      Loss: 0.000005
Train Epoch: 40 [15104/45176 (33%)]     Loss: 0.000012
Train Epoch: 40 [22784/45176 (50%)]     Loss: 0.000008
Train Epoch: 40 [30464/45176 (67%)]     Loss: 0.000004
Train Epoch: 40 [38144/45176 (84%)]     Loss: 0.000010

Test set: Average loss: 0.0000, Accuracy: 8952/8952 (100%)

你可能感兴趣的:(Python)