Callback didn't worked, but solution got accepted

Hi,

Although my solution got accepted, but callback never worked for me. I am not sure what’s wrong with the code (or the way I am calling it), so just want to check with you guys if faced similar issue.

Here is my code:


And this is how I am calling it, see the output, training didn’t stop after reaching 97% accuracy.

Hi @Shambhu_Kumar_Gupta,

If you take a look at your log, it reads logs and accuracy. If you change acc by accuracy in your callback function it should work.

Best,

Maybe take a look at the code samples provided here:
Writing your own callbacks to see a way you could debug this. Specifically, iterating on the keys as


def on_epoch_end(self, epoch, logs=None):
        keys = list(logs.keys())
        print("End epoch {} of training; got log keys: {}".format(epoch, keys))

This would expose the difference between the lookup key currently used in your custom callback acc versus the metric name used in the model compile function accuracy. Hope this helps.

Great! You were right, it worked now.

Unfortunately, it’s not working with “acc”. Although it worked when I replaced “acc” with “accuracy”

Just FYI, logs in the callback function is a Python dictionary. You use a key to lookup and retrieve a value from that dictionary. You can use either the string ‘acc’ or the string ‘accuracy’ as long as the same string is used in both callback and in model compile. That is…

model.compile(…,metrics=[‘accuracy’])
#and
logs[‘accuracy’]

Or


model.compile(…,metrics=[‘acc’])
#and
logs[‘acc’]

Under the covers of the compile function. …
You can also pass a list to specify a metric or a list of metrics for each output, such as metrics=[['accuracy'], ['accuracy', 'mse']] or metrics=['accuracy', ['accuracy', 'mse']] . When you pass the strings ‘accuracy’ or ‘acc’, we convert this to one of tf.keras.metrics.BinaryAccuracy , tf.keras.metrics.CategoricalAccuracy , tf.keras.metrics.SparseCategoricalAccuracy based on the loss function used and the model output shape.

So while ‘change acc to accuracy’ fixes the problem you experienced, it doesn’t tell the whole story. Hope this helps.

PS: if you look at the list of dictionary keys you printed out, right now you should see accuracy and not acc, so the lookup using acc of course won’t work. But if you put metrics=[‘acc’] into the compile() call, it will.

Thanks @ai_curious , it indeed helped and brought more clarity. Thanks for feedback and helping me to learn :slight_smile:

1 Like