Neural Mastery
← Practice
Early Stopping Patience Checkmedium

Early Stopping Patience Check

medium ML Systems & MLOps 15 min

NumPy allowed · plain Python earns 10 bonus points

Implement patience-based early stopping, the real, standard mechanism training loops use to halt before overfitting.

Task

Implement should_stop_early(val_losses, patience, min_delta=0.0). Track the best (minimum) loss seen so far as you walk val_losses in order; a loss counts as an improvement only if it's < best - min_delta. Count consecutive epochs WITHOUT improvement. Return True if that counter ever reaches patience, else False after processing the whole list.

Function Signature

should_stop_early(val_losses: list[float], patience: int, min_delta: float = 0.0) -> bool

Examples

Example 1: steady improvement, no stop
Input: val_losses = [1, 0.8, 0.6, 0.4] patience = 2 min_delta = 0
Output: False
Example 2: plateau triggers stop
Input: val_losses = [1, 0.5, 0.5, 0.5, 0.5] patience = 2 min_delta = 0
Output: True
Example 3: min_delta filters noise improvements
Input: val_losses = [1, 0.999, 0.998, 0.997] patience = 2 min_delta = 0.1
Output: True

Constraints

  • patience >= 1
  • min_delta >= 0
Python3Saved ✓
def should_stop_early(val_losses, patience, min_delta=0.0):
# Your implementation here
pass

Case 1: steady improvement, no stop
Input: val_losses = [1, 0.8, 0.6, 0.4] patience = 2 min_delta = 0
Expected: False
Case 2: plateau triggers stop
Input: val_losses = [1, 0.5, 0.5, 0.5, 0.5] patience = 2 min_delta = 0
Expected: True
Case 3: min_delta filters noise improvements
Input: val_losses = [1, 0.999, 0.998, 0.997] patience = 2 min_delta = 0.1
Expected: True