用pytorch-python通过cnn实现mnist手写字体的识别。
程序:
import torch
import torch.nn as nn
import torch.utils.data as data
import torchvision
import matplotlib.pyplot as plt
# Hyper Parameter
EPOCH = 1 # 训练整批数据的次数
BATCH_SIZE = 50 # 批训练的数据数量
LR = 0.001 # 学习率
DOWNLOAD_MNIST = False # 如果下载好了mnist数据库就写False
# mnist手写数字
train_data = torchvision.datasets.MNIST(
root='./mnist/', # 保存或提取的位置
train=True, # 是否为训练数据
transform=torchvision.transforms.ToTensor(), # 转换plt.image or numpy.ndarray 成torch.FloatTensor(C*H*W),训练时normalize成[0.0, 0.1]区间
download=DOWNLOAD_MNIST # 是否为下载好
)
test_data = torchvision.datasets.MNIST(root='./mnist', train=False)
# 批训练 50 samples,1 channel, 28*28 (50,1,28,28)
train_loader = data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)
# 为了节约时间, 只测试前2000个
test_x = torch.unsqueeze(test_data.test_data, dim=1).type(torch.FloatTensor)[:2000]/255. # 把[0,255]转换成[0.0,1.0]
test_y = test_data.test_labels[:2000]
# 创建cnn网络
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Sequential( # input shape (1,28,28)
nn.Conv2d(
in_channels=1, # 输入数据的通道数(channels)
out_channels=16, # 输出数据的通道数(channels)
kernel_size=5, # 卷积核的大小
stride=1, # 步长
padding=2, # 外部填充, padding = (kernel_size - 1) / 2
), # output shape (16,28,28)
nn.ReLU(), # 激活函数
nn.MaxPool2d(kernel_size=2) # 池化层,向下采样步长为2 # output shape (16,14,14)
)
self.conv2 = nn.Sequential( # input shape (16,14,14)
nn.Conv2d(16, 32, 5, 1, 2), # output shape (32,14,14)
nn.ReLU(),
nn.MaxPool2d(2) # output shape (32,7,7)
)
self.out = nn.Linear(32*7*7, 10) # 输出层为全连接层
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = x.view(x.size(0), -1)
output = self.out(x)
return output
cnn = CNN()
# 训练
optimizer = torch.optim.Adam(cnn.parameters(), lr=LR)
loss_func = nn.CrossEntropyLoss()
for epoch in range(EPOCH):
for step, (b_x, b_y) in enumerate(train_loader):
output = cnn(b_x)
loss = loss_func(output, b_y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if step % 50 == 0:
test_output = cnn(test_x)
pred_y = torch.max(test_output, 1)[1].data.squeeze()
accuracy = (sum(pred_y == test_y)).numpy() / test_y.size(0)
print('step:', step, 'accuracy: %.2f' % accuracy)
结果: