Python 官方文档:入门教程 => 点击学习
目录一、PyTorch是什么?二、程序示例1.引入必要库2.下载数据集3.加载数据集4.搭建CNN模型并实例化5.交叉熵损失函数损失函数及SGD算法优化器6.训练函数7.测试函数8.
前言:
本篇文章基于卷积神经网络CNN,使用PyTorch实现MNIST数据集手写数字识别。
PyTorch 是一个 Torch7 团队开源的 Python 优先的深度学习框架,提供两个高级功能:
你可以重用你喜欢的 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实现MNIST数据集手写数字识别详情的文章就介绍到这了,更多相关PyTorch MNIST 内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: PyTorch实现MNIST数据集手写数字识别详情
本文链接: https://lsjlt.com/news/120304.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0