Shortcuts

pytorch_lightning.callbacks.early_stopping module

Early Stopping

Monitor a validation metric and stop training when it stops improving.

class pytorch_lightning.callbacks.early_stopping.EarlyStopping(monitor='val_loss', min_delta=0.0, patience=3, verbose=False, mode='auto', strict=True)[source]

Bases: pytorch_lightning.callbacks.base.Callback

Parameters
  • monitor (str) – quantity to be monitored. Default: 'val_loss'.

  • min_delta (float) – minimum change in the monitored quantity to qualify as an improvement, i.e. an absolute change of less than min_delta, will count as no improvement. Default: 0.

  • patience (int) – number of validation epochs with no improvement after which training will be stopped. Default: 0.

  • verbose (bool) – verbosity mode. Default: False.

  • mode (str) – one of {auto, min, max}. In min mode, training will stop when the quantity monitored has stopped decreasing; in max mode it will stop when the quantity monitored has stopped increasing; in auto mode, the direction is automatically inferred from the name of the monitored quantity. Default: 'auto'.

  • strict (bool) – whether to crash the training if monitor is not found in the validation metrics. Default: True.

Example:

>>> from pytorch_lightning import Trainer
>>> from pytorch_lightning.callbacks import EarlyStopping
>>> early_stopping = EarlyStopping('val_loss')
>>> trainer = Trainer(early_stop_callback=early_stopping)
_run_early_stopping_check(trainer, pl_module)[source]
_stop_distributed_training(trainer, pl_module)[source]
_validate_condition_metric(logs)[source]

Checks that the condition metric for early stopping is good

Parameters

logs – callback metrics from validation output

Returns

True if specified metric is available

load_state_dict(state_dict)[source]
on_sanity_check_end(trainer, pl_module)[source]

Called when the validation sanity check ends.

on_train_end(trainer, pl_module)[source]

Called when the train ends.

on_validation_end(trainer, pl_module)[source]

Called when the validation loop ends.

state_dict()[source]
mode_dict = {'max': torch.gt, 'min': torch.lt}[source]
property monitor_op[source]