Shortcuts

early_stopping

Classes

EarlyStopping

Monitor a metric and stop training when it stops improving.

Early Stopping

Monitor a metric and stop training when it stops improving.

class pytorch_lightning.callbacks.early_stopping.EarlyStopping(monitor=None, min_delta=0.0, patience=3, verbose=False, mode='min', strict=True, check_finite=True, stopping_threshold=None, divergence_threshold=None, check_on_train_epoch_end=False)[source]

Bases: pytorch_lightning.callbacks.base.Callback

Monitor a metric and stop training when it stops improving.

Parameters
  • monitor (Optional[str]) – quantity to be monitored.

  • 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.

  • patience (int) –

    number of checks with no improvement after which training will be stopped. Under the default configuration, one check happens after every training epoch. However, the frequency of validation can be modified by setting various parameters on the Trainer, for example check_val_every_n_epoch and val_check_interval.

    Note

    It must be noted that the patience parameter counts the number of validation checks with no improvement, and not the number of training epochs. Therefore, with parameters check_val_every_n_epoch=10 and patience=3, the trainer will perform at least 40 training epochs before being stopped.

  • verbose (bool) – verbosity mode.

  • mode (str) – one of 'min', 'max'. In 'min' mode, training will stop when the quantity monitored has stopped decreasing and in 'max' mode it will stop when the quantity monitored has stopped increasing.

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

  • check_finite (bool) – When set True, stops training when the monitor becomes NaN or infinite.

  • stopping_threshold (Optional[float]) – Stop training immediately once the monitored quantity reaches this threshold.

  • divergence_threshold (Optional[float]) – Stop training as soon as the monitored quantity becomes worse than this threshold.

  • check_on_train_epoch_end (bool) – whether to run early stopping at the end of the training epoch. If this is False, then the check runs at the end of the validation epoch.

Raises
  • MisconfigurationException – If mode is none of "min" or "max".

  • RuntimeError – If the metric monitor is not available.

Example:

>>> from pytorch_lightning import Trainer
>>> from pytorch_lightning.callbacks import EarlyStopping
>>> early_stopping = EarlyStopping('val_loss')
>>> trainer = Trainer(callbacks=[early_stopping])
on_load_checkpoint(callback_state)[source]

Called when loading a model checkpoint, use to reload state.

Parameters
  • trainer – the current Trainer instance.

  • pl_module – the current LightningModule instance.

  • callback_state (Dict[str, Any]) – the callback state returned by on_save_checkpoint.

Note

The on_load_checkpoint won’t be called with an undefined state. If your on_load_checkpoint hook behavior doesn’t rely on a state, you will still need to override on_save_checkpoint to return a dummy state.

Return type

None

on_save_checkpoint(trainer, pl_module, checkpoint)[source]

Called when saving a model checkpoint, use to persist state.

Parameters
  • trainer – the current Trainer instance.

  • pl_module – the current LightningModule instance.

  • checkpoint (Dict[str, Any]) – the checkpoint dictionary that will be saved.

Return type

Dict[str, Any]

Returns

The callback state.

on_train_epoch_end(trainer, pl_module)[source]

Called when the train epoch ends.

To access all batch outputs at the end of the epoch, either:

  1. Implement training_epoch_end in the LightningModule and access outputs via the module OR

  2. Cache data across train batch hooks inside the callback implementation to post-process in this hook.

Return type

None

on_validation_end(trainer, pl_module)[source]

Called when the validation loop ends.

Return type

None

Read the Docs v: latest
Versions
latest
stable
1.3.6
1.3.5
1.3.4
1.3.3
1.3.2
1.3.1
1.3.0
1.2.10
1.2.9_a
1.2.8
1.2.7
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.1.8
1.1.7
1.1.6
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.8
1.0.7
1.0.6
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
0.10.0
0.9.0
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.6
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.0
0.5.3
0.4.9
ipynb-update
Downloads
pdf
html
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.