Skip to content

Commit 38ed30d

Browse files
committed
Updated EarlyStopping in rst doc
1 parent 3b39be2 commit 38ed30d

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

docs/source/early_stopping.rst

+24-14
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,44 @@ By default early stopping will be enabled if `'val_loss'`
1313
is found in :meth:`~pytorch_lightning.core.lightning.LightningModule.validation_epoch_end`'s
1414
return dict. Otherwise training will proceed with early stopping disabled.
1515

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

2024
.. doctest::
2125

2226
>>> from pytorch_lightning import Trainer
2327
>>> from pytorch_lightning.callbacks import EarlyStopping
2428

25-
# A) Set early_stop_callback to True. Will look for 'val_loss'
26-
# in validation_epoch_end() return dict. If it is not found an error is raised.
29+
# A) Set early_stop_callback=True.
30+
# The callback will look for 'val_loss' in the dict returned by validation_epoch_end().
31+
# If `val_loss` is not found an error is raised.
2732
>>> trainer = Trainer(early_stop_callback=True)
28-
# B) Or configure your own callback
33+
# B) Create the callback object and pass it to the trainer.
34+
# This allows for further customization of the callback.
2935
>>> early_stop_callback = EarlyStopping(
30-
... monitor='val_loss',
36+
... monitor='val_accuracy',
3137
... min_delta=0.00,
3238
... patience=3,
3339
... verbose=False,
34-
... mode='min'
40+
... mode='max'
3541
... )
3642
>>> trainer = Trainer(early_stop_callback=early_stop_callback)
3743

38-
In any case, the callback will fall back to the training metrics (returned in
39-
:meth:`~pytorch_lightning.core.lightning.LightningModule.training_step`,
40-
:meth:`~pytorch_lightning.core.lightning.LightningModule.training_step_end`)
41-
looking for a key to monitor if validation is disabled or
42-
:meth:`~pytorch_lightning.core.lightning.LightningModule.validation_epoch_end`
43-
is not defined.
44+
.. note::
45+
The EarlyStopping callback runs at the end of every validation epoch,
46+
which, under the default configuration, happen after every training epoch.
47+
However, the frequency of validation can be modified by setting various parameters on the
48+
:class:`~pytorch_lightning.trainer.trainer.Trainer`
49+
, for example `check_val_every_n_epoch` and `val_check_interval`.
50+
It must be noted that the `patience` parameter counts the number of
51+
validation epochs with no improvement, and not the number of training epochs.
52+
Therefore, with parameters `check_val_every_n_epoch=10` and `patience=3`, the trainer
53+
will perform at least 40 training epochs before being stopped.
4454

4555
.. seealso::
4656
:class:`~pytorch_lightning.trainer.trainer.Trainer`

0 commit comments

Comments
 (0)