C1 W3 Lab Assignment Error

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.

Hi @Sweacha_Lakshmi ,

You need to pass the callback to the model.fit as well.
Try that and let us know incase it helps!


this error is showing up

Can you ping the Convolution model?
I think you have a model.fit inside the model as well?

Removed since it contained code :slight_smile:

Hi @Sweacha_Lakshmi ,

I saw that you are calling callbacks but if you see the code, then callbacks have not been defined. So you would get the same error.
I would suggest you look into scopes of a variable and arguments of a function incase you are not familiar with them.