Help me demystify myCallback()?

In the final lab (and before) we define the class myCallback() which has method on_epoch_end(). A few questions

  • Perhaps my understanding of inheritance is wrong; how is it that our code which defines on_epoch_end() as it does, maintains the parent class behavior, namely to look at this method after the end of each epoch? The documentation says “Subclasses should override for any actions to run.” Is this standard inheritance in Python or is this special?
  • The method on_epoch_end includes arguments: self, epoch, and logs. However, it is not obvious to me that these are instantiated with values the exist after the end of a given epoch, i.e. such that logs.get(‘accuracy’) refers to the most recent epoch’s log’s accuracy value. Is this an unspoken convention or is this explained somewhere?

Thanks for any help!

The super class is tensorflow.keras.callack.Callback and so the on_epoch_end method gets invoked correctly.

You can print the items of interest and observe that the callback gets triggered. Here’s pdb if you want to debug your program.

Thanks Balaji. I was looking for something a little more specific so I put my questions into ChatGPT. I’m pasting excerpts from its answer below, which make sense to me. Pls shout if anything looks off? Thanks!

Regarding inheritance as it works here:

By default, a subclass inherits all the methods and attributes of its parent class, including its behavior. In the case of the myCallback class, it is inheriting from the tf.keras.callbacks.Callback class, which provides the behavior of executing the on_epoch_end() method after each epoch. The on_epoch_end() method in the subclass can override or extend the behavior of the parent class, but if it doesn’t, the parent class behavior is still executed.

Regarding logs.get(‘accuracy’) referring to the most recent epoch’s log’s accuracy value:

The on_epoch_end() method in tf.keras.callbacks.Callback class is called by the training loop after the end of each epoch, passing in the current epoch number and a dictionary of logs containing the training and validation metrics. When you define a new class that inherits from tf.keras.callbacks.Callback and override the on_epoch_end() method, the method signature needs to match the parent class method signature, including the self , epoch , and logs parameters. The epoch parameter is an integer representing the current epoch number, and the logs parameter is a dictionary containing the values of the metrics logged during training and validation for the current epoch. The keys of the logs dictionary are the names of the metrics, and the values are the corresponding metric values for the current epoch. Therefore, logs.get('accuracy') will give you the accuracy value for the most recent epoch. This convention is well-documented in the TensorFlow documentation.

ChatGPT response is pretty detailed and good in this case.

While I don’t see any harm in using chatgpt as an assistant, you still need to be in a position to understand / verify responses before moving forward.

Here’s a video talking about chatgpt performing illegal moves to win a game against stockfish.

Here’s github copilot to assist in coding. Some companies have privacy issues due to chatgpt. Here’s an article about it.