2020-02-11 04:55:22 +00:00
Early stopping
2020-02-18 16:25:39 +00:00
==============
2020-02-11 04:55:22 +00:00
2020-04-08 12:38:53 +00:00
Stopping an epoch early
-----------------------
You can stop an epoch early by overriding :meth: `~pytorch_lightning.core.lightning.LightningModule.on_batch_start` to return `-1` when some condition is met.
If you do this repeatedly, for every epoch you had originally requested, then this will stop your entire run.
Default Epoch End Callback Behavior
-----------------------------------
2020-02-18 16:25:39 +00:00
By default early stopping will be enabled if `'val_loss'`
2020-03-31 06:24:26 +00:00
is found in :meth: `~pytorch_lightning.core.lightning.LightningModule.validation_epoch_end` 's
return dict. Otherwise training will proceed with early stopping disabled.
2020-02-11 04:55:22 +00:00
2020-04-08 12:38:53 +00:00
Enable Early Stopping using Callbacks on epoch end
--------------------------------------------------
There are two ways to enable early stopping using callbacks on epoch end.
2020-02-11 04:55:22 +00:00
2020-04-05 09:38:52 +00:00
.. doctest ::
2020-02-11 04:55:22 +00:00
2020-04-05 09:38:52 +00:00
>>> from pytorch_lightning import Trainer
>>> from pytorch_lightning.callbacks import EarlyStopping
2020-02-11 04:55:22 +00:00
2020-02-18 16:25:39 +00:00
# A) Set early_stop_callback to True. Will look for 'val_loss'
2020-03-06 00:31:57 +00:00
# in validation_epoch_end() return dict. If it is not found an error is raised.
2020-04-05 09:38:52 +00:00
>>> trainer = Trainer(early_stop_callback=True)
2020-02-11 04:55:22 +00:00
# B) Or configure your own callback
2020-04-05 09:38:52 +00:00
>>> early_stop_callback = EarlyStopping(
... monitor='val_loss',
... min_delta=0.00,
... patience=3,
... verbose=False,
... mode='min'
... )
>>> trainer = Trainer(early_stop_callback=early_stop_callback)
2020-02-11 04:55:22 +00:00
2020-03-31 06:24:26 +00:00
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.
2020-04-05 09:38:52 +00:00
.. seealso ::
:class: `~pytorch_lightning.trainer.trainer.Trainer`
2020-03-31 06:24:26 +00:00
2020-04-08 12:38:53 +00:00
Disable Early Stopping with callbacks on epoch end
--------------------------------------------------
2020-03-31 06:24:26 +00:00
To disable early stopping pass `` False `` to the
:paramref: `~pytorch_lightning.trainer.trainer.Trainer.early_stop_callback` .
2020-02-18 16:25:39 +00:00
Note that `` None `` will not disable early stopping but will lead to the
default behaviour.
2020-02-11 04:55:22 +00:00
2020-03-20 19:49:01 +00:00
.. seealso ::
:class: `~pytorch_lightning.trainer.trainer.Trainer`