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

import tensorflow as tf
from os import path, getcwd, chdir

# +
path = f"{getcwd()}/data/mnist.npz"

mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data(path=path)
# -

x_train=x_train/255.0
x_test=x_test/255.0


class myCallback(tf.keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs={}):
        if(logs.get('acc')>=0.99):
            print("\nReached 99% accuracy so cancelling training!")
            self.model.stop_training = True


callbacks = myCallback()

# +
# 10 outputs. Each output is the probability of that feature. Softmax calculates this.
# Relu function ignores values less than zero. i.e. x = x<0 : 0 ? x

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(512, activation=tf.nn.relu),
    tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
# -

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

history = model.fit(x_train, y_train, epochs=10, callbacks=[callbacks])



StarcoderdataJupyterScriptsDedupFiltered