Models import Sequential from keras. Models import Sequential from keras. Layers import LSTM, Dense from keras.datasets import mnist from keras.utils import np_utils from keras import initializations

Def init_weights(shape, name=None):return initializations. Normal (shape, scale=0.01, name=name) The plot from keras.utils. Visualize_util import plot variable needs to be loaded to initialize

Hyper parameters

batch_size = 128 nb_epoch = 10

Parameters for MNIST dataset

img_rows, img_cols = 28, 28 nb_classes = 10

Parameters for LSTM network

Nb_lstm_outputs = 30 nb_TIME_steps = IMg_ROWS DIM_INput_vector = img_cols Prepare data

Load MNIST dataset

(X_train, y_train), (X_test, y_test) = mnist.load_data() print(‘X_train original shape:’, X_train.shape) input_shape = (nb_time_steps, dim_input_vector)

X_train = X_train.astype(‘float32’) / 255. X_test = X_test.astype(‘float32’) / 255. Y_train = np_utils.to_categorical(y_train, nb_classes) Y_test = np_utils.to_categorical(y_test, nb_classes)

print(‘X_train shape:’, X_train.shape) print(X_train.shape[0], ‘train samples’) print(X_test.shape[0], ‘Test samples’) to model

Build LSTM network

model = Sequential() model.add(LSTM(nb_lstm_outputs, input_shape=input_shape)) model.add(Dense(nb_classes, Activation =’softmax’, init=init_weights)) Print model model.summary() Plot (model, to_file=’lstm_model.png’) compile model.compile(optimizer=’rmsprop’, loss=’categorical_crossentropy’, Metrics =[‘accuracy’]) iteration training history = model.fit(X_train, Y_train, nb_epoch=nb_epoch, batch_size=batch_size, Evaluate (X_test, Y_test, verbose=1) print(‘Test score:’, evaluate(X_test, Y_test, verbose=1) print(‘Test score:’, score[0]) print(‘Test accuracy:’, score[1])

— — — — — — — —