Challenge - 5 Problems
Callback Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of a simple custom callback during training
Consider the following TensorFlow custom callback that prints the loss at the end of each epoch. What will be printed after the first epoch if the loss is 0.25?
TensorFlow
import tensorflow as tf class LossPrinter(tf.keras.callbacks.Callback): def on_epoch_end(self, epoch, logs=None): print(f"Epoch {epoch + 1}: loss = {logs['loss']}") model = tf.keras.Sequential([tf.keras.layers.Dense(1, input_shape=(1,))]) model.compile(optimizer='sgd', loss='mse') callback = LossPrinter() # Simulate training with logs callback.on_epoch_end(0, {'loss': 0.25})
Attempts:
2 left
💡 Hint
Remember that epoch numbering in callbacks starts at 0, but the print adds 1.
✗ Incorrect
The callback method on_epoch_end receives epoch starting at 0, so adding 1 prints 'Epoch 1'. The logs dictionary contains the loss value 0.25.
❓ Model Choice
intermediateChoosing the correct callback to stop training early
Which TensorFlow callback is designed to stop training when the monitored metric stops improving?
Attempts:
2 left
💡 Hint
This callback watches a metric and stops training if no improvement is seen.
✗ Incorrect
EarlyStopping monitors a metric like validation loss and stops training if it does not improve after a set number of epochs.
❓ Hyperparameter
advancedSetting patience in EarlyStopping callback
You want to stop training if validation accuracy does not improve for 3 consecutive epochs. Which parameter and value should you set in EarlyStopping?
Attempts:
2 left
💡 Hint
Patience controls how many epochs to wait before stopping.
✗ Incorrect
Setting patience=3 means training stops if no improvement after 3 epochs. min_delta controls minimum change to qualify as improvement.
🔧 Debug
advancedDebugging a custom callback that does not save best model
A custom callback tries to save the best model weights when validation loss improves, but it never saves. What is the likely bug?
TensorFlow
class SaveBestModel(tf.keras.callbacks.Callback): def __init__(self): super().__init__() self.best_loss = float('inf') def on_epoch_end(self, epoch, logs=None): if logs is None: return current_loss = logs.get('val_loss') if current_loss is not None and current_loss < self.best_loss: self.model.save_weights('best_weights.h5') self.best_loss = current_loss # Model training with this callback
Attempts:
2 left
💡 Hint
Check if logs can be None during callback calls.
✗ Incorrect
If logs is None, logs.get('val_loss') raises AttributeError. Adding a check for logs prevents this.
🧠 Conceptual
expertUnderstanding callback order of execution
During model training, in what order are the following callback methods called within one epoch?
Attempts:
2 left
💡 Hint
Epoch callbacks wrap batch callbacks in the training loop.
✗ Incorrect
The order is: on_epoch_begin at start of epoch, then for each batch on_batch_begin and on_batch_end, finally on_epoch_end after all batches.
