How to monitor a filtered version of a metric in EarlyStopping callback in tensorflow?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
EarlyStopping can only monitor values that appear in the logs dictionary at the end of an epoch. If you want to stop based on a smoothed or otherwise filtered version of a metric, the practical solution is to compute that derived metric in a custom callback, add it to logs, and then tell EarlyStopping to monitor the derived name.
How EarlyStopping Sees Metrics
At on_epoch_end, Keras passes a logs dictionary to each callback. That dictionary contains values such as:
- '
loss' - '
accuracy' - '
val_loss' - '
val_accuracy'
EarlyStopping does not know how you got those values. It just looks up the metric named in monitor.
That means you can create your own filtered metric as long as you insert it into logs before EarlyStopping runs.
Example: Exponential Moving Average of val_loss
Suppose raw val_loss is noisy and you want an exponentially smoothed version called val_loss_ema.
The important detail is callback order: FilterMetric must come before EarlyStopping, otherwise the filtered value will not be present when EarlyStopping checks logs.
Why Filtering Can Help
Validation curves can bounce around from epoch to epoch because of:
- small validation sets
- stochastic training noise
- data augmentation effects
- unstable objectives
A filtered metric can reduce false early stops caused by one unlucky noisy epoch.
But smoothing also delays the signal. If the filter is too aggressive, training may continue long after the model has clearly stopped improving.
Alternatives to Injecting into logs
Another valid design is to subclass EarlyStopping itself and override on_epoch_end so it transforms the metric before applying the stopping logic. That works, but the injected-logs approach is often simpler because:
- the filtered metric becomes visible in the history and logs
- you can reuse standard
EarlyStopping - you can monitor different filtered metrics without reimplementing stop logic
For many teams, that makes debugging much easier.
Window Average Versus Exponential Smoothing
An exponential moving average is not the only option. You could also maintain:
- a fixed-size moving average
- a median filter
- a clipped or winsorized metric
The mechanism is the same: compute the derived value inside a callback and write it into logs under a custom key.
What History Will Contain
Because you add the filtered metric to logs, Keras can include it in the training history. That is useful for plotting and for checking whether your filter is actually improving stop behavior rather than just hiding problems.
If the filtered metric does not appear, the usual cause is callback ordering or a mismatch in the metric name.
Common Pitfalls
The biggest mistake is monitoring a filtered metric name that never gets added to logs. EarlyStopping can only monitor names it actually sees.
Another mistake is placing the filtering callback after EarlyStopping in the callback list. In that order, the derived metric arrives too late.
People also make the smoothing factor too aggressive. A filter that is too slow can mask real degradation and delay stopping too much.
Finally, make sure the mode of EarlyStopping matches the metric. For loss-like metrics, use mode="min". For accuracy-like metrics, use mode="max".
Summary
- '
EarlyStoppingcan monitor only metrics present inlogsat epoch end.' - To stop on a filtered metric, compute the filtered value in a custom callback and add it to
logs. - Put the filtering callback before
EarlyStoppingin the callback list. - Exponential moving averages are a common and simple filter choice.
- Tune the filter strength carefully so you smooth noise without hiding real regressions.
Related reading
- How to monitor dead relus
- How to monitor gradient vanish and explosion in keras with tensorboard?
- How to multiply matrices in the result of using chain rule in the backpropagation algorithm
- how to normalize input data for models in tensorflow
- How to obtain filenames during prediction while using tf.keras.preprocessing.image_dataset_from_directory?
- How to optimize for inference a simple, saved TensorFlow 1.0.1 graph?
- How to monitor the status of model training running on the server via fast-api
- How to normalize a confusion matrix?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.