Training too long overfits. Early stopping watches the validation loss and stops once it hasn't improved for a while.
Implement a class EarlyStopping(patience=3, min_delta=0.0) with:
step(val_loss) -> bool: record one epoch's validation loss and return True if training should stop now.
val_loss < best - min_delta. Then update best, remember this epoch as best_epoch (epochs count from 0) and reset the counter.patience.best (start at infinity) and best_epoch (start at None).Example (patience 2): losses 1.0, 0.8, 0.85, 0.9 → stop on the 4th call; best = 0.8, best_epoch = 1.