# ---
# 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/kee1bo/Machine-Learning/blob/main/mnist_num_recog.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="KZWiGtcqI1dS"
import tensorflow as tf

# + colab={"base_uri": "https://localhost:8080/"} id="ZHLmMYRCI89a" outputId="f6f9d28f-3684-485a-929a-249fee510627"
mnist = tf.keras.datasets.mnist
(x_train,y_train),(x_test,y_test) = mnist.load_data()

x_train = tf.keras.utils.normalize(x_train,axis=1)
x_test = tf.keras.utils.normalize(x_test,axis=1)

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(10,activation=tf.nn.softmax))

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

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

# + colab={"base_uri": "https://localhost:8080/", "height": 265} id="Fol2Qu0oI9_8" outputId="89139347-3b03-4ea4-da81-c732ca255676"
import matplotlib.pyplot as plt

plt.imshow(x_train[0],cmap=plt.cm.binary)
plt.show()

# + colab={"base_uri": "https://localhost:8080/"} id="79iTOoMMJAOB" outputId="b057099b-0b2e-4042-82fd-3a3e5478fe1d"

print(x_train[0])

# + colab={"base_uri": "https://localhost:8080/"} id="Cttn0i4rJBSM" outputId="7316aa98-9ee7-4c60-c17d-07403c4e744f"
val_loss,val_acc=model.evaluate(x_test,y_test)
print(val_loss,val_acc)

# + colab={"base_uri": "https://localhost:8080/"} id="Ha0iwenXJC4O" outputId="763c89a0-d89b-4fe2-ba7d-0cefa59c7a82"
model.save('number_recognizer.model')

# + id="QHkE8LZYJEhZ"
new_model = tf.keras.models.load_model('number_recognizer.model')

# + id="-HFX-ty5JHZD"
predictions = new_model.predict([x_test])

# + colab={"base_uri": "https://localhost:8080/"} id="AJcPlkr7JIja" outputId="8c69cf4c-685a-40d7-e5b7-0ef021b70c92"
predictions

# + colab={"base_uri": "https://localhost:8080/"} id="6iEpJTxbJdqi" outputId="66ef104c-33d4-4a2f-8f00-83e05d0bff9f"
import numpy as np

print(np.argmax(predictions[0]))

# + colab={"base_uri": "https://localhost:8080/", "height": 265} id="zH69VqwtJfub" outputId="aa92b9ff-f24a-42a2-8987-c9aa43fa9823"
plt.imshow(x_test[0])
plt.show()

# + id="Adydy9rDJgw6"


StarcoderdataJupyterScriptsDedupFiltered