MNIST 包括6万张28x28的训练样本,1万张测试样本。Pytorch里面包含了MNIST的数据集,直接使用即可。
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=512 #大概需要2G的显存
EPOCHS=20 # 总共训练批次
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu") # 让torch判断是否使用GPU,建议使用GPU环境,因为会快很多
# 读取数据
# 训练集数据
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) # 将网络放到GPU设备上
optimizer = optim.Adam(model.parameters()) # 优化器我们也直接选择简单暴力的Adam
# 训练函数
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)))
# 开始训练
# 每一个epoch,查看一次trian数据集和test数据集的精度。
for epoch in range(1, EPOCHS + 1):
train(model, DEVICE, train_loader, optimizer, epoch)
test(model, DEVICE, test_loader)
结果:
Train Epoch: 1 [14848/60000 (25%)] Loss: 0.272529
Train Epoch: 1 [30208/60000 (50%)] Loss: 0.235455
Train Epoch: 1 [45568/60000 (75%)] Loss: 0.101858
Test set: Average loss: 0.1018, Accuracy: 9695/10000 (97%)
Train Epoch: 2 [14848/60000 (25%)] Loss: 0.057989
Train Epoch: 2 [30208/60000 (50%)] Loss: 0.083935
Train Epoch: 2 [45568/60000 (75%)] Loss: 0.051921
Test set: Average loss: 0.0523, Accuracy: 9825/10000 (98%)
Train Epoch: 3 [14848/60000 (25%)] Loss: 0.045383
Train Epoch: 3 [30208/60000 (50%)] Loss: 0.049402
Train Epoch: 3 [45568/60000 (75%)] Loss: 0.061366
Test set: Average loss: 0.0408, Accuracy: 9866/10000 (99%)
Train Epoch: 4 [14848/60000 (25%)] Loss: 0.035253
Train Epoch: 4 [30208/60000 (50%)] Loss: 0.038444
Train Epoch: 4 [45568/60000 (75%)] Loss: 0.036877
Test set: Average loss: 0.0433, Accuracy: 9859/10000 (99%)
Train Epoch: 5 [14848/60000 (25%)] Loss: 0.038996
Train Epoch: 5 [30208/60000 (50%)] Loss: 0.020670
Train Epoch: 5 [45568/60000 (75%)] Loss: 0.034658
Test set: Average loss: 0.0339, Accuracy: 9885/10000 (99%)
Train Epoch: 6 [14848/60000 (25%)] Loss: 0.067320
Train Epoch: 6 [30208/60000 (50%)] Loss: 0.016328
Train Epoch: 6 [45568/60000 (75%)] Loss: 0.017037
Test set: Average loss: 0.0348, Accuracy: 9881/10000 (99%)
Train Epoch: 7 [14848/60000 (25%)] Loss: 0.022150
Train Epoch: 7 [30208/60000 (50%)] Loss: 0.009608
Train Epoch: 7 [45568/60000 (75%)] Loss: 0.012742
Test set: Average loss: 0.0346, Accuracy: 9895/10000 (99%)
Train Epoch: 8 [14848/60000 (25%)] Loss: 0.010173
Train Epoch: 8 [30208/60000 (50%)] Loss: 0.019482
Train Epoch: 8 [45568/60000 (75%)] Loss: 0.012159
Test set: Average loss: 0.0323, Accuracy: 9886/10000 (99%)
Train Epoch: 9 [14848/60000 (25%)] Loss: 0.007792
Train Epoch: 9 [30208/60000 (50%)] Loss: 0.006970
Train Epoch: 9 [45568/60000 (75%)] Loss: 0.004989
Test set: Average loss: 0.0294, Accuracy: 9909/10000 (99%)
Train Epoch: 10 [14848/60000 (25%)] Loss: 0.003764
Train Epoch: 10 [30208/60000 (50%)] Loss: 0.005944
Train Epoch: 10 [45568/60000 (75%)] Loss: 0.001866
Test set: Average loss: 0.0361, Accuracy: 9902/10000 (99%)
Train Epoch: 11 [14848/60000 (25%)] Loss: 0.002737
Train Epoch: 11 [30208/60000 (50%)] Loss: 0.014134
Train Epoch: 11 [45568/60000 (75%)] Loss: 0.001365
Test set: Average loss: 0.0309, Accuracy: 9905/10000 (99%)
Train Epoch: 12 [14848/60000 (25%)] Loss: 0.003344
Train Epoch: 12 [30208/60000 (50%)] Loss: 0.003090
Train Epoch: 12 [45568/60000 (75%)] Loss: 0.004847
Test set: Average loss: 0.0318, Accuracy: 9902/10000 (99%)
Train Epoch: 13 [14848/60000 (25%)] Loss: 0.001278
Train Epoch: 13 [30208/60000 (50%)] Loss: 0.003016
Train Epoch: 13 [45568/60000 (75%)] Loss: 0.001328
Test set: Average loss: 0.0358, Accuracy: 9906/10000 (99%)
Train Epoch: 14 [14848/60000 (25%)] Loss: 0.002219
Train Epoch: 14 [30208/60000 (50%)] Loss: 0.003487
Train Epoch: 14 [45568/60000 (75%)] Loss: 0.014429
Test set: Average loss: 0.0376, Accuracy: 9896/10000 (99%)
Train Epoch: 15 [14848/60000 (25%)] Loss: 0.003042
Train Epoch: 15 [30208/60000 (50%)] Loss: 0.002974
Train Epoch: 15 [45568/60000 (75%)] Loss: 0.000871
Test set: Average loss: 0.0346, Accuracy: 9909/10000 (99%)
Train Epoch: 16 [14848/60000 (25%)] Loss: 0.000618
Train Epoch: 16 [30208/60000 (50%)] Loss: 0.003164
Train Epoch: 16 [45568/60000 (75%)] Loss: 0.007245
Test set: Average loss: 0.0357, Accuracy: 9905/10000 (99%)
Train Epoch: 17 [14848/60000 (25%)] Loss: 0.001874
Train Epoch: 17 [30208/60000 (50%)] Loss: 0.013951
Train Epoch: 17 [45568/60000 (75%)] Loss: 0.000729
Test set: Average loss: 0.0322, Accuracy: 9922/10000 (99%)
Train Epoch: 18 [14848/60000 (25%)] Loss: 0.002581
Train Epoch: 18 [30208/60000 (50%)] Loss: 0.001396
Train Epoch: 18 [45568/60000 (75%)] Loss: 0.015521
Test set: Average loss: 0.0389, Accuracy: 9914/10000 (99%)
Train Epoch: 19 [14848/60000 (25%)] Loss: 0.000283
Train Epoch: 19 [30208/60000 (50%)] Loss: 0.001385
Train Epoch: 19 [45568/60000 (75%)] Loss: 0.011184
Test set: Average loss: 0.0383, Accuracy: 9901/10000 (99%)
Train Epoch: 20 [14848/60000 (25%)] Loss: 0.000472
Train Epoch: 20 [30208/60000 (50%)] Loss: 0.003306
Train Epoch: 20 [45568/60000 (75%)] Loss: 0.018017
Test set: Average loss: 0.0393, Accuracy: 9899/10000 (99%)
注:本文来自github上的教程https://github.com/zergtant/pytorch-handbook/blob/master/chapter3/3.2-mnist.ipynb