Keras early stopping callback error, val_loss metric not available
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The Keras error saying val_loss is not available appears when EarlyStopping monitors a metric that training never logs. Most often, training is run without validation data, so no validation metrics are produced. Fixing this is usually as simple as adding validation input or changing the monitored metric.
Why val_loss Is Missing
val_loss exists only if model training runs validation each epoch. If fit has no validation_data and no validation_split, callbacks cannot read val_loss.
At this point, callback monitoring val_loss will fail unless validation is configured.
Correct Usage with Validation Split
If data is in arrays, provide validation_split.
Now history contains val_loss, so early stopping can monitor it.
Correct Usage with Explicit Validation Data
For train and validation sets prepared separately, pass validation_data.
This is preferred when you need deterministic split control.
Monitoring Training Loss Instead
If validation is intentionally disabled, monitor loss rather than val_loss.
This can still prevent overtraining loops, though it does not guard against overfitting as well as validation-based monitoring.
Inspect Available Metrics from History
When callback names are uncertain, inspect training logs.
Use these exact names in callback monitor settings.
Common Related Misconfigurations
Other callback issues that look similar:
- typo in monitor string such as
val_los - monitor metric not produced because metric was never compiled
- custom training loop not logging metric expected by callback
Always align callback monitor names with actual logs.
Practical Callback Configuration
A robust early stopping setup often includes:
patiencetuned to noise levelmin_deltato ignore tiny fluctuationsrestore_best_weights=Trueto keep best epoch state
This avoids stopping too early due to minor oscillations.
Using Validation with tf.data Pipelines
If you train with tf.data.Dataset, ensure a separate validation dataset is passed to validation_data. Without it, validation metrics are not emitted and monitor keys with val_ prefix will remain unavailable. Keep train and validation pipelines deterministic when debugging callback behavior.
This pattern is common in production training code and avoids ambiguity about where validation metrics originate.
Log callback configuration and monitored keys at training start to catch monitor mismatches early.
During experimentation, save callback history and final selected epoch so stopping behavior can be audited. This helps differentiate real convergence from accidental early stops due to misconfigured monitor settings.
Common Pitfalls
- Monitoring
val_losswithout providing any validation data infit. - Using wrong monitor key that does not exist in
history.history. - Assuming callbacks can infer validation metrics from training loss.
- Running custom loops without logging metrics expected by callbacks.
- Forgetting to restore best weights and evaluating final, not best, epoch model.
Summary
val_lossappears only when validation is run during training.- Add
validation_splitorvalidation_datato enable validation metrics. - If no validation is used, monitor
lossinstead. - Verify available metric names from training history.
- Tune early stopping parameters for stable and meaningful stopping behavior.

