delu.EarlyStopping#

class delu.EarlyStopping(patience, *, mode, min_delta=0.0)[source]#

Performs early stopping after N consequtive bad (non-improving) updates.

Example of stopping the training after validation loss not improving for ten consequtive epochs:

early_stopping = delu.EarlyStopping(10, mode='min')
while epoch < n_epochs and not early_stopping.should_stop():
    train_epoch()
    metrics = computer_metrics()
    early_stopping.update(metrics['val']['loss'])

where the method EarlyStopping.update is used to submit a new score, and the method EarlyStopping.should_stop returns True if the number of the latest consequtive bad updates reaches patience (10 in the above example). Regardless of the number of the latest consequtive bad updates, if the next update is good, then the number of consequtive bad updates is reset to zero.

Other examples:
early_stopping = delu.EarlyStopping(2, mode='max')
early_stopping.update(1.0)  # the number of bad updates: 0
assert not early_stopping.should_stop()
early_stopping.update(0.0)  # the number of bad updates: 1
assert not early_stopping.should_stop()
early_stopping.update(2.0)  # the number of bad updates: 0
early_stopping.update(0.0)  # the number of bad updates: 1
early_stopping.update(0.0)  # the number of bad updates: 2
assert early_stopping.should_stop()

early_stopping.forget_bad_updates()  # the number of bad updates: 0
assert not early_stopping.should_stop()
early_stopping.update(0.0)  # the number of bad updates: 1
early_stopping.update(0.0)  # the number of bad updates: 2
assert early_stopping.should_stop()

early_stopping.reset()
assert not early_stopping.should_stop()
early_stopping.update(0.0)  # the number of bad updates: 0
early_stopping.update(0.0)  # the number of bad updates: 1
early_stopping.update(0.0)  # the number of bad updates: 2
assert early_stopping.should_stop()

Methods

__init__

Initialize self.

forget_bad_updates

Reset the counter of consecutive bad updates to zero.

reset

Reset everything.

should_stop

Check whether early stopping condition is activated.

update

Submit a new value.

Parameters:
  • patience (int) –

  • mode (Literal['min', 'max']) –

  • min_delta (float) –