Skip to content

Commit

Permalink
Updated EarlyStopping in rst doc
Browse files Browse the repository at this point in the history
  • Loading branch information
baldassarreFe committed May 12, 2020
1 parent 3b39be2 commit 38ed30d
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions docs/source/early_stopping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,44 @@ By default early stopping will be enabled if `'val_loss'`
is found in :meth:`~pytorch_lightning.core.lightning.LightningModule.validation_epoch_end`'s
return dict. Otherwise training will proceed with early stopping disabled.

Enable Early Stopping using Callbacks on epoch end
--------------------------------------------------
There are two ways to enable early stopping using callbacks on epoch end.
Enable Early Stopping using the EarlyStopping Callback
------------------------------------------------------
The
:class:`~pytorch_lightning.callbacks.early_stopping.EarlyStopping`
callback can be used to monitor a validation metric and stop the training when no improvement is observed.

There are two ways to enable the EarlyStopping callback:

.. doctest::

>>> from pytorch_lightning import Trainer
>>> from pytorch_lightning.callbacks import EarlyStopping

# A) Set early_stop_callback to True. Will look for 'val_loss'
# in validation_epoch_end() return dict. If it is not found an error is raised.
# A) Set early_stop_callback=True.
# The callback will look for 'val_loss' in the dict returned by validation_epoch_end().
# If `val_loss` is not found an error is raised.
>>> trainer = Trainer(early_stop_callback=True)
# B) Or configure your own callback
# B) Create the callback object and pass it to the trainer.
# This allows for further customization of the callback.
>>> early_stop_callback = EarlyStopping(
... monitor='val_loss',
... monitor='val_accuracy',
... min_delta=0.00,
... patience=3,
... verbose=False,
... mode='min'
... mode='max'
... )
>>> trainer = Trainer(early_stop_callback=early_stop_callback)

In any case, the callback will fall back to the training metrics (returned in
:meth:`~pytorch_lightning.core.lightning.LightningModule.training_step`,
:meth:`~pytorch_lightning.core.lightning.LightningModule.training_step_end`)
looking for a key to monitor if validation is disabled or
:meth:`~pytorch_lightning.core.lightning.LightningModule.validation_epoch_end`
is not defined.
.. note::
The EarlyStopping callback runs at the end of every validation epoch,
which, under the default configuration, happen after every training epoch.
However, the frequency of validation can be modified by setting various parameters on the
:class:`~pytorch_lightning.trainer.trainer.Trainer`
, for example `check_val_every_n_epoch` and `val_check_interval`.
It must be noted that the `patience` parameter counts the number of
validation epochs 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.

.. seealso::
:class:`~pytorch_lightning.trainer.trainer.Trainer`
Expand Down

0 comments on commit 38ed30d

Please sign in to comment.