Exercise 2

In the course you learned how to do classificaiton using Fashion MNIST, a data set containing items of clothing. There's another, similar dataset called MNIST which has items of handwriting -- the digits 0 through 9.

Write an MNIST classifier that trains to 99% accuracy or above, and does it without a fixed number of epochs -- i.e. you should stop training once you reach that level of accuracy.

Some notes:

  1. It should succeed in less than 10 epochs, so it is okay to change epochs= to 10, but nothing larger
  2. When it reaches 99% or greater it should print out the string "Reached 99% accuracy so cancelling training!"
  3. If you add any additional variables, make sure you use the same names as the ones used in the class

I've started the code for you below -- how would you finish it?

In [1]:
import tensorflow as tf
from os import path, getcwd, chdir

# DO NOT CHANGE THE LINE BELOW. If you are developing in a local
# environment, then grab mnist.npz from the Coursera Jupyter Notebook
# and place it inside a local folder and edit the path to that location
path = f"{getcwd()}/../tmp2/mnist.npz"
In [2]:
# GRADED FUNCTION: train_mnist
def train_mnist():
    # Please write your code only where you are indicated.
    # please do not remove # model fitting inline comments.

    # YOUR CODE SHOULD START HERE
    class Reaches_99_Callback(tf.keras.callbacks.Callback):
        def on_epoch_end(self, epoch, logs={}):
            if logs.get('acc')>0.99:
                print("Reached 99% accuracy so cancelling training!")
                self.model.stop_training = True
    # YOUR CODE SHOULD END HERE

    mnist = tf.keras.datasets.mnist

    (x_train, y_train),(x_test, y_test) = mnist.load_data(path=path)
    # YOUR CODE SHOULD START HERE
    x_train = x_train/ 255.0
    x_test = x_test/ 255.0
    
    callback = Reaches_99_Callback()
    # YOUR CODE SHOULD END HERE
    model = tf.keras.models.Sequential([
        # YOUR CODE SHOULD START HERE
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(units=128, activation='relu'),
        tf.keras.layers.Dense(units=10, activation='softmax')
        # YOUR CODE SHOULD END HERE
    ])

    model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])
    
    # model fitting
    history = model.fit(# YOUR CODE SHOULD START HERE
        x_train, y_train,
        epochs=10,
#         validation_data=(x_test, y_test),
        callbacks=[callback]
              # YOUR CODE SHOULD END HERE
    )
    # model fitting
    return history.epoch, history.history['acc'][-1]
In [3]:
train_mnist()
WARNING: Logging before flag parsing goes to stderr.
W1217 06:52:32.109669 140720286062400 deprecation.py:506] From /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/init_ops.py:1251: calling VarianceScaling.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
Instructions for updating:
Call initializer instance with the dtype argument instead of passing it to the constructor
Epoch 1/10
60000/60000 [==============================] - 9s 153us/sample - loss: 0.2584 - acc: 0.9267
Epoch 2/10
60000/60000 [==============================] - 9s 151us/sample - loss: 0.1143 - acc: 0.9670
Epoch 3/10
60000/60000 [==============================] - 9s 157us/sample - loss: 0.0778 - acc: 0.9764
Epoch 4/10
60000/60000 [==============================] - 9s 144us/sample - loss: 0.0577 - acc: 0.9826
Epoch 5/10
60000/60000 [==============================] - 9s 155us/sample - loss: 0.0444 - acc: 0.9866
Epoch 6/10
60000/60000 [==============================] - 10s 162us/sample - loss: 0.0363 - acc: 0.9884
Epoch 7/10
59584/60000 [============================>.] - ETA: 0s - loss: 0.0284 - acc: 0.9908Reached 99% accuracy so cancelling training!
60000/60000 [==============================] - 9s 153us/sample - loss: 0.0283 - acc: 0.9908
Out[3]:
([0, 1, 2, 3, 4, 5, 6], 0.99085)
In [4]:
# Now click the 'Submit Assignment' button above.
# Once that is complete, please run the following two cells to save your work and close the notebook
In [5]:
%%javascript
<!-- Save the notebook -->
IPython.notebook.save_checkpoint();
In [ ]:
%%javascript
IPython.notebook.session.delete();
window.onbeforeunload = null
setTimeout(function() { window.close(); }, 1000);