Custom callbacks let you track and control training steps like saving models or stopping early. The key metrics to watch depend on your goal. For example, if you want to stop training when validation loss stops improving, validation loss is the metric to monitor. If you save the best model, you track validation accuracy or validation loss. Custom callbacks help you react to these metrics during training.
Custom callbacks in TensorFlow - Model Metrics & Evaluation
Custom callbacks do not directly produce confusion matrices, but you can create callbacks to compute and display confusion matrices at the end of each epoch. For example, after predictions on validation data, you can build a confusion matrix like this:
Confusion Matrix:
Predicted
P N
Actual P TP FN
N FP TN
Where:
TP = True Positives
FP = False Positives
TN = True Negatives
FN = False Negatives
A callback can compute these counts and print or log them during training.
Custom callbacks can help monitor precision and recall during training. For example, if you want to improve recall (catch more positives), you can stop training early when recall stops improving. Or if you want to avoid false alarms, you can focus on precision.
Example: In a cancer detection model, recall is critical because missing a cancer case is dangerous. A custom callback can stop training if recall drops, even if accuracy is high.
Custom callbacks let you choose which metric to prioritize and act on it during training.
Good metric values depend on your task and what your callback monitors:
- Good: Validation loss steadily decreases, validation accuracy increases, and your callback saves the best model at the right time.
- Bad: Validation loss bounces up and down, accuracy does not improve, or your callback stops training too early or too late.
Custom callbacks help you automate these decisions based on metric trends.
- Accuracy paradox: High accuracy can be misleading if classes are imbalanced. Callbacks should monitor metrics like precision or recall instead.
- Data leakage: If validation data leaks into training, callbacks may save models that look good but fail in real use.
- Overfitting indicators: If validation loss increases while training loss decreases, callbacks can stop training early to avoid overfitting.
- Improper metric monitoring: Callbacks must monitor the correct metric (e.g., 'val_loss' vs 'loss') to work as intended.
No, this model is not good for fraud detection. Even though accuracy is high, recall is very low. This means the model misses most fraud cases, which is dangerous. For fraud detection, recall is critical because missing fraud is worse than false alarms. You should improve recall, possibly by adjusting the model or using a custom callback to monitor and stop training based on recall.
Practice
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 AQuick Check:
Custom callbacks = run code during training [OK]
- Thinking callbacks change model structure
- Confusing callbacks with data augmentation
- Assuming callbacks convert model formats
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 BQuick Check:
End of epoch method = on_epoch_end [OK]
- Using on_train_begin for epoch end
- Confusing batch and epoch methods
- Overriding on_train_end instead
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()])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 DQuick Check:
Prints after each epoch with loss [OK]
- Assuming epoch numbering starts at 1 internally
- Expecting no print output
- Thinking logs is None causing error
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()])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 CQuick Check:
Check logs is not None before accessing [OK]
- Calling stop_training as a method
- Expecting on_epoch_end to return a value
- Inheriting from wrong base class
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 AQuick Check:
Save on epoch end if val_accuracy improves [OK]
- Saving weights too often (every batch)
- Saving only before or after all training
- Not checking if accuracy improved before saving
Ready to prove it?
Pass the test and this pattern moves unknown
