v9.1.1
Menu

Early Stopping Check

easyFREERegularization & TuningDeep LearningDeep Learning 1

Problem Statement

Determine if training should stop based on validation loss history.

Background

Early stopping is a regularization technique that stops training when the model stops improving on a validation set. This prevents overfitting to the training data.

The rule: stop if validation loss hasn't improved for patience consecutive epochs.

Your Task

Write a function should_stop(history_losses, patience) that returns True if training should stop.

Output Format

Return a boolean: True if should stop, False otherwise.

Example

Input
history_losses=[5.0, 4.0, 4.1, 4.2], patience=2
Output
True
Reasoning
  • Inputs: history_losses is the validation loss after each epoch, in order, so epochs are numbered 0,1,2,30, 1, 2, 3 with losses 5.0,4.0,4.1,4.25.0, 4.0, 4.1, 4.2. patience =2= 2 is how many epochs in a row we tolerate without a new best.
  • Best so far: the lowest loss is 4.04.0, reached at epoch 11. Lower is better, so that is the last time the model improved.
  • What happened since: epochs 22 and 33 gave 4.14.1 and 4.24.2, both worse than 4.04.0. The latest epoch is 33, so the number of epochs without improvement is 3−1=23 - 1 = 2.
  • Decision: stop when that gap reaches the patience, i.e. 2≥22 \ge 2, so the output is True.
  • Notice: with patience =3= 3 the same history gives False, since 2<32 < 3. A new minimum at the last epoch resets the gap to 00, and a loss merely equal to the best is not an improvement.

Constraints

  • len(history_losses) >= 1
  • patience >= 1
  • All losses are positive numbers

Learn this in

More from Deep Learning 1

solution.py

Test results

Sign in to run your code against the test cases.