In [ ]:
# from: https://github.com/fchollet/keras/blob/master/examples/mnist_hierarchical_rnn.py
In [1]:
from __future__ import print_function
from keras.datasets import mnist
from keras.models import Sequential, Model
from keras.layers import Input, Dense, TimeDistributed
from keras.layers import LSTM
from keras.utils import np_utils
In [2]:
# Training parameters.
batch_size = 32
nb_classes = 10
nb_epochs = 5
In [3]:
# Embedding dimensions.
row_hidden = 128
col_hidden = 128
In [4]:
# The data, shuffled and split between train and test sets.
(X_train, y_train), (X_test, y_test) = mnist.load_data()
In [5]:
X_train?
In [6]:
# Reshapes data to 4D for Hierarchical RNN.
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')
In [7]:
# Converts class vectors to binary class matrices.
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
In [8]:
row, col, pixel = X_train.shape[1:]
In [9]:
# 4D input.
x = Input(shape=(row, col, pixel))
In [10]:
# Encodes a row of pixels using TimeDistributed Wrapper.
encoded_rows = TimeDistributed(LSTM(output_dim=row_hidden))(x)
In [11]:
# Encodes columns of encoded rows.
encoded_columns = LSTM(col_hidden)(encoded_rows)
In [12]:
# Final predictions and model.
prediction = Dense(nb_classes, activation='softmax')(encoded_columns)
model = Model(input=x, output=prediction)
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
In [13]:
# Training.
model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epochs,
verbose=1, validation_data=(X_test, Y_test))
Out[13]:
In [ ]: