pytorch MNIST study(二)
上一篇使用了logisticRegression訓練MNIST準確率只有91%左右,本次使用CNN準確率能到98.7%左右;
一、先記錄下幾個知識點:
1、tensor.view(1,-1) : 以一張28X28的圖片為例, img.view(1,-1) 該圖背拉直為1X784,-1的意思是,將圖片拉成一行時它將自行計算有多少列
2、value, index = tensor.max(x,1):按行獲取最大值,並返回所在列的索引值, tensor.max(x,0):按列獲取最大值,並返回所在行的索引值
二、訓練:
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 5 10:14:25 2018
@author: Administrator
"""
import torchvision
from torchvision import transforms,datasets
import torch
from torch.utils.data import DataLoader
from torch.autograd import Variable
import time
# 定義超引數
batch_size = 32
learning_rate = 1e-3
num_epoches = 100
t1 = time.time()
# 下載訓練集 MNIST 手寫數字訓練集
train_dataset = datasets.MNIST(root='./data', train=True,
transform=transforms.ToTensor(),
download=True)
test_dataset = datasets.MNIST(root='./data', train=False,
transform=transforms.ToTensor())
#匯入圖片
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
class CnnNetwork(torch.nn.Module):
def __init__(self, in_dim, n_class):
super(CnnNetwork, self).__init__()
self.conv = torch.nn.Sequential(
torch.nn.Conv2d(in_dim, 6, 3, stride=1, padding=1), # in_channels, out_channels, kernel_size, stride, padding
torch.nn.ReLU(True),
torch.nn.MaxPool2d(2,2),
torch.nn.Conv2d(6,16,5,stride=1,padding=0),
torch.nn.ReLU(True),
torch.nn.MaxPool2d(2,2))
self.fc = torch.nn.Sequential(
torch.nn.Linear(400,120),
torch.nn.Linear(120,84),
torch.nn.Linear(84,n_class))
def forward(self, x):
x = self.conv(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
model = CnnNetwork(1, 10) # 圖片大小是28x28 one channel
if torch.cuda.is_available():
model = model.cuda()
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
for epoch in range(num_epoches):
running_loss = 0.0
running_acc = 0.0
total = 0
for i, data in enumerate(train_loader, 1):#可以定製從第幾個開始列舉
img, label = data # img: torch.Size([32, 1, 28, 28])
img = Variable(img).cuda()
label = Variable(label).cuda()
# 向前傳播
x = model(img) # x: torch.Size([32, 10])
# print("x:",x.size())
loss = criterion(x, label)
# print("loss:",loss)
running_loss += loss.data * label.size(0)
_, pred = torch.max(x, 1) #按行獲取最大值,並返回所在列的索引值
num_correct = (pred == label).sum().item() # item()將一個維度的張量變為標量
total += label.size(0)
running_acc += num_correct
# 向後傳播
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (epoch + 1) % 20 == 0:
print('Epoch[{}/{}], train_loss: {:.6f}, train_Acc: {:.6f}'.format(epoch + 1,num_epoches,loss.data, running_acc/total))
# 測試
total = 0
running_acc = 0.0
for i,data in enumerate(test_loader, 1):
img , label = data
img = Variable(img).cuda()
label = Variable(label).cuda()
model.eval()
out = model(img)
_, pred = torch.max(out, 1)
num_correct = (pred == label).sum().item()
running_acc += num_correct
total += label.size(0)
if total:
print(' test_loss:{:.6f}, test_Acc: {:.6f}'.format( loss.data, running_acc/total))
print("ues time:",time.time() - t1)