TensorFlow 1.10 custom estimator early stopping with train_and_evaluate
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
In TensorFlow 1.10, the usual way to combine a custom estimator with early stopping is to attach a stopping hook to the TrainSpec used by train_and_evaluate. The important detail is that early stopping is driven by evaluation results written during the evaluation phase, so your monitored metric must exist in the estimator's eval output.
train_and_evaluate Does Not Stop by Itself
tf.estimator.train_and_evaluate coordinates alternating training and evaluation, but it does not automatically stop when validation metrics stall. You have to add that behavior explicitly.
For TensorFlow 1.10-era estimator workflows, the standard approach is an early-stopping hook such as tf.contrib.estimator.stop_if_no_decrease_hook.
Your Custom Estimator Must Expose Eval Metrics
The model function for a custom estimator should return an EstimatorSpec with:
- '
loss' - '
train_op' - '
eval_metric_ops'
A minimal custom estimator model function might look like this:
That mae metric can now be monitored by an early-stopping hook.
Add the Stop Hook to the TrainSpec
In TensorFlow 1.10, a common pattern is:
This tells training to stop after the monitored metric stops decreasing for the configured number of steps.
Choose the Metric Carefully
You can monitor built-in eval loss, or one of your custom metrics from eval_metric_ops. For example, if lower MAE is what matters, monitor mae instead of loss.
The key rule is simple: the metric name must exactly match what the evaluation phase writes. If the hook watches a metric that does not exist, early stopping will not behave the way you expect.
min_steps and run_every_steps Matter
Two parameters are especially important:
- '
min_stepsprevents premature stopping before the model has had a chance to learn anything' - '
run_every_stepscontrols how often the stopping condition is checked'
If min_steps is too small, noisy validation metrics can stop training too early. If run_every_steps is too large, early stopping becomes sluggish and wastes training work.
Why This Works with Custom Estimators
The estimator does not need special early-stopping logic inside model_fn. That logic sits outside the model in the training hook. Your job inside the custom estimator is simply to expose stable evaluation metrics and make sure train_and_evaluate is actually running evaluations often enough to provide signal.
Common Pitfalls
- Monitoring a metric name that is not present in
eval_metric_ops. - Forgetting that early stopping depends on evaluation runs, not only on training steps.
- Setting
min_stepsso low that early noise stops training prematurely. - Putting the hook in the wrong place instead of attaching it to
TrainSpec. - Expecting TensorFlow 1.x estimator hooks to behave exactly like modern Keras callbacks.
Summary
- In TensorFlow 1.10, early stopping with
train_and_evaluateis typically done with a stopping hook on theTrainSpec. - Your custom estimator must expose the metric you want to monitor during evaluation.
- '
lossis the simplest metric to monitor, but custom eval metrics can work too.' - '
min_stepsand evaluation frequency strongly affect the stopping behavior.' - The model function defines metrics; the early-stopping hook decides when training ends.
Related reading
- Tensorflow 1.11 needs CuDNN 7.2 for CUDA 9.0, but there is no such library
- TensorFlow 1.14.0 is not using GPU
- Tensorflow 1.14 performance issue on rtx 3090
- Tensorflow 1.15 CUDA cuDNN installation using Conda
- Tensorflow 1.8.0 Wide and Deep Model results are not stable. Random seed is not working
- Tensorflow 2.0.0-alpha0 tf.logging.set_verbosity
- Tensorflow 2.0 - AttributeError module 'tensorflow' has no attribute 'Session
- Tensorflow 2.0 - AttributeError module 'tensorflow' has no attribute 'Session
.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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.