一个基于MNIST数据集的简单卷积神经网络案例
import torch
from torch import nn, optim
import torch.nn.functional as F
from torch.utils.data import DataLoader
from torchvision import datasets,transforms
import numpy as np
import matplotlib.pyplot as plt
input_size = 28
num_classes = 10 #有多少类
num_epochs = 3 #循环训练数据多少次
batch_size = 64 #一次训练多少张图片
# 训练集
train_dataset = datasets.MNIST(root="./data",train=True,transform =transforms.ToTensor(),
download=True)
# 测试集
test_dataset = datasets.MNIST(root="./data",train=False,transform =transforms.ToTensor(),
download=True)
# 构建batch数据
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,batch_size=batch_size,shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,batch_size=batch_size,shuffle=True)
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__() # 输入大小(1,28,28)
self.conv1 = nn.Sequential(
# H =(h - kernel + 2padding)/stride +1
# W =(w - kernel + 2padding)/stride +1
# 要使卷积后大小不变,if stride =1,那么 padding =(kernel-1)/2
nn.Conv2d(in_channels=1,out_channels=16,kernel_size=5,stride=1,padding=2), #(16,28,28)
nn.ReLU(),
nn.MaxPool2d(kernel_size=2) # (16,14,14)
)
self.conv2 =nn.Sequential(
nn.Conv2d(16,32,5,1,2), # (32,14,14)
nn.ReLU(),
nn.MaxPool2d(2) # (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) # flatten操作,结果为(batch_size,32*7*7),可以替换为 x =torch.flatten(x)
output = self.out(x)
return output # 全连接层得到的结果
def accuracy(predicitions , labels):
pred = torch.max(predicitions.data,1)[1] #返回两个列表,第一个列表为最大值,第二个为标签,纵向比较最大值
''' 类似 .argmax(1) '''
'''
>>> a = torch.randn(4, 4)
>>> a
tensor([[-1.2360, -0.2942, -0.1222, 0.8475],
[ 1.1949, -1.1127, -2.2379, -0.6702],
[ 1.5717, -0.9207, 0.1297, -1.8768],
[-0.6172, 1.0036, -0.6060, -0.2432]])
>>> torch.max(a, 1)
torch.return_types.max(values=tensor([0.8475, 1.1949, 1.5717, 1.0036]), indices=tensor([3, 0, 0, 1]))
'''
rights = pred.eq(labels.data.view_as(pred)).sum()
return rights,len(labels) # 返回正确的个数,总数量
# 实例化
net = CNN()
# 损失函数
criterion = nn.CrossEntropyLoss()
# 优化器
optimizer = optim.Adam(net.parameters(),lr=0.001) # 定义优化器,普通的随机梯度下降算法
# 开始训练循环
for epoch in range(num_epochs):
# 将当前epoch的结果保存下来
train_rights = []
for batch_idx , (data,target) in enumerate(train_loader): #针对容器中的每一个批进行循环
net.train()
output = net(data)
loss = criterion(output,target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
right = accuracy(output,target)
train_rights.append(right)
if batch_idx % 100 ==0:
net.eval()
val_rights = []
for (data,target) in test_loader:
output = net(data)
right = accuracy(output,target)
val_rights.append(right)
#准确率计算
train_r = (sum([tup[0] for tup in train_rights]),sum([tup[1] for tup in train_rights]))
val_r = (sum([tup[0] for tup in val_rights]),sum([tup[1] for tup in val_rights]))
print('当前epoch:{}[{}/{} ({:.0f}%)]\t损失:{:.6f}\t训练集准确率:{:.2f}%\t测试集正确率:{:.2f}%'.format(
epoch,
batch_idx * batch_size ,
len(train_dataset),
100. * batch_idx / len(train_loader),
loss.data,
100. * train_r[0].numpy() /train_r[1],
100. * val_r[0].numpy() / val_r[1]
))