<jupyter_start><jupyter_text>RNN: Recurent Neural Network on handwrting digits recognition  import libraries<jupyter_code>import numpy as np
from keras.layers import Dense, Activation, Input, SimpleRNN
from keras.models import Model
from keras.datasets import mnist
import matplotlib.pyplot as plt<jupyter_output>Using TensorFlow backend.<jupyter_text>load data<jupyter_code>(x_train,y_train), (x_test,y_test) = mnist.load_data()<jupyter_output><empty_output><jupyter_text>compute the number of labels<jupyter_code>num_labels = len(np.unique(y_train))<jupyter_output><empty_output><jupyter_text>verify the data<jupyter_code>indexes = np.random.randint(0,x_train.shape[0], size=25)
images = x_train[indexes]
labels = y_train[indexes]

plt.figure(figsize=(5,5))
for i in range(len(indexes)):
    plt.subplot(5,5,i+1)
    image = images[i]
    plt.imshow(image,cmap='gray')
    plt.axis('off')

plt.show()
plt.close('all')<jupyter_output><empty_output><jupyter_text>resize and normalize<jupyter_code>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<jupyter_output><empty_output><jupyter_text>network parameters<jupyter_code>input_shape = (image_size,image_size)
batch_size = 128
units = 256
dropout = 0.2
epochs = 20<jupyter_output><empty_output><jupyter_text>functional API to build CNN layers<jupyter_code>inputs = Input(shape=input_shape)
y = SimpleRNN(units=units,dropout=dropout)(inputs)<jupyter_output><empty_output><jupyter_text>outputs<jupyter_code>outputs = Dense(num_labels,activation='softmax')(y)<jupyter_output><empty_output><jupyter_text>build the model<jupyter_code>model = Model(inputs=inputs, outputs=outputs)
model.summary()<jupyter_output>Model: "model_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 28, 28)            0         
_________________________________________________________________
simple_rnn_1 (SimpleRNN)     (None, 256)               72960     
_________________________________________________________________
dense_1 (Dense)              (None, 10)                2570      
=================================================================
Total params: 75,530
Trainable params: 75,530
Non-trainable params: 0
_________________________________________________________________<jupyter_text>train the model<jupyter_code>model.compile(optimizer='sgd',loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
history = model.fit(x_train,y_train,
                    validation_data=(x_test,y_test),
                    epochs=20,batch_size=batch_size)
print("\n")
print(history.history.keys())<jupyter_output>Train on 60000 samples, validate on 10000 samples
Epoch 1/20
60000/60000 [==============================] - 10s 173us/step - loss: 0.7885 - accuracy: 0.7693 - val_loss: 0.3790 - val_accuracy: 0.8941
Epoch 2/20
60000/60000 [==============================] - 10s 174us/step - loss: 0.3585 - accuracy: 0.8936 - val_loss: 0.2332 - val_accuracy: 0.9337
Epoch 3/20
60000/60000 [==============================] - 11s 183us/step - loss: 0.2685 - accuracy: 0.9198 - val_loss: 0.1771 - val_accuracy: 0.9487
Epoch 4/20
60000/60000 [==============================] - 13s 219us/step - loss: 0.2213 - accuracy: 0.9346 - val_loss: 0.1455 - val_accuracy: 0.9562
Epoch 5/20
60000/60000 [==============================] - 14s 241us/step - loss: 0.1977 - accuracy: 0.9407 - val_loss: 0.1505 - val_accuracy: 0.9561
Epoch 6/20
60000/60000 [==============================] - 12s 207us/step - loss: 0.1733 - accuracy: 0.9485 - val_loss: 0.1368 - val_accuracy: 0.9605
Epoch 7/20
60000/60000 [==============================] [...]<jupyter_text>evaluate the model<jupyter_code>score = model.evaluate(x_test,y_test,batch_size=batch_size)
print("\nTest accuracy: %.1f%%" % (100*score[1]))
print("\n")<jupyter_output>10000/10000 [==============================] - 1s 60us/step

Test accuracy: 98.3%<jupyter_text>plotting<jupyter_code>plt.plot(history.history['accuracy'],'o',label='accuracy')
plt.plot(history.history['loss'],'o',label='loss')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0,1])
plt.legend(loc='best')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20165993719156572, 'fraction_numerical': 0.07671601615074024, 'mean_word_length': 6.055379746835443, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '446745', 'license': 'MIT', 'n_tokens_mistral': 1763, 'n_tokens_neox': 1492, 'n_words': 246, 'path': 'Artificial_Neural_Network/RNN.ipynb', 'repo_name': 'AndryRafam/DEEP-LEARNING-Python3', 'size': '4458'}