本篇文章基于卷积神经网络CNN,使用PyTorch实现MNIST数据集手写数字识别。
PyTorch 是一个 Torch7 团队开源的 Python 优先的深度学习框架,提供两个高级功能:
强大的 GPU 加速 Tensor 计算(类似 numpy)
构建基于 tape 的自动升级系统上的深度神经网络
你可以重用你喜欢的 python 包,如 numpy、scipy 和 Cython ,在需要时扩展 PyTorch。
下面案例可供运行参考
import torchvision
import torch
from torch.utils.data import DataLoader
import torch.nn.functional as F
这里设置download=True,将会自动下载数据集,并存储在./data文件夹。
train_data = torchvision.datasets.MNIST(root="./data",train=True,transform=torchvision.transforms.ToTensor(),download=True)
test_data = torchvision.datasets.MNIST(root="./data",train=False,transform=torchvision.transforms.ToTensor(),download=True)
batch_size=32表示每一个batch中包含32张手写数字图片,shuffle=True表示打乱测试集(data和target仍一一对应)
train_loader = DataLoader(train_data,batch_size=32,shuffle=True)
test_loader = DataLoader(test_data,batch_size=32,shuffle=False)
class Net(torch.nn.Module):
def __init__(self):
super(Net,self).__init__()
self.con1 = torch.nn.Conv2d(1,10,kernel_size=5)
self.con2 = 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):
batch_size = x.size(0)
x = F.relu(self.pooling(self.con1(x)))
x = F.relu(self.pooling(self.con2(x)))
x = x.view(batch_size,-1)
x = self.fc(x)
return x
#模型实例化
model = Net()
lossfun = torch.nn.CrossEntropyLoss()
opt = torch.optim.SGD(model.parameters(),lr=0.01,momentum=0.5)
def train(epoch):
running_loss = 0.0
for i,(inputs,targets) in enumerate(train_loader,0):
# inputs,targets = inputs.to(device),targets.to(device)
opt.zero_grad()
outputs = model(inputs)
loss = lossfun(outputs,targets)
loss.backward()
opt.step()
running_loss += loss.item()
if i % 300 == 299:
print('[%d,%d] loss:%.3f' % (epoch+1,i+1,running_loss/300))
running_loss = 0.0
def test():
total = 0
correct = 0
with torch.no_grad():
for (inputs,targets) in test_loader:
# inputs, targets = inputs.to(device), targets.to(device)
outputs = model(inputs)
_,predicted = torch.max(outputs.data,dim=1)
total += targets.size(0)
correct += (predicted == targets).sum().item()
print(100*correct/total)
if __name__ == '__main__':
for epoch in range(20):
train(epoch)
test()
本文仅仅简单介绍了PyTorch在卷积神经网络中的使用,希望我的分享能对你有所帮助。