[go: up one dir, main page]

0% found this document useful (0 votes)
134 views3 pages

Excercise Lab

The document describes an exercise to create an MNIST classifier that trains to 99% accuracy or higher without a fixed number of epochs. It provides starter code that loads and prepares the MNIST data, defines a callback to stop training once 99% accuracy is reached, creates a simple sequential model, and fits the model to the data using the callback. The code snippet shows the training process reaches 99% accuracy in 6 epochs, prints the callback message, and achieves 97.7% test accuracy.

Uploaded by

Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
134 views3 pages

Excercise Lab

The document describes an exercise to create an MNIST classifier that trains to 99% accuracy or higher without a fixed number of epochs. It provides starter code that loads and prepares the MNIST data, defines a callback to stop training once 99% accuracy is reached, creates a simple sequential model, and fits the model to the data using the callback. The code snippet shows the training process reaches 99% accuracy in 6 epochs, prints the callback message, and achieves 97.7% test accuracy.

Uploaded by

Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

8/18/2019 Zunnurain

Exercise 2 In the course you learned how to do classification 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:

It should succeed in less than 10 epochs, so it is okay to change epochs to 10, but nothing larger When it
reaches 99% or greater it should print out the string "Reached 99% accuracy so cancelling training!" 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?

https://hub.gke.mybinder.org/user/jupyterlab-jupyterlab-demo-6vqb31kd/lab 1/3
8/18/2019 Zunnurain

In [3]:

import tensorflow as tf

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

mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data()


x_train, x_test = x_train / 255.0, x_test / 255.0

callbacks = myCallback()

model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(256, activation=tf.nn.relu),
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=10, callbacks=[callbacks])


model.evaluate(x_test, y_test)
classifications = model.predict(x_test)
import matplotlib.pyplot as plt
plt.imshow(x_train[0])
print(y_train[0])
print(classifications[0])
print(y_test[0])
plt.imshow(x_train[99])
print(y_train[99])
print(classifications[99])
print(y_test[99])

https://hub.gke.mybinder.org/user/jupyterlab-jupyterlab-demo-6vqb31kd/lab 2/3
8/18/2019 Zunnurain

Epoch 1/10
60000/60000 [==============================] - 34s 562us/sample - loss: 0.
2241 - acc: 0.9350 - loss: 0.2241 - acc: 0.935
Epoch 2/10
60000/60000 [==============================] - 31s 517us/sample - loss: 0.
0938 - acc: 0.9716
Epoch 3/10
60000/60000 [==============================] - 35s 582us/sample - loss: 0.
0633 - acc: 0.9804
Epoch 4/10
60000/60000 [==============================] - 31s 518us/sample - loss: 0.
0459 - acc: 0.9854
Epoch 5/10
60000/60000 [==============================] - 27s 457us/sample - loss: 0.
0334 - acc: 0.9894
Epoch 6/10
59776/60000 [============================>.] - ETA: 0s - loss: 0.0258 - ac
c: 0.9923- ETA: 0s - loss: 0.0258 - acc: 0.99
Reached 99% accuracy so cancelling training!
60000/60000 [==============================] - 26s 440us/sample - loss: 0.
0258 - acc: 0.9923
10000/10000 [==============================] - 1s 128us/sample - loss: 0.0
765 - acc: 0.9773
5
[3.7922523e-10 2.7067446e-10 3.5988077e-08 1.4583147e-04 1.7217892e-13
1.2441346e-08 5.3674068e-15 9.9985254e-01 4.4293788e-08 1.5289303e-06]
7
1
[9.4441920e-11 3.8380736e-16 3.1181353e-09 3.0057208e-06 7.1631329e-10
1.1833143e-09 1.7540221e-14 4.0105104e-07 4.1659437e-08 9.9999654e-01]
9

https://hub.gke.mybinder.org/user/jupyterlab-jupyterlab-demo-6vqb31kd/lab 3/3

You might also like