Pytorch實(shí)現(xiàn)的手寫數(shù)字mnist識(shí)別功能完整示例
本文實(shí)例講述了Pytorch實(shí)現(xiàn)的手寫數(shù)字mnist識(shí)別功能。分享給大家供大家參考,具體如下:
import torch import torchvision as tv import torchvision.transforms as transforms import torch.nn as nn import torch.optim as optim import argparse # 定義是否使用GPU device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # 定義網(wǎng)絡(luò)結(jié)構(gòu) class LeNet(nn.Module): def __init__(self): super(LeNet, self).__init__() self.conv1 = nn.Sequential( #input_size=(1*28*28) nn.Conv2d(1, 6, 5, 1, 2), #padding=2保證輸入輸出尺寸相同 nn.ReLU(), #input_size=(6*28*28) nn.MaxPool2d(kernel_size=2, stride=2),#output_size=(6*14*14) ) self.conv2 = nn.Sequential( nn.Conv2d(6, 16, 5), nn.ReLU(), #input_size=(16*10*10) nn.MaxPool2d(2, 2) #output_size=(16*5*5) ) self.fc1 = nn.Sequential( nn.Linear(16 * 5 * 5, 120), nn.ReLU() ) self.fc2 = nn.Sequential( nn.Linear(120, 84), nn.ReLU() ) self.fc3 = nn.Linear(84, 10) # 定義前向傳播過程,輸入為x def forward(self, x): x = self.conv1(x) x = self.conv2(x) # nn.Linear()的輸入輸出都是維度為一的值,所以要把多維度的tensor展平成一維 x = x.view(x.size()[0], -1) x = self.fc1(x) x = self.fc2(x) x = self.fc3(x) return x #使得我們能夠手動(dòng)輸入命令行參數(shù),就是讓風(fēng)格變得和Linux命令行差不多 parser = argparse.ArgumentParser() parser.add_argument('--outf', default='./model/', help='folder to output images and model checkpoints') #模型保存路徑 parser.add_argument('--net', default='./model/net.pth', help="path to netG (to continue training)") #模型加載路徑 opt = parser.parse_args() # 超參數(shù)設(shè)置 EPOCH = 8 #遍歷數(shù)據(jù)集次數(shù) BATCH_SIZE = 64 #批處理尺寸(batch_size) LR = 0.001 #學(xué)習(xí)率 # 定義數(shù)據(jù)預(yù)處理方式 transform = transforms.ToTensor() # 定義訓(xùn)練數(shù)據(jù)集 trainset = tv.datasets.MNIST( root='./data/', train=True, download=True, transform=transform) # 定義訓(xùn)練批處理數(shù)據(jù) trainloader = torch.utils.data.DataLoader( trainset, batch_size=BATCH_SIZE, shuffle=True, ) # 定義測(cè)試數(shù)據(jù)集 testset = tv.datasets.MNIST( root='./data/', train=False, download=True, transform=transform) # 定義測(cè)試批處理數(shù)據(jù) testloader = torch.utils.data.DataLoader( testset, batch_size=BATCH_SIZE, shuffle=False, ) # 定義損失函數(shù)loss function 和優(yōu)化方式(采用SGD) net = LeNet().to(device) criterion = nn.CrossEntropyLoss() # 交叉熵?fù)p失函數(shù),通常用于多分類問題上 optimizer = optim.SGD(net.parameters(), lr=LR, momentum=0.9) # 訓(xùn)練 if __name__ == "__main__": for epoch in range(EPOCH): sum_loss = 0.0 # 數(shù)據(jù)讀取 for i, data in enumerate(trainloader): inputs, labels = data inputs, labels = inputs.to(device), labels.to(device) # 梯度清零 optimizer.zero_grad() # forward + backward outputs = net(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() # 每訓(xùn)練100個(gè)batch打印一次平均loss sum_loss += loss.item() if i % 100 == 99: print('[%d, %d] loss: %.03f' % (epoch + 1, i + 1, sum_loss / 100)) sum_loss = 0.0 # 每跑完一次epoch測(cè)試一下準(zhǔn)確率 with torch.no_grad(): correct = 0 total = 0 for data in testloader: images, labels = data images, labels = images.to(device), labels.to(device) outputs = net(images) # 取得分最高的那個(gè)類 _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum() print('第%d個(gè)epoch的識(shí)別準(zhǔn)確率為:%d%%' % (epoch + 1, (100 * correct / total))) #torch.save(net.state_dict(), '%s/net_%03d.pth' % (opt.outf, epoch + 1))
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python中實(shí)現(xiàn)輸入一個(gè)整數(shù)的案例
這篇文章主要介紹了Python中實(shí)現(xiàn)輸入一個(gè)整數(shù)的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05Python進(jìn)階之如何快速將變量插入有序數(shù)組
在我們學(xué)習(xí)python的過程中,學(xué)習(xí)序列是一門必修課。本文我們就來一起看一看Python是如何快速將變量插入有序數(shù)組的,感興趣的可以了解一下2023-04-04使用 Python ssh 遠(yuǎn)程登陸服務(wù)器的最佳方案
這篇文章主要介紹了使用 Python ssh 遠(yuǎn)程登陸服務(wù)器的最佳方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03