Early stopping on validation loss
Background
Early stopping is a simple, effective regularizer: you monitor validation loss each epoch and stop training once it stops improving, restoring the best checkpoint. This prevents the model from over-fitting in later epochs while saving compute. The key knob is patience — how many epochs of no improvement to tolerate before halting.
Problem statement
Implement early_stopping(val_losses, patience, min_delta) that scans the per-epoch validation losses and returns a tuple (stop_epoch, best_epoch):
- Track the best (lowest) loss seen. An epoch counts as an improvement only if its loss is below
best_loss - min_delta. - Count consecutive non-improving epochs. When that count reaches
patience, stop and return the current epoch asstop_epoch. - If the count never reaches
patience,stop_epochis the last epoch index. best_epochis the epoch index of the lowest qualifying loss.
Input
val_losses—list[float], validation loss at each epoch (0-indexed).patience—int, allowed consecutive epochs without improvement.min_delta—float, minimum decrease in loss to count as an improvement.
Output
A tuple (stop_epoch, best_epoch) of two ints.
Examples
Example 1
Input: val_losses = [0.9, 0.8, 0.75, 0.77, 0.76, 0.77, 0.78], patience = 2, min_delta = 0.01
Output: (4, 2)
Explanation: the best loss 0.75 occurs at epoch 2. Epochs 3 and 4 do not improve on it by at least min_delta, so after 2 non-improving epochs training stops at epoch 4. The best epoch remains 2.
Constraints
- Improvement test is strict:
loss < best_loss - min_delta. - Reset the no-improvement counter to 0 whenever an improvement occurs.
- Stop as soon as the counter is
>= patience.
Notes
min_deltaguards against declaring "improvement" for negligible noise-level decreases.- Returning
best_epochlets the trainer roll back to the checkpoint with the lowest validation loss, not the (worse) checkpoint at the stopping epoch.
This problem ships 4 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.
- •Reference example
- •Monotonically decreasing loss never triggers early stop
- •Patience of 1 stops at first non-improvement
- •min_delta ignores tiny improvements