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)


 

猜你喜欢

转载自blog.csdn.net/qq_42527487/article/details/84855072