pytorch学习-7:RNN 循环神经网络 (分类)

Posted Paul-Huang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pytorch学习-7:RNN 循环神经网络 (分类)相关的知识,希望对你有一定的参考价值。

pytorch学习-7:RNN 循环神经网络(分类)

循环神经网络让神经网络有了记忆, 对于序列话的数据,循环神经网络能达到更好的效果.

1. 加载MNIST手写数据

import torch
from torch import nn
import torchvision.datasets as dsets
import torchvision.transforms as transforms
import matplotlib.pyplot as plt

torch.manual_seed(1)

# Hyper Parameters
EPOCH = 1           #  训练整批数据多少次,为了节省时间,只训练一次
BATCH_SIZE = 64
TIME_STEP  = 28     # run 时间步骤/图片高度
INPUT_SIZE = 28     # run 每步输入值/图片的每行像素
LR = 0.01           # Learning rate
DOWNLOAD_MNIST = False

## MNIST 数据集
train_data = dsets.MNIST(
    root='./mnist/',                                      # 保存数据的位置
    train=True,                                         # this is training data
    transform = transforms.ToTensor() ,     # 转换成 PIL.Image or numpy.ndarray 成 torch.FloatTensor (C * H * W),
                                                         #  训练的时候normalize 成 [0.0, 1.0]区间
    download = DOWNLOAD_MNIST,
)
print(train_data.train_data.size())     # (60000, 28, 28)
print(train_data.train_labels.size())   # (60000)
plt.imshow(train_data.train_data[0].numpy(), cmap='gray')
plt.title('%i' % train_data.train_labels[0])
plt.show()

1.1 数据预处理

# 批训练 50 samples, 1 channel, 28*28(50,1, 28,28)
train_loader = torch.utils.data.DataLoader(dataset= train_data, batch_size= BATCH_SIZE, shuffle= True)

# 准备测试数据
# shape from (2000, 28,28) to (2000,1,28,28), value in range (0,1)
test_data = dsets.MNIST(root='./mnist/', train = False, transform=transforms.ToTensor())
test_x = test_data.test_data.type(torch.FloatTensor)[:2000]/255.    # shape (2000, 28, 28) value in range(0,1)
test_y = test_data.test_labels.numpy()[:2000]   # covert to numpy array

2. RNN模型建立

## RNN 模型
class RNN(nn.Module):
    def __init__(self):
        super(RNN, self).__init__()
        self.rnn = nn.LSTM(     # LSTM 效果要比 nn.RNN() 好多了
            input_size=INPUT_SIZE,      # 图片每行的数据像素点
            hidden_size=64,     # rnn hidden unit
            num_layers=2,       # 有2层 RNN layers
            batch_first=True,   # input & output 会是以 batch size 为第一维度的特征集 e.g. (batch, time_step, input_size)
        )

        self.out = nn.Linear(64, 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)   LSTM 有两个 hidden states, h_n 是分线, h_c 是主线
        # h_c shape (n_layers, batch, hidden_size)
        r_out, (h_n, h_c) = self.rnn(x, None)  # None 表示 hidden state 会用全0的 state

        # 选取最后一个时间点的 r_out 输出
        # 这里 r_out[:, -1, :] 的值也是 h_n 的值
        out = self.out(r_out[:, -1, :])
        return out

rnn = RNN()
print(rnn)

3. 训练

  • 将图片数据看成一个时间上的连续数据, 每一行的像素点都是这个时刻的输入,
  • 读完整张图片就是从上而下的读完了每行的像素点.
  • 最后就可以拿出 RNN 在最后一步的分析值判断图片是哪一类了.
# RNN 训练
optimizer = torch.optim.Adam(rnn.parameters(), lr=LR)
loss_func = nn.CrossEntropyLoss()

# training
for epoch in range(EPOCH):
    for step, (x , b_y) in enumerate(train_loader):   # gives batch data
        b_x = x.view(-1,28,28)          # reshape x to (batch, time_step, input_size)

        output = rnn(b_x)               # rnn output
        loss = loss_func(output, b_y)   # cross entropy loss
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        if step % 50 == 0:
            test_output = rnn(test_x)              # (samples, time_step, input_size)
            pred_y = torch.max(test_output, 1)[1].data.numpy()
            accuracy = float((pred_y == test_y).astype(int).sum()) / float(test_y.size)
            print('Epoch: ', epoch, '| train loss: %.4f' % loss.data.numpy(), '| test accuracy: %.2f' % accuracy)

4. 预测

## testing
test_output = rnn(test_x[:15].view(-1, 28, 28))
pred_y = torch.max(test_output, 1)[1].data.numpy()
print(pred_y, 'prediction number')
print(test_y[:10], 'real number')

参考

  1. https://github.com/MorvanZhou/PyTorch-Tutorial/blob/master/tutorial-contents

以上是关于pytorch学习-7:RNN 循环神经网络 (分类)的主要内容,如果未能解决你的问题,请参考以下文章

pytorch学习-7:RNN 循环神经网络 (分类)

pytorch学习笔记:循环神经网络RNN(简介)

循环神经网络 RNN 从零开始实现 动手学深度学习v2 pytorch

Pytorch_循环神经网络RNN

《PyTorch深度学习实践10》——循环神经网络-基础篇(Basic-Recurrent Neural Network)

小白学习PyTorch教程九基于Pytorch训练第一个RNN模型