# ---
# 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/electrotechnik/deploying-machine-learning-models/blob/master/MNIST_model.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="dHiQab3-5BW4" colab_type="code" colab={}
import tensorflow as tf
import matplotlib.pyplot as plt


mnist = tf.keras.datasets.mnist 

# + id="LuPhnbsIaWIg" colab_type="code" colab={}


# + id="q0MV0-HM5v6W" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 51} outputId="a15be333-de83-43ee-f6f0-5860d5fcf7fa"
(x_train, y_train) , (x_test,y_test) = mnist.load_data()


# + id="Hoa3etSqaXC5" colab_type="code" colab={}
x_train = tf.keras.utils.normalize(x_train, axis=1)
x_test = tf.keras.utils.normalize(x_test, axis=1)

# + id="Kw_0J4VgYjyT" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 1000} outputId="013cf6c3-eea8-48ab-f0e4-61728afbb791"
plt.imshow(x_train[0], cmap=plt.cm.binary)

print(x_train[0])

# + id="hug6D4x6c1Ug" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 153} outputId="a7b3ecc8-07aa-4f34-81c6-8b21abf0a23d"
model = tf.keras.models.Sequential()

model.add(tf.keras.layers.Flatten())

model.add(tf.keras.layers.Dense(128,activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128,activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128,activation=tf.nn.softmax))

model.compile(optimizer = 'adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

model.fit(x_train, y_train, epochs = 3)

# + id="9s0pcwCQf1A6" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 51} outputId="da67e129-a32a-4619-91c8-568690ca8ab9"
#Calculate valuation accuracy and loss

val_loss, val_acc = model.evaluate(x_test, y_test)
print(val_loss, val_acc)

# + id="VqrOosDdh5T0" colab_type="code" colab={}
#Save the model
model.save('num_reader_1.model')

# + id="D-yrGXXxiJXx" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 156} outputId="62f34e4d-84dd-4003-8064-30ccac0fca19"
#Load the model
new_model=tf.keras.models.load_model('num_reader_1.model')

# + id="xmi4npr9d2hr" colab_type="code" colab={}
predictions = model.predict(x_test)

# + id="AEgIWAUBwAfX" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 578} outputId="90502952-0267-4a4f-9494-485d004346b2"
predictions[0]

# + id="cdMLVfGPwJE9" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="9e50f236-7dbb-4fc6-9187-e4f59ba935a8"
import numpy as np

np.argmax(predictions[1])

# + id="JFzHU8ICwdqS" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 282} outputId="37c8a38b-b312-45cf-9800-67d01b874ad5"
plt.imshow(x_test[1])

StarcoderdataJupyterScriptsDedupFiltered