Deep learning: Early stopping with patience

Decide automatically when to stop training.

What to build

A program that applies early stopping to a sequence of validation losses.

Requirements

  • Epochs are numbered from 1. An epoch counts as an improvement if its validation loss is lower than the best so far minus min_delta (the first epoch always improves). After each epoch without improvement, add 1 to a counter; an improvement resets it. Stop after the epoch at which the counter reaches the patience.
  • Print stopped at epoch E (or ran all N epochs if it never stops), then best epoch B, loss L with the loss to four decimal places.

Input

Line 1: the patience and min_delta. Line 2: the validation losses, one per epoch.

Output

Two lines.

Constraints

At most 1,000 epochs.

Example

Input:

2 0
1.0 0.8 0.7 0.75 0.72 0.6

Output:

stopped at epoch 5
best epoch 3, loss 0.7000

Why it works

Validation loss is noisy, so waiting a few epochs avoids stopping on a single bad epoch, while keeping the best epoch's weights gives the model that generalized best.

Examples

Example 1 · Input
2 0
1.0 0.8 0.7 0.75 0.72 0.6
Expected output
stopped at epoch 5
best epoch 3, loss 0.7000
Example 2 · Input
3 0
1.0 0.9 0.8 0.7
Expected output
ran all 4 epochs
best epoch 4, loss 0.7000

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Keep the best validation loss so far and its epoch; an epoch improves only if its loss is below best − min_delta.

Hint 2

Count the epochs since the last improvement; stop when the count reaches the patience.

Solution

Show one correct solution (try it yourself first)
patience, delta = input().split()
patience, delta = int(patience), float(delta)
losses = list(map(float, input().split()))
best, best_epoch, waited, stopped = None, 0, 0, None
for epoch, loss in enumerate(losses, start=1):
    if best is None or loss < best - delta:
        best, best_epoch, waited = loss, epoch, 0
    else:
        waited += 1
        if waited >= patience:
            stopped = epoch
            break
print(f"stopped at epoch {stopped}" if stopped else f"ran all {len(losses)} epochs")
print(f"best epoch {best_epoch}, loss {best:.4f}")

Lesson: Overfitting and regularization →