machine learning
model training
early stopping
validation accuracy
neural networks

How to stop training when it hits a specific validation accuracy?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

If you want training to stop as soon as validation accuracy reaches a target, the clearest solution in Keras is a custom callback. Built-in EarlyStopping is designed for “stop when progress stalls,” which is related but not the same as “stop when metric reaches 0.95.” A small callback lets you express the exact threshold directly.

Use a Custom Callback for a Hard Threshold

Keras callbacks receive the metric values for each epoch in the logs dictionary. That makes it easy to stop the model once val_accuracy crosses a target.

python
1import tensorflow as tf
2
3
4class StopAtValidationAccuracy(tf.keras.callbacks.Callback):
5    def __init__(self, target, monitor='val_accuracy'):
6        super().__init__()
7        self.target = target
8        self.monitor = monitor
9
10    def on_epoch_end(self, epoch, logs=None):
11        logs = logs or {}
12        value = logs.get(self.monitor)
13
14        if value is not None and value >= self.target:
15            print(f"\nStopping: {self.monitor} reached {value:.4f}")
16            self.model.stop_training = True

Then pass the callback to model.fit.

python
1model = tf.keras.Sequential([
2    tf.keras.layers.Dense(16, activation='relu', input_shape=(10,)),
3    tf.keras.layers.Dense(2, activation='softmax')
4])
5
6model.compile(
7    optimizer='adam',
8    loss='sparse_categorical_crossentropy',
9    metrics=['accuracy']
10)
11
12callback = StopAtValidationAccuracy(target=0.95)
13
14history = model.fit(
15    x_train,
16    y_train,
17    validation_data=(x_val, y_val),
18    epochs=100,
19    callbacks=[callback]
20)

This is explicit and easy to maintain. Anyone reading the training code can see the target immediately.

Why EarlyStopping Is Not the Same Thing

tf.keras.callbacks.EarlyStopping stops when a monitored value stops improving enough. That is useful for preventing overtraining, but it does not mean “stop at the first moment validation accuracy is at least X.”

You can combine both patterns when needed. For example, one callback can stop at a success threshold, while EarlyStopping acts as a fallback when the model plateaus.

python
1early_stopping = tf.keras.callbacks.EarlyStopping(
2    monitor='val_accuracy',
3    patience=5,
4    mode='max',
5    restore_best_weights=True
6)
7
8callback = StopAtValidationAccuracy(target=0.95)

That combination works well when you want either a success cutoff or a safe termination if the target is never reached.

Monitor the Correct Metric Name

The callback only works if the monitored name matches what Keras logs. For standard classification, val_accuracy is common. Depending on your compile configuration, you may instead see names such as val_sparse_categorical_accuracy.

Check history.history.keys() after a small run if you are unsure.

python
history = model.fit(...)
print(history.history.keys())

This avoids the easy mistake of waiting for a metric name that never appears.

Batch-Level Stopping vs Epoch-Level Stopping

The example above stops at the end of an epoch because validation metrics are usually computed there. If you need finer control during training batches, you can implement on_train_batch_end, but that only has access to training metrics unless you run validation manually.

For most use cases, epoch-level stopping is the right design because the validation score is the value you actually trust for the decision.

Pick a Threshold That Matches the Problem

A hard stop at 0.95 sounds simple, but it only makes sense if the dataset, label quality, and model capacity make that score realistic. For noisy or imbalanced problems, a threshold-based stop may need to target a different validation metric such as precision, recall, or AUC instead.

Common Pitfalls

  • Using EarlyStopping when the real requirement is a fixed threshold.
  • Monitoring the wrong metric name.
  • Forgetting to supply validation_data, which means no validation metric exists.
  • Treating a noisy validation accuracy spike as a final success without considering stability.
  • Stopping at a threshold that is unrealistic for the model and dataset.

Summary

  • Use a custom Keras callback to stop when validation accuracy reaches a specific target.
  • Set self.model.stop_training = True once the monitored value crosses the threshold.
  • 'EarlyStopping is useful for plateau detection, not exact threshold checks.'
  • Confirm the metric name that Keras actually logs.
  • Combine a success-threshold callback with EarlyStopping when you want both behaviors.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.