1. 程式人生 > >pytorch MNIST study(一)

pytorch MNIST study(一)

最近開始學習pytorch,從mnist入手

一、先記錄下幾個知識點:

1、torch中的tensor與numpy中的array,tensor與array是可以相互轉換的, Variable中包含兩部分資訊,分別是data and grad:

tensor to array  :  a = torch.FloatTensor(3,3)               b = a.numpy()

numpy to tensor  :   a = np.ones(5)      b = torch.from_numpy(a)

Variable to array  :   a = Variable(torch.FloatTensor(3,3))      b = a.data.numpy()

array to Variable  :  a = np.ones(5)     b = Variable(torch.from_numpy(a))

2、tensor.size() 返回一個張量的shape,可以看成是一個tuple:

x = torch.Tensor(5, 3)     # construct a 5x3 matrix, uninitialized

x.size()      # out: torch.Size([5, 3])

x.size(0)    # out: 5

二、MNIST訓練:

# -*- 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


# 定義超引數
batch_size = 32
learning_rate = 1e-3
num_epoches = 100

# 下載訓練集 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 Logstic_Regression(torch.nn.Module):
    def __init__(self, in_dim, n_class):
        super(Logstic_Regression, self).__init__()
        self.logstic = torch.nn.Linear(in_dim, n_class)

    def forward(self, x):
        out = self.logstic(x)
        return out

model = Logstic_Regression(28*28, 10)  # 圖片大小是28x28
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
    time = 0
    for i, data in enumerate(train_loader, 1):        #可以定製從第幾個開始列舉
        img, label = data
        img = img.view(img.size(0), -1)             # 將圖片展開成 28x28
        if torch.cuda.is_available():
            img = Variable(img).cuda()
            label = Variable(label).cuda()
        else:
            img = Variable(img)
            label = Variable(label)
            
        # 向前傳播
        out = model(img)
        loss = criterion(out, label)
        running_loss += loss.data * label.size(0)
        
        _, pred = torch.max(out, 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:
            time += 1
            print('Epoch[{}/{}], loss: {:.6f}, Acc: {:.6f}'.format(epoch + 1,num_epoches,loss.data, running_acc/total))
        #if time:
            #print("time:",time)

# 測試
total = 0
running_acc = 0.0
for i,data in enumerate(test_loader, 1):
    img , label = data
    img = img.view(img.size(0), -1)
    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(' Acc: {:.6f}'.format( running_acc/total))

結果:

Epoch[100/100], loss: 0.198154, Acc: 0.911670
Epoch[100/100], loss: 0.165920, Acc: 0.911684
Epoch[100/100], loss: 0.276448, Acc: 0.911698
Epoch[100/100], loss: 0.155880, Acc: 0.911728
Epoch[100/100], loss: 0.185178, Acc: 0.911759
Epoch[100/100], loss: 0.256010, Acc: 0.911756
Epoch[100/100], loss: 0.309416, Acc: 0.911753
Epoch[100/100], loss: 0.244036, Acc: 0.911767
 Acc: 0.916600