# What Is Early Stopping?

> What early stopping is, how a held-back score and patience decide when to stop training, and why you keep the best checkpoint, for your AI interview.

**In short:** Early stopping means ending training once the score on held-back data stops improving, instead of running every planned round. You keep the version that scored best, and it also limits overfitting, where a model memorises its training data.

AI and LLM interview question Q429, Classic ML and deep-learning basics. Beginner, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/what-is-early-stopping

## Say it in 30 seconds

Early stopping is a way to decide when to stop training a model. I hold back some data the model never trains on, and I score the model on it after every round. If that score stops improving for a set number of rounds, I stop, and I keep the version that scored best, even if it came before the last round. It is a simple guard against a model that memorises its training data. It needs a held-back set, and a patience setting that is not too short.

## Say it in 2 minutes

Training a model means repeating a pass over the training data, round after round. Each pass is an epoch. Early on, the model gets better at the real task, and its score on held-back data goes up. Later, it starts learning quirks of the training data, and the held-back score can slip. Early stopping watches that score and stops at the right point.

In practice, I set aside a validation set, which is data the model never learns from. After each epoch I score the model on it. I also set a patience, which is how many epochs in a row I will wait without a real gain. A tiny improvement that is only noise doesn't count. When the patience runs out, I stop and load the checkpoint with the best validation score, which may be several epochs back.

Take a made-up fraud model at a card company. Its validation loss fell to 0.34 at epoch 6, then slipped back to 0.36 by epoch 12. With a patience of 3, training stops at epoch 9, and the epoch 6 checkpoint is kept. Running all 12 epochs would have kept a worse model. The costs are real: the held-back data is no longer used for training, and a patience that is too short can stop a model that would have improved later.

The same rule shows up in other tools. Scikit-learn has an early stopping option for its neural networks, and Hugging Face's Trainer has an early stopping callback that stops training when a chosen metric gets worse. I keep a separate test set out of all of this, so the final score is fair.

## What is early stopping, in plain words?

Early stopping means you stop training a model at the point where it stops getting better on data it has not learnt from, instead of running every planned round. The held-back data is never used for training, so it shows whether the model really learnt the pattern.

Think of a fraud analyst who writes a new rule each week and checks it against last month's cases. When the catch rate stops rising for three weeks running, she stops tuning and goes back to the rule that caught the most fraud. A card company's fraud model works the same way. It is scored on held-back transactions after every round, and training stops when that score stops improving.

- **Epoch:** One full pass through the training data.
- **Validation set:** Examples held back from training, used only to score the model.
- **Patience:** How many rounds in a row to wait for a real gain before stopping.
- **Checkpoint:** A saved copy of the model at one point in training.

## How does the stopping rule work?

1. **Hold back some data:** Set aside a validation set before training starts. The model never learns from it.
2. **Score after each epoch:** After every pass, measure the loss on the validation set. Loss is a number for how wrong the model is, so lower is better.
3. **Save when it really improves:** If the loss drops below the best so far by a clear margin, save a checkpoint and reset the wait count to zero.
4. **Count the misses:** If it does not beat the best, add one to the wait count.
5. **Stop and keep the best:** When the wait count reaches the patience limit, stop training and load the best checkpoint.

*Diagram, Watching the validation score: Training data teaches the model, and validation data only scores it. After each epoch, a real gain resets the wait count, and a run of misses stops training and keeps the best checkpoint.*
1. Training examples are what the model learns from.
2. Held-back examples only score the model, they never teach it.
3. The score after this epoch is checked against the best so far.
4. A real gain resets the count. No gain adds one.
5. When the count hits the limit, training stops and the best version is kept.

The margin matters. Validation scores bounce about from one round to the next, so a gain counts only when it is bigger than a small minimum. Scikit-learn calls that minimum gain tol, and the patience n_iter_no_change. Its rule is quoted below.

## Why does it help, and what does it cost?

Early stopping is a way to stop a model overfitting. Google's machine learning glossary describes it as a method for regularization, though not a formal one, and scikit-learn writes out the patience rule:

> "A method for regularization that involves ending training before training loss finishes decreasing." (Google machine learning glossary)
>
> "In early stopping, you intentionally stop training the model when the loss on a validation dataset starts to increase; that is, when generalization performance worsens." (Google machine learning glossary)
>
> "terminate training when validation score is not improving by at least tol for n_iter_no_change consecutive epochs" (Scikit-learn, MLPClassifier)

The same glossary adds that early stopping is not a formal regularization method, but it can effectively limit overfitting. A model trained for too long learns the quirks of its training data, and the held-back score is what catches that.

The cost has two sides. First, you give up some training data to validation. When Scikit-learn's neural network turns early stopping on, it holds back 10% of the training data by default (validation_fraction=0.1). Second, a patience that is too short can stop a model that would have improved after a noisy patch.

*Diagram, Two endings for the same run: The same made-up run, two endings. Running to the last epoch keeps a model that scores worse on validation than the epoch 6 checkpoint that early stopping keeps.*
1. The run goes on to epoch 12 and keeps that model.
2. Training stops at epoch 9, and the epoch 6 model is kept.

## What does the stop rule look like in code?

Here is the rule in plain Python. The validation losses are made up, one per epoch, and the code prints where it stops and which checkpoint it keeps.

```python
# A made-up fraud model: validation loss after each epoch (lower is better).
val_loss = [0.62, 0.48, 0.41, 0.37, 0.35, 0.34, 0.345, 0.343, 0.346, 0.349, 0.35, 0.36]
PATIENCE = 3       # epochs in a row without a gain before we stop
MIN_DELTA = 0.001  # smaller gains are treated as noise

best, best_epoch, waited = float("inf"), 0, 0
for epoch, loss in enumerate(val_loss, start=1):
    if loss < best - MIN_DELTA:
        best, best_epoch, waited = loss, epoch, 0
    else:
        waited += 1
        if waited == PATIENCE:
            print(f"Stop after epoch {epoch}: no gain for {PATIENCE} epochs")
            break
print(f"Keep the checkpoint from epoch {best_epoch} (loss {best:.3f})")
```

Output:

```text
Stop after epoch 9: no gain for 3 epochs
Keep the checkpoint from epoch 6 (loss 0.340)
```

> **Tip:** Keep the checkpoint and the stop point apart in your head. Training stopped at epoch 9, but the model you ship is the one from epoch 6.

## Where do real tools use it?

- **Scikit-learn:** Its neural network can switch early stopping on, and then it sets aside part of the training data as the validation set for you. It is off by default.
- **Histogram gradient boosting:** Scikit-learn's histogram-based gradient boosting turns early stopping on by default once a training set has 10,000 samples or more, and then it uses the validation loss.
- **Hugging Face Trainer:** Its early stopping callback stops training when a chosen metric gets worse for a set number of evaluations. The patience default is 1, so set it higher on noisy runs. It needs load_best_model_at_end switched on, and it waits for the next save step if save steps and eval steps differ.
- **Fine-tuning a language model:** With load_best_model_at_end on, the Trainer uses loss as its default metric, so the callback can stop a fine-tuning run when that loss stops improving.

> **Tip:** Turn on load_best_model_at_end, which needs an evaluation schedule set, and the Trainer loads the best checkpoint at the end for you. That is the same idea as restoring the epoch 6 model.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A plain definition: stop training when a held-back score stops improving.
- [ ] The validation set, kept apart from the training data and the test set.
- [ ] Patience and a minimum gain, because validation scores are noisy.
- [ ] Keeping the best checkpoint as the final model.
- [ ] It limits overfitting, at the cost of some data and a risk of stopping too soon.
- [ ] Where it shows up: model libraries and fine-tuning runs.

**Common traps**

- Trap: "Train until the training loss stops falling." Better: Training loss can keep falling even while the model gets worse on new data. Stop on the held-back score.
- Trap: "Stop when the test score is at its best." Better: Picking the stop point with the test set spoils it as a final check. Use a validation set for the stop rule.
- Trap: "Just keep the last model." Better: The last epoch is often not the best one. Restore the best checkpoint.

## Can you spot the safe stop rule?

**Spot the rule:** Which stop rule is safe for a fraud model?

A. if test_loss < best: save(model)
B. if val_loss < best - 0.001: save(model); waited = 0
C. if train_loss < best: save(model)
D. if no gain for 1 epoch: stop, keep the last model

**Answer:** B. The second rule scores a held-back validation set, needs a small real gain, and keeps the best checkpoint. The test set stays for the final check, training loss can keep falling, and one missed epoch is too short a patience.

**Quick check:** Your patience is 3. The best validation score was at epoch 6, and epochs 7, 8 and 9 gave no gain. Which model do you keep?

A. The model from epoch 9, the last one trained
B. The model from epoch 6, the best checkpoint
C. The model from epoch 12, after all planned rounds
D. An average of the models from epochs 6 to 9

**Answer:** B. Epochs 7 to 9 failed to beat epoch 6, so epoch 6 is the best checkpoint. The last model trained is often not the best one.

## Can you answer these from memory?

**Flash cards**

- **What is early stopping?** Ending training when a held-back validation score stops improving.
- **What is patience?** How many rounds in a row to wait for a real gain before stopping.
- **Which data decides when to stop?** The validation set, never the training data or the test set.
- **Which model do you keep?** The best checkpoint, which may be from before the last epoch.
- **Does early stopping limit overfitting?** Yes. Google says it can effectively limit overfitting, though it is not a formal regularization method.
- **Where does it show up?** Scikit-learn's neural networks and boosted trees, and Hugging Face's Trainer.

## Likely follow-up questions

- Q413 Overfitting vs underfitting? https://designervenkat.online/interview/ai/overfitting-vs-underfitting
- Q414 L1, L2 and dropout regularisation? https://designervenkat.online/interview/ai/l1-l2-and-dropout-regularisation
- Q422 Train/validation/test splits? https://designervenkat.online/interview/ai/train-validation-test-splits
- Q423 What is cross-validation? https://designervenkat.online/interview/ai/what-is-cross-validation
- Q297 How do you spot overfitting during fine-tuning? https://designervenkat.online/interview/ai/how-do-you-spot-overfitting-during-fine-tuning
- Q436 How do you tune hyperparameters? https://designervenkat.online/interview/ai/how-do-you-tune-hyperparameters
