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)倉(cāng)庫(kù)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)倉(cāng)庫(kù)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
基于Python實(shí)現(xiàn)二維圖像雙線性插值
雙線性插值,又稱為雙線性內(nèi)插。在數(shù)學(xué)上,雙線性插值是有兩個(gè)變量的插值函數(shù)的線性插值擴(kuò)展,其核心思想是在兩個(gè)方向分別進(jìn)行一次線性插值。本文將用Python實(shí)現(xiàn)二維圖像雙線性插值,感興趣的可以了解下2022-06-06
Python基于pygame實(shí)現(xiàn)單機(jī)版五子棋對(duì)戰(zhàn)
這篇文章主要為大家詳細(xì)介紹了Python基于pygame實(shí)現(xiàn)單機(jī)版五子棋對(duì)戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
PO模式在selenium自動(dòng)化測(cè)試框架的優(yōu)勢(shì)
大家都知道po模式可以提高代碼的可讀性和減少了代碼的重復(fù),但是相對(duì)的缺點(diǎn)還有,今天通過本文一起學(xué)習(xí)下PO模式在selenium自動(dòng)化測(cè)試框架的優(yōu)勢(shì),需要的朋友可以參考下2022-03-03
Python如何用filter函數(shù)篩選數(shù)據(jù)
這篇文章主要介紹了Python如何用filter函數(shù)篩選數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
python目標(biāo)檢測(cè)數(shù)據(jù)增強(qiáng)的代碼參數(shù)解讀及應(yīng)用
這篇文章主要為大家介紹了python目標(biāo)檢測(cè)數(shù)據(jù)增強(qiáng)的代碼參數(shù)解讀及應(yīng)用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05

