A custom callback is a user-defined function or class that allows you to insert your own code at specific points during model training, such as at the start or end of an epoch or batch.
Click to reveal answer
beginner
Which method in a custom callback runs at the end of each training epoch?
The method on_epoch_end(self, epoch, logs=None) runs at the end of each training epoch and can be used to perform actions like logging or adjusting learning rates.
Click to reveal answer
intermediate
How do you create a custom callback in TensorFlow?
You create a custom callback by subclassing tf.keras.callbacks.Callback and overriding the methods you want, such as on_train_begin, on_epoch_end, or on_batch_end.
Click to reveal answer
intermediate
Why use custom callbacks instead of built-in callbacks?
Custom callbacks let you add personalized behavior during training that built-in callbacks don’t support, like saving extra data, custom logging, or stopping training based on your own rules.
Click to reveal answer
beginner
What is the purpose of the logs parameter in callback methods?
The logs parameter is a dictionary that contains training metrics like loss and accuracy for the current batch or epoch, which you can use inside your callback to monitor or react to training progress.
Click to reveal answer
Which base class should you extend to create a custom callback in TensorFlow?
Atf.keras.Model
Btf.keras.callbacks.Callback
Ctf.data.Dataset
Dtf.keras.layers.Layer
✗ Incorrect
Custom callbacks are created by subclassing tf.keras.callbacks.Callback.
Which method is called at the start of training in a custom callback?
Aon_train_begin
Bon_epoch_end
Con_train_end
Don_batch_begin
✗ Incorrect
The on_train_begin method runs at the start of training.
What does the logs dictionary typically contain during training?
ATraining metrics like loss and accuracy
BModel architecture details
CHardware specifications
DDataset samples
✗ Incorrect
The logs dictionary contains training metrics such as loss and accuracy.
Which method would you override to execute code after each batch during training?
Aon_epoch_end
Bon_train_begin
Con_batch_end
Don_train_end
✗ Incorrect
The on_batch_end method runs after each batch.
Why might you want to stop training early using a custom callback?
ATo increase the batch size
BTo load a different dataset
CTo change the model architecture
DTo save time if the model is not improving
✗ Incorrect
Stopping early can save time if the model stops improving.
Explain how to create and use a custom callback in TensorFlow to print the loss after each epoch.
Think about which method runs after each epoch and how to get loss info.
You got /5 concepts.
Describe three practical uses of custom callbacks during model training.
Consider what you might want to do automatically during training.
You got /5 concepts.
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
Step 1: Understand what callbacks do
Callbacks let you add custom actions during training, like logging or stopping early.
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.
Final Answer:
To run your own code at specific points during model training -> Option A
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
Step 1: Recall callback method names
Common callback methods include on_train_begin, on_epoch_end, on_batch_begin, on_train_end.
Step 2: Match method to event
on_epoch_end runs code after each epoch finishes, which is what the question asks.
Final Answer:
on_epoch_end -> Option B
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
Step 1: Understand on_epoch_end behavior
The method prints epoch number +1 and loss from logs after each epoch.
Step 2: Check training epochs and output
Training runs 2 epochs, so prints for epoch 1 and 2 with loss values (numbers).
Final Answer:
Epoch 1 ended with loss X\nEpoch 2 ended with loss Y (X and Y are numbers) -> Option D
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
Step 1: Check logs usage
logs can be None if no metrics are passed, so logs['loss'] may cause KeyError.
Step 2: Confirm correct way to stop training
Setting self.model.stop_training = True is correct; no method call needed.
Final Answer:
logs dictionary might be None causing KeyError -> Option C
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
Step 1: Identify when to check validation accuracy
Validation accuracy is updated at epoch end, so on_epoch_end is the right method.
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.
Final Answer:
Override on_epoch_end, check logs['val_accuracy'], save weights if improved -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.