2020-05-05 02:16:54 +00:00
.. testsetup :: *
from pytorch_lightning.trainer.trainer import Trainer
from pytorch_lightning.callbacks.early_stopping import EarlyStopping
2020-09-14 01:04:21 +00:00
.. _early_stopping:
2020-05-05 02:16:54 +00:00
2020-10-07 17:01:50 +00:00
***** ***** *** *
2020-02-11 04:55:22 +00:00
Early stopping
2020-10-07 17:01:50 +00:00
***** ***** *** *
2020-02-11 04:55:22 +00:00
2020-04-08 12:38:53 +00:00
Stopping an epoch early
2020-10-07 17:01:50 +00:00
=======================
2020-10-02 19:46:46 +00:00
You can stop an epoch early by overriding :meth: `~pytorch_lightning.core.hooks.ModelHooks.on_train_batch_start` to return `` -1 `` when some condition is met.
2020-04-08 12:38:53 +00:00
If you do this repeatedly, for every epoch you had originally requested, then this will stop your entire run.
2020-06-19 06:38:10 +00:00
----------
2020-10-07 17:01:50 +00:00
Early stopping based on metric using the EarlyStopping Callback
===============================================================
2020-05-25 17:33:00 +00:00
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.
2020-09-30 12:31:16 +00:00
To enable it:
2020-05-05 02:16:54 +00:00
2020-10-07 17:01:50 +00:00
- Import :class: `~pytorch_lightning.callbacks.early_stopping.EarlyStopping` callback.
- Log the metric you want to monitor using :func: `~~pytorch_lightning.core.lightning.LightningModule.log` method.
- Init the callback, and set `monitor` to the logged metric of your choice.
- Pass the :class: `~pytorch_lightning.callbacks.early_stopping.EarlyStopping` callback to the :class: `~pytorch_lightning.trainer.trainer.Trainer` callbacks flag.
2020-09-30 12:31:16 +00:00
2020-10-07 17:01:50 +00:00
.. code-block :: python
2020-09-30 12:31:16 +00:00
2020-10-07 17:01:50 +00:00
from pytorch_lightning.callbacks.early_stopping import EarlyStopping
def validation_step(...):
self.log('val_loss', loss)
2020-05-05 02:16:54 +00:00
2020-10-07 17:01:50 +00:00
trainer = Trainer(callbacks=[EarlyStopping(monitor='val_loss')])
2020-05-05 02:16:54 +00:00
2020-10-07 17:01:50 +00:00
- You can customize the callbacks behaviour by changing its parameters.
2020-02-11 04:55:22 +00:00
2020-10-07 17:01:50 +00:00
.. code-block :: python
2020-02-11 04:55:22 +00:00
2020-10-07 17:01:50 +00:00
early_stop_callback = EarlyStopping(
monitor='val_accuracy',
min_delta=0.00,
patience=3,
verbose=False,
mode='max'
)
trainer = Trainer(callbacks=[early_stop_callback])
2020-02-11 04:55:22 +00:00
2020-05-25 17:33:00 +00:00
In case you need early stopping in a different part of training, subclass EarlyStopping
and change where it is called:
.. testcode ::
class MyEarlyStopping(EarlyStopping):
def on_validation_end(self, trainer, pl_module):
# override this to disable early stopping at the end of val loop
pass
def on_train_end(self, trainer, pl_module):
# instead, do it at the end of training loop
self._run_early_stopping_check(trainer, pl_module)
.. 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 :paramref: `~pytorch_lightning.trainer.trainer.Trainer.check_val_every_n_epoch`
and :paramref: `~pytorch_lightning.trainer.trainer.Trainer.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.
2020-03-31 06:24:26 +00:00
2020-04-05 09:38:52 +00:00
.. seealso ::
2020-05-05 02:16:54 +00:00
- :class: `~pytorch_lightning.trainer.trainer.Trainer`
- :class: `~pytorch_lightning.callbacks.early_stopping.EarlyStopping`
2020-03-31 06:24:26 +00:00
2020-06-19 06:38:10 +00:00
----------
2020-03-20 19:49:01 +00:00
.. seealso ::
2020-05-05 02:16:54 +00:00
- :class: `~pytorch_lightning.trainer.trainer.Trainer`
- :class: `~pytorch_lightning.callbacks.early_stopping.EarlyStopping`