<jupyter_start><jupyter_text>Table of Contents  CNN for MNIST with TensorFlow and Keras<jupyter_code>import os

import numpy as np
np.random.seed(123)
print("NumPy:{}".format(np.__version__))

import tensorflow as tf
tf.set_random_seed(123)
print("TensorFlow:{}".format(tf.__version__))
DATASETSLIB_HOME = '../datasetslib'
import sys
if not DATASETSLIB_HOME in sys.path:
    sys.path.append(DATASETSLIB_HOME)
%reload_ext autoreload
%autoreload 2
import datasetslib

datasetslib.datasets_root = os.path.join(os.path.expanduser('~'),'datasets')<jupyter_output><empty_output><jupyter_text>Get the MNIST data<jupyter_code>from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets(os.path.join(datasetslib.datasets_root,
                                               'mnist'), 
                                  one_hot=True)

X_train = mnist.train.images
X_test = mnist.test.images
Y_train = mnist.train.labels
Y_test = mnist.test.labels
n_classes = 10<jupyter_output>Extracting /home/armando/datasets/mnist/train-images-idx3-ubyte.gz
Extracting /home/armando/datasets/mnist/train-labels-idx1-ubyte.gz
Extracting /home/armando/datasets/mnist/t10k-images-idx3-ubyte.gz
Extracting /home/armando/datasets/mnist/t10k-labels-idx1-ubyte.gz<jupyter_text>Preprocess for RNN<jupyter_code>X_train = X_train.reshape(-1,28,28)
X_test = X_test.reshape(-1,28,28)<jupyter_output><empty_output><jupyter_text>RNN With Keras for MNIST Data<jupyter_code>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
tf.reset_default_graph()
keras.backend.clear_session()
# create and fit the SimpleRNN model
model = Sequential()
model.add(SimpleRNN(units=16, activation='relu', input_shape=(28,28)))
model.add(Dense(n_classes))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer=RMSprop(lr=0.01),
              metrics=['accuracy'])
model.summary()

model.fit(X_train, Y_train,
                    batch_size=100,
                    epochs=20)

score = model.evaluate(X_test, Y_test)
print('\nTest loss:', score[0])
print('Test accuracy:', score[1])<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
simple_rnn_1 (SimpleRNN)     (None, 16)                720       
_________________________________________________________________
dense_1 (Dense)              (None, 10)                170       
_________________________________________________________________
activation_1 (Activation)    (None, 10)                0         
=================================================================
Total params: 890
Trainable params: 890
Non-trainable params: 0
_________________________________________________________________
Epoch 1/20
55000/55000 [==============================] - 5s 84us/step - loss: 1.3061 - acc: 0.5084
Epoch 2/20
55000/55000 [==============================] - 5s 88us/step - loss: 1.0005 - acc: 0.6136
Epoch 3/20
55000/55000 [==============================] - 4s 77us/step - los[...]
{'fraction_non_alphanumeric': 0.1653975022844959, 'fraction_numerical': 0.04081632653061224, 'mean_word_length': 4.357259380097879, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '114916', 'license': 'MIT', 'n_tokens_mistral': 1122, 'n_tokens_neox': 1013, 'n_words': 185, 'path': 'Chapter06/ch-06_RNN_MNIST_Keras.ipynb', 'repo_name': 'vgoklani/Mastering-TensorFlow-1x', 'size': '3283'}