GRADED FUNCTION: reshape_and_normalize
def reshape_and_normalize(images):
### START CODE HERE
# Reshape the images to add an extra dimension
images = images[..., np.newaxis]
# Normalize pixel values
images = images / np.max(images)
### END CODE HERE
return images
GRADED CLASS: myCallback
def train_mnist_conv():
START CODE HERE
Remember to inherit from the correct class
class Callbackclass(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if logs.get('accuracy') >= 0.995:
print("\nReached 99.5% accuracy so cancelling training!")
self.model.stop_training = True
# Define the method that checks the accuracy at the end of each epoch
pass
mnist = tf.keras.datasets.mnist
(training_images, training_labels), (test_images, test_labels) = mnist.load_data()
callbacks=Callbackclass()
training_images=training_images.reshape(60000, 28, 28, 1)
training_images=training_images / 255.0
test_images = test_images.reshape(10000, 28, 28, 1)
test_images=test_images/255.0
END CODE HERE
GRADED FUNCTION: convolutional_model
def convolutional_model():
### START CODE HERE
model = tf.keras.models.Sequential([
# Define the model
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(128, activation=‘relu’),
tf.keras.layers.Dense(10, activation=‘softmax’)
# YOUR CODE ENDS HERE
])
### END CODE HERE
# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# model fitting
history= model.fit(
# YOUR CODE STARTS HERE
training_images, training_labels, epochs=10
# YOUR CODE ENDS HERE
)
# model fitting
return history.epoch, history.history['accuracy'][-1]
Save your untrained model
model = convolutional_model()
Instantiate the callback class
callbacks=Callbackclass()
Train your model (this can take up to 5 minutes)
history=model.fit(training_images,training_labels,epoch=10)
This is the code i have written but am getting error “history is not defined” or “callback is not defined” and also am getting epoch to 99.8
myCallBack is not working…, where am doing mistake am unable to find it out.