RNN in Keras for MNIST data
Although RNN is mostly used for sequence data, it can also be used for image data. We know that images have minimum two dimensions - height and width. Now think of one of the dimensions as time steps, and other as features. For MNIST, the image size is 28 x 28 pixels, thus we can think of an MNIST image as having 28 time steps with 28 features in each timestep.
Let us build and train an RNN for MNIST in Keras to quickly glance over the process of building and training the RNN models.
Note
You can follow along with the code in the Jupyter notebook ch-06_RNN_MNIST_Keras
.
Import the required modules:
import keras from keras.models import Sequential from keras.layers import Dense, Activation from keras.layers.recurrent import SimpleRNN from keras.optimizers import RMSprop from keras.optimizers import SGD
Get the MNIST data and transform the data from 784 pixels in 1-D to 28 x 28 pixels in 2-D:
from tensorflow.examples.tutorials.mnist import input_data mnist = input_data...