@@ -13,34 +13,44 @@ By default early stopping will be enabled if `'val_loss'`
13
13
is found in :meth: `~pytorch_lightning.core.lightning.LightningModule.validation_epoch_end `'s
14
14
return dict. Otherwise training will proceed with early stopping disabled.
15
15
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:
19
23
20
24
.. doctest ::
21
25
22
26
>>> from pytorch_lightning import Trainer
23
27
>>> from pytorch_lightning.callbacks import EarlyStopping
24
28
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.
27
32
>>> 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.
29
35
>>> early_stop_callback = EarlyStopping(
30
- ... monitor= ' val_loss ' ,
36
+ ... monitor= ' val_accuracy ' ,
31
37
... min_delta= 0.00 ,
32
38
... patience= 3 ,
33
39
... verbose= False ,
34
- ... mode= ' min '
40
+ ... mode= ' max '
35
41
... )
36
42
>>> trainer = Trainer(early_stop_callback = early_stop_callback)
37
43
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.
44
54
45
55
.. seealso ::
46
56
:class: `~pytorch_lightning.trainer.trainer.Trainer `
0 commit comments