循环神经网络(Recurrent Neural Network)
让模型充满记忆力,在序列问题和自然语言处理等领域取得很大的成功。
RNN目前使用最多的两种变式:LSTM和GRU
以上2种变式都能够很好地解决长时依赖问题。
LSTM:Long Short Term Memory Networks,长的短时记忆网络
它解决的仍是短时记忆问题,只不过这种短时记忆网络比较长,能在一定程度解决长时依赖的问题。
LSTM,是1997年提出的。
GRU:Gated Recurrent Unit的缩写,由Cho2014年提出。
在此,不详述二者的区别,先把它们当做黑盒,我们先会调用,再深入理论学习。
我们搭建RNN来实现MNIST数据集的识别分类
首先需要将图片数据转化为一个序列数据,MNIST手写数字图片大小为28*28,那么可以将每张图片看作长为28的序列,序列中的每个元素的特征维度是28,这样就将图片变成了一个序列。
Python3.6环境
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 7 12:45:39 2019
@author: ZQQ
"""
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,定义超参数
EPOCH = 1 # 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()
)
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,pick 2000 samples to speed up testing
# 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)
# 定义网络模型
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
# 训练,测试。training and testing
for epoch in range(EPOCH):
for step, (b_x, b_y) in enumerate(train_loader): # gives batch data
#print(step) # 1,2,3,4,5,...,
#print(b_x) # 图片向量
#print(b_y) # 图片对应的标签
b_x = b_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() # clear gradients for this training step
loss.backward() # backpropagation, compute gradients
optimizer.step() # apply gradients
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)
# print 10 predictions from test data
test_output = rnn(test_x[:10].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')
参考:莫烦pytorch