使用层次循环神经网络(HNNN)对MNIST数字(集合)进行分类实例
代码注释
"""Example of using Hierarchical RNN (HRNN) to classify MNIST digits.
使用层次循环神经网络(HNNN)对MNIST数字进行分类实例
HRNNs can learn across multiple levels
of temporal hierarchy over a complex sequence.
HNNS可以在复杂的序列上跨越多个层次的时间层次学习。
Usually, the first recurrent layer of an HRNN
encodes a sentence (e.g. of word vectors)
into a sentence vector.
通常,HRNN的第一递归层将句子(例如单词向量)编码成句子向量。
The second recurrent layer then encodes a sequence of
such vectors (encoded by the first layer) into a document vector.
然后,第二递归层将这样的向量序列(由第一层编码)编码成文档向量。
This document vector is considered to preserve both
the word-level and sentence-level structure of the context.
该文档载体被认为既保留了上下文的词级,又保留了句子级结构。
# References
参考
- [A Hierarchical Neural Autoencoder for Paragraphs and Documents](https://arxiv.org/abs/1506.01057)
用于段落和文档的分层神经自动编码器
Encodes paragraphs and documents with HRNN.
用HRNN对段落和文档进行编码。
Results have shown that HRNN outperforms standard
RNNs and may play some role in more sophisticated generation tasks like
summarization or question answering.
结果表明,HRNN优于标准RNNs,并可在更复杂的生成任务中起作用,如摘要或问答。
- [Hierarchical recurrent neural network for skeleton based action recognition](http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=7298714)
基于层次循环神经网络动作识别
Achieved state-of-the-art results on
skeleton based action recognition with 3 levels
of bidirectional HRNN combined with fully connected layers.
实现了基于3层双向HRNN与全连接层的基于骨架的动作识别的最新结果。
In the below MNIST example the first LSTM layer first encodes every
column of pixels of shape (28, 1) to a column vector of shape (128,).
在下面的MNIST示例中,第一LSTM层首先将形状(28, 1)的每一列编码为形状(128,)的列向量。
The second LSTM layer encodes then these 28 column vectors of shape (28, 128)
to a image vector representing the whole image.
第二LSTM层将形状(28, 128)的这28个列向量编码为表示整个图像的图像向量。
A final Dense layer is added for prediction.
最后添加全连接层用于预测。
After 5 epochs: train acc: 0.9858, val acc: 0.9864
5个周期后;训练准确度:0.9858,验证准确率:0.9864
"""
from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Model
from keras.layers import Input, Dense, TimeDistributed
from keras.layers import LSTM
# Training parameters.
# 训练参数
batch_size = 32
num_classes = 10
epochs = 5
# Embedding dimensions.
# 嵌入维度。
row_hidden = 128
col_hidden = 128
# The data, shuffled and split between train and test sets.
# 用于训练和测试的数据集,经过了筛选(清洗、数据样本顺序打乱)和划分(划分为训练和测试集)
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# Reshapes data to 4D for Hierarchical RNN.
# 为HRNN,调整数据为4维格式
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# Converts class vectors to binary class matrices.
# 类别向量转为多分类矩阵
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
row, col, pixel = x_train.shape[1:]
# 4D input.
# 4维输入
x = Input(shape=(row, col, pixel))
# Encodes a row of pixels using TimeDistributed Wrapper.
# 使用TimeDistributed Wrapper编码一行像素。
encoded_rows = TimeDistributed(LSTM(row_hidden))(x)
# Encodes columns of encoded rows.
# 编码已编码行的列。
encoded_columns = LSTM(col_hidden)(encoded_rows)
# Final predictions and model.
# 预测和建模
prediction = Dense(num_classes, activation='softmax')(encoded_columns)
model = Model(x, prediction)
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
# Training.
# 训练
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(x_test, y_test))
# Evaluation.
# 评估
scores = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])
代码执行
Keras详细介绍
中文:http://keras-cn.readthedocs.io/en/latest/
实例下载
https://github.com/keras-team/keras
https://github.com/keras-team/keras/tree/master/examples
完整项目下载
方便没积分童鞋,请加企鹅452205574,共享文件夹。
包括:代码、数据集合(图片)、已生成model、安装库文件等。
![](https://i-blog.csdnimg.cn/blog_migrate/b1987bf9c08566803334751f442a7f32.png)