Week 2 project error

def train_mnist(x_train, y_train):


# Instantiate the callback class
callbacks = myCallback()

# Define the model, it should have 3 layers:
# - A Flatten layer that receives inputs with the same shape as the images
# - A Dense layer with 512 units and ReLU activation function
# - A Dense layer with 10 units and softmax activation function
model = tf.keras.models.Sequential([ 
    tf.keras.layers.Dense(512, activation=tf.nn.relu),
    tf.keras.layers.Dense(10, activation=tf.nn.softmax) ]),

# Compile the model

# Fit the model for 10 epochs adding the callbacks
# and save the training history
history = model.fit(x_train, y_train, epochs=10, callbacks=[Callbacks])


return history

Output Error

AttributeError Traceback (most recent call last)
----> 1 hist = train_mnist(x_train, y_train)

in train_mnist(x_train, y_train)
19 # Compile the model
—> 20 model.compile(optimizer=‘adam’,
21 loss=‘sparse_categorical_crossentropy’,
22 metrics=[‘accuracy’])

AttributeError: ‘tuple’ object has no attribute ‘compile’

Above is my code of week two buy on running the hist = train_mist… line of action, I’m getting the error message above and this seems a bit confusing. How can I correct this please.

There is a trailing comma at the end of model definition.
Also, callbacks=[callbacks] (your case is wrong)

I’m still stock at initiating this callback function


When you have a problem defining the training callback, I can give you the following hints:

  1. In homework there is already a hint:
# Remember to inherit from the correct class
class myCallback():

Check the reference I mention :point_down: you will get it quite easily.

  1. To point # Define the correct function signature for on_epoch_end
    I can give official Keras reference to this method definition, check here Writing your own callbacks  |  TensorFlow Core

  2. The homework wants you to stop training when certain accuracy has been reached. (according to homework comment # Stop training once the above condition is met) When you had finished the point 1. :point_up: , then you can stop model quite straight forward. Here is the official Keras reference to guide you how to stop training. كتابة الاسترجاعات الخاصة بك  |  TensorFlow Core

Hopefully, it helps :smile: