Bird
Raised Fist0
TensorFlowml~20 mins

Custom callbacks in TensorFlow - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Callback Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1:30remaining
Output 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})
AEpoch 1: loss = None
BEpoch 0: loss = 0.25
CEpoch 1: loss = 0.25
DEpoch 0: loss = None
Attempts:
2 left
💡 Hint
Remember that epoch numbering in callbacks starts at 0, but the print adds 1.
❓ Model Choice
intermediate
1:00remaining
Choosing the correct callback to stop training early
Which TensorFlow callback is designed to stop training when the monitored metric stops improving?
Atf.keras.callbacks.ModelCheckpoint
Btf.keras.callbacks.LearningRateScheduler
Ctf.keras.callbacks.TensorBoard
Dtf.keras.callbacks.EarlyStopping
Attempts:
2 left
💡 Hint
This callback watches a metric and stops training if no improvement is seen.
❓ Hyperparameter
advanced
1:30remaining
Setting 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?
Apatience=3
Bmin_delta=3
Cverbose=3
Dmonitor='val_accuracy'
Attempts:
2 left
💡 Hint
Patience controls how many epochs to wait before stopping.
🔧 Debug
advanced
2:00remaining
Debugging 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
AThe callback does not check if logs is None before accessing 'val_loss', causing an error.
BThe model attribute is not set in the callback, so self.model is None.
CThe callback should use on_train_end instead of on_epoch_end to save weights.
DThe file extension '.h5' is invalid for save_weights method.
Attempts:
2 left
💡 Hint
Check if logs can be None during callback calls.
🧠 Conceptual
expert
2:00remaining
Understanding callback order of execution
During model training, in what order are the following callback methods called within one epoch?
A2, 3, 1, 4
B1, 2, 3, 4
C2, 1, 3, 4
D1, 3, 2, 4
Attempts:
2 left
💡 Hint
Epoch callbacks wrap batch callbacks in the training loop.

Practice

(1/5)
1. What is the main purpose of creating a custom callback in TensorFlow?
easy
A. To run your own code at specific points during model training
B. To change the model architecture automatically
C. To increase the dataset size during training
D. To convert the model to a different format

Solution

  1. Step 1: Understand what callbacks do

    Callbacks let you add custom actions during training, like logging or stopping early.
  2. Step 2: Identify the role of custom callbacks

    Custom callbacks let you run your own code at points like start/end of epochs or batches.
  3. Final Answer:

    To run your own code at specific points during model training -> Option A
  4. Quick Check:

    Custom callbacks = run code during training [OK]
Hint: Callbacks run code during training steps [OK]
Common Mistakes:
  • Thinking callbacks change model structure
  • Confusing callbacks with data augmentation
  • Assuming callbacks convert model formats
2. Which method should you override in a custom callback to execute code at the end of each training epoch?
easy
A. on_train_begin
B. on_epoch_end
C. on_batch_begin
D. on_train_end

Solution

  1. Step 1: Recall callback method names

    Common callback methods include on_train_begin, on_epoch_end, on_batch_begin, on_train_end.
  2. Step 2: Match method to event

    on_epoch_end runs code after each epoch finishes, which is what the question asks.
  3. Final Answer:

    on_epoch_end -> Option B
  4. Quick Check:

    End of epoch method = on_epoch_end [OK]
Hint: End of epoch callback method is on_epoch_end [OK]
Common Mistakes:
  • Using on_train_begin for epoch end
  • Confusing batch and epoch methods
  • Overriding on_train_end instead
3. What will be printed after training if this custom callback is used?
import tensorflow as tf
class MyCallback(tf.keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs=None):
        print(f"Epoch {epoch+1} ended with loss {logs['loss']:.2f}")

model = tf.keras.Sequential([tf.keras.layers.Dense(1)])
model.compile(optimizer='sgd', loss='mse')
model.fit([[1.0]], [[2.0]], epochs=2, callbacks=[MyCallback()])
medium
A. Error because logs is None
B. Epoch 0 ended with loss X\nEpoch 1 ended with loss Y
C. No output printed during training
D. Epoch 1 ended with loss X\nEpoch 2 ended with loss Y (X and Y are numbers)

Solution

  1. Step 1: Understand on_epoch_end behavior

    The method prints epoch number +1 and loss from logs after each epoch.
  2. Step 2: Check training epochs and output

    Training runs 2 epochs, so prints for epoch 1 and 2 with loss values (numbers).
  3. Final Answer:

    Epoch 1 ended with loss X\nEpoch 2 ended with loss Y (X and Y are numbers) -> Option D
  4. Quick Check:

    Prints after each epoch with loss [OK]
Hint: Epoch count starts at 0, print adds 1 [OK]
Common Mistakes:
  • Assuming epoch numbering starts at 1 internally
  • Expecting no print output
  • Thinking logs is None causing error
4. Identify the error in this custom callback code that tries to stop training when loss is below 0.1:
class StopCallback(tf.keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs=None):
        if logs['loss'] < 0.1:
            self.model.stop_training = True

model.fit(x_train, y_train, epochs=10, callbacks=[StopCallback()])
medium
A. on_epoch_end method must return a value
B. stop_training should be called as a method
C. logs dictionary might be None causing KeyError
D. Callback class must inherit from tf.keras.Model

Solution

  1. Step 1: Check logs usage

    logs can be None if no metrics are passed, so logs['loss'] may cause KeyError.
  2. Step 2: Confirm correct way to stop training

    Setting self.model.stop_training = True is correct; no method call needed.
  3. Final Answer:

    logs dictionary might be None causing KeyError -> Option C
  4. Quick Check:

    Check logs is not None before accessing [OK]
Hint: Always check logs is not None before using keys [OK]
Common Mistakes:
  • Calling stop_training as a method
  • Expecting on_epoch_end to return a value
  • Inheriting from wrong base class
5. You want to save the model weights only when validation accuracy improves during training. Which custom callback method and logic should you implement?
hard
A. Override on_epoch_end, check logs['val_accuracy'], save weights if improved
B. Override on_train_begin, save weights before training starts
C. Override on_batch_end, save weights after every batch
D. Override on_train_end, save weights after training finishes

Solution

  1. Step 1: Identify when to check validation accuracy

    Validation accuracy is updated at epoch end, so on_epoch_end is the right method.
  2. Step 2: Implement logic to save weights only if val_accuracy improves

    Inside on_epoch_end, compare current val_accuracy with previous best and save if better.
  3. Final Answer:

    Override on_epoch_end, check logs['val_accuracy'], save weights if improved -> Option A
  4. Quick Check:

    Save on epoch end if val_accuracy improves [OK]
Hint: Use on_epoch_end to monitor validation metrics [OK]
Common Mistakes:
  • Saving weights too often (every batch)
  • Saving only before or after all training
  • Not checking if accuracy improved before saving
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard