<jupyter_start><jupyter_text>MNIST with Recurrent Neural Networks  PrerequisitesInstall the following packages<jupyter_code>! pip3 install cloudmesh-installer
! pip3 install cloudmesh-common<jupyter_output>Requirement already satisfied: cloudmesh-installer in /usr/local/lib/python3.6/dist-packages (4.4.19)
Requirement already satisfied: docopt in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (0.6.2)
Requirement already satisfied: bump2version==1.0.0 in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (1.0.0)
Requirement already satisfied: coverage in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (3.7.1)
Requirement already satisfied: oyaml in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (1.0)
Requirement already satisfied: humanize in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (0.5.1)
Requirement already satisfied: pip-tools in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (4.5.1)
Requirement already satisfied: pytest-cov in /usr/local/lib/python3.6/dist-packages (from cloudmesh-installer) (2.11.1)
Requirement already satisfied: colorama in /usr/local/lib/python3.6/dis[...]<jupyter_text>Import Libraries<jupyter_code>from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation, SimpleRNN
from tensorflow.keras.utils import to_categorical, plot_model
from tensorflow.keras.datasets import mnist
from cloudmesh.common.StopWatch import StopWatch<jupyter_output><empty_output><jupyter_text>Download Data and Pre-Process<jupyter_code>StopWatch.start("data-load")
(x_train, y_train), (x_test, y_test) = mnist.load_data()
StopWatch.stop("data-load")


StopWatch.start("data-pre-process")
num_labels = len(np.unique(y_train))


y_train = to_categorical(y_train)
y_test = to_categorical(y_test)


image_size = x_train.shape[1]
x_train = np.reshape(x_train,[-1, image_size, image_size])
x_test = np.reshape(x_test,[-1, image_size, image_size])
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255
StopWatch.stop("data-pre-process")

input_shape = (image_size, image_size)
batch_size = 128
units = 256
dropout = 0.2<jupyter_output><empty_output><jupyter_text>Define Model<jupyter_code>StopWatch.start("compile")
model = Sequential()
model.add(SimpleRNN(units=units,
                    dropout=dropout,
                    input_shape=input_shape, return_sequences=True))
model.add(SimpleRNN(units=units,
                    dropout=dropout,
                    return_sequences=True))
model.add(SimpleRNN(units=units,
                    dropout=dropout,
                    return_sequences=False))
model.add(Dense(num_labels))
model.add(Activation('softmax'))
model.summary()
plot_model(model, to_file='rnn-mnist.png', show_shapes=True)


model.compile(loss='categorical_crossentropy',
              optimizer='sgd',
              metrics=['accuracy'])
StopWatch.stop("compile")<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
simple_rnn (SimpleRNN)       (None, 28, 256)           72960     
_________________________________________________________________
simple_rnn_1 (SimpleRNN)     (None, 28, 256)           131328    
_________________________________________________________________
simple_rnn_2 (SimpleRNN)     (None, 256)               131328    
_________________________________________________________________
dense (Dense)                (None, 10)                2570      
_________________________________________________________________
activation (Activation)      (None, 10)                0         
=================================================================
Total params: 338,186
Trainable params: 338,186
Non-trainable params: 0
__________________________________________________[...]<jupyter_text>Train<jupyter_code>StopWatch.start("train")
model.fit(x_train, y_train, epochs=1, batch_size=batch_size)
StopWatch.stop("train")<jupyter_output>469/469 [==============================] - 127s 265ms/step - loss: 0.9463 - accuracy: 0.6935<jupyter_text>Test<jupyter_code>StopWatch.start("evaluate")
loss, acc = model.evaluate(x_test, y_test, batch_size=batch_size)
print("\nTest accuracy: %.1f%%" % (100.0 * acc))
StopWatch.stop("evaluate")

StopWatch.benchmark()<jupyter_output>79/79 [==============================] - 7s 80ms/step - loss: 0.2581 - accuracy: 0.9181

Test accuracy: 91.8%

+---------------------+------------------------------------------------------------------+
| Attribute           | Value                                                            |
|---------------------+------------------------------------------------------------------|
| BUG_REPORT_URL      | "https://bugs.launchpad.net/ubuntu/"                             |
| DISTRIB_CODENAME    | bionic                                                           |
| DISTRIB_DESCRIPTION | "Ubuntu 18.04.5 LTS"                                             |
| DISTRIB_ID          | Ubuntu                                                           |
| DISTRIB_RELEASE     | 18.04                                                            |
| HOME_URL            | "https://www.ubuntu.com/"                                        |
| ID                  | ubuntu                                        [...]
{'fraction_non_alphanumeric': 0.16274127855498494, 'fraction_numerical': 0.0320524172126793, 'mean_word_length': 2.8317503392130257, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '282723', 'license': 'Apache-2.0', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1555, 'n_words': 291, 'path': 'content/en/modules/notebooks/mnist_rnn.ipynb', 'repo_name': 'Zarthanix/cybertraining-dsc.github.io', 'size': '5647'}