# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/LeonVillanueva/CoLab/blob/master/Google_CoLab_LSTM_MNIST.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="nYbE39yTZhzE" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 73} outputId="47a9d248-5458-406e-b24a-801eb72b69db"
# !pip install -q tensorflow==2.0.0-beta1

# + id="H7etzltMZlhf" colab_type="code" colab={}
# %%capture
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import tensorflow as tf

# + id="GXPLYSg-e6b2" colab_type="code" colab={}
import math
import seaborn as sns

# + id="Y97pLFRbZnck" colab_type="code" colab={}
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

# + id="sm9oFz0UZpsh" colab_type="code" colab={}
from tensorflow.keras.layers import Input, Conv2D, Dense, Flatten, Dropout, GlobalMaxPooling2D, GaussianNoise, BatchNormalization, MaxPooling2D, SimpleRNN, GRU, LSTM, GlobalMaxPooling1D
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import SGD, Adam
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing.sequence import TimeseriesGenerator

# + id="WR_C9aEmaDdJ" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 54} outputId="dabd9fe4-498a-4dd6-e964-3b3745018799"
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()

# + id="rN3aO8AaaFtv" colab_type="code" colab={}
X_train = X_train / np.max (X_train)
X_test = X_test / np.max (X_test)

# + id="A6c8UKWydABk" colab_type="code" colab={}
T, D = X_train[0].shape

# + id="hyquJ6WJdQ6n" colab_type="code" colab={}
i = Input (shape=(T,D))
x = LSTM (128) (i)
x = Dense (10, activation='softmax') (x)

# + id="hGJFjq5xd4a1" colab_type="code" colab={}
model = Model (i, x)

# + id="I9QVCYiqeLD5" colab_type="code" colab={}
adam = tf.keras.optimizers.Adam (learning_rate=0.05, decay=5e-5)

# + id="4DkKiHtBeNiF" colab_type="code" colab={}
model.compile (optimizer=adam,
               loss='sparse_categorical_crossentropy',
               metrics=['accuracy'])

# + id="pXCsGySUeg_r" colab_type="code" colab={}
epochs = 10

# + id="Gr8Ee9ZweVKV" colab_type="code" colab={}
m = model.fit (X_train, y_train, epochs=epochs, verbose=False, validation_data=(X_test, y_test))

# + id="RfCLiRgrfoq2" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 54} outputId="bd13d7c2-ae14-4dce-927b-960f952259c9"
model.evaluate (X_test, y_test)

# + id="zcfW9VUiewCE" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 284} outputId="2c33b020-802c-4629-c314-ef7bb536e7f8"
plt.plot (m.history['loss'], label='loss', color='#840000')
plt.plot (m.history['val_loss'], label='validation loss', color='#00035b')
plt.legend ()

# + id="ggo-TRvhiQTs" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 284} outputId="f414f97a-2d70-426d-ebf8-492b718ad5aa"
plt.plot (m.history['accuracy'], label='accuracy', color='#840000')
plt.plot (m.history['val_accuracy'], label='validation accuracy', color='#00035b')
plt.legend ()

# + id="bLTimphgjMNF" colab_type="code" colab={}
y_pred = model.predict (X_test)

# + id="vxTILzYykufn" colab_type="code" colab={}
y_pred_class = []

for i in range (len (y_pred)):
  y_pred_class.append (np.argmax (y_pred[i]))

# + id="CV1RKwo3lH5g" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 36} outputId="63f57b85-19ca-4a4f-dcc8-01f490b63734"
len (y_pred_class) == len (y_test)

# + id="t5IC8KRNlv99" colab_type="code" colab={}
con_mat = tf.math.confusion_matrix(labels=y_test, predictions=y_pred_class).numpy()

# + id="lvzQZqxvlpyZ" colab_type="code" colab={}
con_mat_norm = np.around(con_mat.astype('float') / con_mat.sum(axis=1)[:, np.newaxis], decimals=2)

# + id="zDhPmKGjl1De" colab_type="code" colab={}
con_mat_df = pd.DataFrame(con_mat_norm)

# + id="owZMa5hZl1e2" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 599} outputId="959a3bdd-0e3b-4ec8-fbcc-6aa0465badcb"
figure = plt.figure(figsize=(8, 8))
sns.heatmap(con_mat_df, annot=True, cmap=plt.cm.BuPu)
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()

# + id="LqNJdQQUmDiY" colab_type="code" colab={}
misclass = np.where (y_pred_class != y_test)[0]

# + id="SNN5eSxImFM2" colab_type="code" colab={}
i = np.random.choice (misclass)

# + id="qaaFUdWhmLMk" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 300} outputId="96fb4ad4-90df-4d95-f74e-10f03a08ead2"
plt.imshow (X_test[i], cmap='Blues')
plt.title ('True: %s, Predicted: %s' % (y_test[i], y_pred_class[i]))

StarcoderdataJupyterScriptsDedupFiltered