Exercise 3

In the videos you looked at how you would improve Fashion MNIST using Convolutions. For your exercise see if you can improve MNIST to 99.8% accuracy or more using only a single convolutional layer and a single MaxPooling 2D. You should stop training once the accuracy goes above this amount. It should happen in less than 20 epochs, so it's ok to hard code the number of epochs for training, but your training must end once it hits the above metric. If it doesn't, then you'll need to redesign your layers.

I've started the code for you -- you need to finish it!

When 99.8% accuracy has been hit, you should print out the string "Reached 99.8% accuracy so cancelling training!"


In [1]:
import tensorflow as tf

# YOUR CODE STARTS HERE
class Callbackclass(tf.keras.callbacks.Callback):
  def on_epoch_end(self,epoch,logs={}):
    if(logs.get("acc")>=0.998):
      print("Reached 99.8% accuracy so cancelling training!")
      self.model.stop_training=True
# YOUR CODE ENDS HERE

mnist = tf.keras.datasets.mnist
(training_images, training_labels), (test_images, test_labels) = mnist.load_data()

# YOUR CODE STARTS HERE
callbacks=Callbackclass()
training_images=training_images.reshape(60000,28,28,1)
training_images=training_images/255
test_images=test_images.reshape(10000,28,28,1)
test_images=test_images/255
# YOUR CODE ENDS HERE

model = tf.keras.models.Sequential([
    # YOUR CODE STARTS HERE
    tf.keras.layers.Conv2D(32,(3,3),activation="relu",input_shape=(28,28,1)),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Conv2D(32,(3,3),activation="relu"),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128,activation="relu"),
    tf.keras.layers.Dense(10,activation="softmax")
    # YOUR CODE ENDS HERE
])

# YOUR CODE STARTS HERE
model.compile(optimizer="adam",loss="sparse_categorical_crossentropy",metrics=["accuracy"])
model.fit(training_images,training_labels,epochs=20,callbacks=[callbacks])
test_loss,test_accuracy=model.evaluate(test_images,test_labels)
print(test_accuracy)
# YOUR CODE ENDS HERE


Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376/11490434 [==============================] - 0s 0us/step
WARNING: Logging before flag parsing goes to stderr.
W0820 12:04:10.372610 139695447488384 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/20
60000/60000 [==============================] - 52s 870us/sample - loss: 0.1426 - acc: 0.9570
Epoch 2/20
60000/60000 [==============================] - 52s 865us/sample - loss: 0.0452 - acc: 0.9858
Epoch 3/20
60000/60000 [==============================] - 51s 852us/sample - loss: 0.0321 - acc: 0.9899
Epoch 4/20
60000/60000 [==============================] - 50s 841us/sample - loss: 0.0231 - acc: 0.9925
Epoch 5/20
60000/60000 [==============================] - 50s 839us/sample - loss: 0.0181 - acc: 0.9943
Epoch 6/20
60000/60000 [==============================] - 50s 840us/sample - loss: 0.0147 - acc: 0.9953
Epoch 7/20
60000/60000 [==============================] - 50s 840us/sample - loss: 0.0108 - acc: 0.9963
Epoch 8/20
60000/60000 [==============================] - 51s 850us/sample - loss: 0.0100 - acc: 0.9967
Epoch 9/20
60000/60000 [==============================] - 50s 841us/sample - loss: 0.0081 - acc: 0.9973
Epoch 10/20
60000/60000 [==============================] - 50s 832us/sample - loss: 0.0066 - acc: 0.9979
Epoch 11/20
59936/60000 [============================>.] - ETA: 0s - loss: 0.0062 - acc: 0.9981Reached 99.8% accuracy so cancelling training!
60000/60000 [==============================] - 50s 839us/sample - loss: 0.0061 - acc: 0.9981
10000/10000 [==============================] - 3s 280us/sample - loss: 0.0516 - acc: 0.9886
0.9886