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

本文介绍如何利用循环神经网络(RNN)及其变体LSTM和GRU解决序列问题,特别是在自然语言处理领域的应用。通过将MNIST手写数字数据集转化为序列数据,使用PyTorch搭建RNN模型进行识别分类,并展示训练和测试过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

循环神经网络(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

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

机器不学习我学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值