<jupyter_start><jupyter_text>Exercise 3In 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!"<jupyter_code>import tensorflow as tf

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

# YOUR CODE ENDS HERE

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

# YOUR CODE STARTS HERE
training_images, test_images = training_images / 255.0, test_images / 255.0
training_images = training_images.reshape(60000, 28, 28, 1)
test_images = test_images.reshape(10000, 28, 28, 1)

print(training_images.shape)

# 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.Flatten(),
    tf.keras.layers.Dense(126, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax'),
    # YOUR CODE ENDS HERE
])

# YOUR CODE STARTS HERE
model.compile(
    optimizer = tf.train.AdamOptimizer(),
    metrics = ['accuracy'],
    loss='sparse_categorical_crossentropy',
)

callbacks = [MyCallback()]
model.fit(
    x = training_images,
    y = training_labels,
    epochs=20,
    callbacks=callbacks
)

model.evaluate(
    x = test_images,
    y = test_labels,
)

classifications = model.predict(test_images)
print(classifications[432])
print(test_labels[432])
# YOUR CODE ENDS HERE<jupyter_output>(60000, 28, 28, 1)
Epoch 1/20
59968/60000 [============================>.] - ETA: 0s - loss: 0.1505 - acc: 0.95510 {'loss': 0.15041063682946065, 'acc': 0.9550833}
60000/60000 [==============================] - 44s 733us/sample - loss: 0.1504 - acc: 0.9551
Epoch 2/20
59904/60000 [============================>.] - ETA: 0s - loss: 0.0524 - acc: 0.98361 {'loss': 0.052418376106768846, 'acc': 0.9836}
60000/60000 [==============================] - 44s 736us/sample - loss: 0.0524 - acc: 0.9836
Epoch 3/20
59968/60000 [============================>.] - ETA: 0s - loss: 0.0338 - acc: 0.98942 {'loss': 0.03378397941125246, 'acc': 0.9894}
60000/60000 [==============================] - 44s 728us/sample - loss: 0.0338 - acc: 0.9894
Epoch 4/20
59968/60000 [============================>.] - ETA: 0s - loss: 0.0229 - acc: 0.99293 {'loss': 0.02286953876625339, 'acc': 0.9929}
60000/60000 [==============================] - 44s 732us/sample - loss: 0.0229 - acc: 0.9929
Epoch 5/20
59904/60000 [=================[...]
StarcoderdataJupyterStructuredCleanDedup