Pytorch搭建循环神经网络(RNN)实现MNIST手写数字识别(2)

```
# -*- coding: utf-8 -*-
"""
Created on Sun Jul  7 12:45:39 2019

@author: ZQQ

参考:https://github.com/L1aoXingyu/pytorch-beginner/blob/master/05-Recurrent%20Neural%20Network/recurrent_network.py
"""

import torch
from torch import nn
import torchvision.datasets as dsets
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
from torch.utils.data import DataLoader
# torch.manual_seed(1)    # reproducible

# Hyper Parameters,定义超参数
num_epoches = 2               # train the training data n times, to save time, we just train 1 epoch
BATCH_SIZE = 64         #批训练的数量
TIME_STEP = 28          # rnn time step / image height  考虑28个时间点,一行信息包括28个像素点,
INPUT_SIZE = 28         # rnn input size / image width  每28步中的一步读取一行信息(一行信息包括28个像素点)
LR = 0.01               # learning rate
DOWNLOAD_MNIST = False  # set to True if haven't download the data

# MNIST数据集下载
train_data = dsets.MNIST(root='./mnist/',
                         train=True,                         # this is training data
                         transform=transforms.ToTensor(),    # Converts a PIL.Image or numpy.ndarray to
                                        # torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0]
                         download=DOWNLOAD_MNIST,            # download it if you don't have it
                        )

test_data = dsets.MNIST(root='./mnist/',
                        train=False,                         # 测试集
                        transform=transforms.ToTensor()
                        )

# plot其中一张手写数字图片
print(train_data.train_data.size())     # 查看训练集数据大小,60000张28*28的图片 (60000, 28, 28)
print(train_data.train_labels.size())   # 查看训练集标签大小,60000个标签 (60000)
plt.imshow(train_data.train_data[0].numpy(), cmap='gray') # plot 训练集第一张图片
plt.title('%i' % train_data.train_labels[0])              # 图片名称,显示真实标签,%i %d十进制整数,有区别,深入请查阅资料
plt.show()                                                # show

# Data Loader for easy mini-batch return in training
train_loader = DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)
test_loader = DataLoader(dataset=test_data, batch_size=BATCH_SIZE, shuffle=True)

# 定义网络模型
class RNN(nn.Module):
    def __init__(self):
        super(RNN, self).__init__()
        self.rnn = nn.LSTM(input_size=INPUT_SIZE,  # if use nn.RNN(), it hardly learns
                           hidden_size=64,         # rnn 隐藏单元
                           num_layers=1,           # rnn 层数
                           batch_first=True,       # input & output will has batch size as 1s dimension. e.g. (batch, time_step, input_size)
                          )
        self.out = nn.Linear(64, 10)  #10分类

    def forward(self, x):
        # x shape (batch, time_step, input_size)
        # r_out shape (batch, time_step, output_size)
        # h_n shape (n_layers, batch, hidden_size)
        # h_c shape (n_layers, batch, hidden_size)
        r_out, (h_n, h_c) = self.rnn(x, None)   # None represents zero initial hidden state

        # choose r_out at the last time step
        out = self.out(r_out[:, -1, :])
        return out

rnn = RNN() # 调用模型
print(rnn)  # 查看模型结构

optimizer = torch.optim.Adam(rnn.parameters(), lr=LR)   # 选择优化器,optimize all cnn parameters
loss_func = nn.CrossEntropyLoss()                       # 定义损失函数,the target label is not one-hotted

train_acces = []
train_losses = []
eval_acces = []
eval_losses = []
# 训练,测试。training and testing
for epoch in range(num_epoches):
    print('epoch {}'.format(epoch + 1))
    print('*' * 10)
    train_loss = 0.0
    train_acc = 0.0
    #print(len(train_loader)) # num_iterations = len(train_loader):一次epoch经过num_iterations次迭代,就是下面的step:938,跟batch_size有关
    for step, (imgs, labels) in enumerate(train_loader):    # gives batch data
        #print(step)  # 1,2,3,4,5,...,
        #print(b_x)   # 图片向量
        #print(b_y)    # 图片对应的标签
        #print(len(b_x)) # 每次迭代数据量大小
        imgs = imgs.view(-1, 28, 28)                      # reshape x to (batch, time_step, input_size)
        # 前向传播
        output = rnn(imgs)                               # rnn output
        loss = loss_func(output, labels)                   # cross entropy loss   
        train_loss += loss.data.numpy() # loss张量转为numpy标量
        #print(train_loss)
        pred_y = torch.max(output,1)[1].data.numpy()
        labels = labels.numpy() # covert to numpy array
        num_correct = (pred_y == labels).sum()
        train_acc += num_correct / len(labels) # 每批次数据的accuracy
        #print(train_acc)
        # 后向传播
        optimizer.zero_grad()                           # clear gradients for this training step
        loss.backward()                                 # backpropagation, compute gradients
        optimizer.step()                                # apply gradients
        
#        print('[%d/%d epoch]:'%(epoch,num_epoches),
#              '[%d/%d iter]:' % (step+1,len(train_loader)))  # 从step+1开始
    
    # 完成一次epoch,记录平均值
    train_losses.append(train_loss / len(train_loader))
    train_acces.append(train_acc / len(train_loader))
    
    print('[%d/%d]'%(epoch+1,num_epoches),
          'train loss:%.4f' % (train_loss / len(train_loader)),
          'train acc:%.4f' % (train_acc / len(train_loader))
          )
    
    rnn.eval() 
    eval_loss = 0.0
    eval_acc = 0.0      
    for test_step,(test_imgs, test_labels) in enumerate(test_loader):
        test_imgs = test_imgs.view(-1,28,28)
        test_output = rnn(test_imgs)
        test_loss = loss_func(test_output,test_labels)
        eval_loss += test_loss.data.numpy() # test_loss张量转为numpy标量
        test_pred_y = torch.max(test_output, 1)[1].data.numpy()
        test_labels = test_labels.numpy()   # covert to numpy array
#        #test_acc = float((test_pred_y == test_labels).astype(int).sum()) / float(test_labels.size)
        test_num_correct = (test_pred_y == test_labels).sum()
        eval_acc += test_num_correct / len(test_labels)

    # 完成一次epoch,记录平均值
    eval_losses.append(eval_loss / len(test_loader))
    eval_acces.append(eval_acc / len(test_loader))
    
    print('[%d/%d]'%(epoch+1,num_epoches),
          'test loss:%.4f' % (eval_loss / len(test_loader)),
          'test acc:%.4f' % (eval_acc / len(test_loader))
          )

# 保存模型
torch.save(rnn.state_dict(), 'Result/model_save/rnn.pth')
```

Pytorch中文官网
https://www.pytorchtutorial.com/10-minute-pytorch-6/

你可能感兴趣的:(python,PyTorch,深度学习,RNN)