mirror of
https://github.com/wassname/pytorch-lightning.git
synced 2026-09-09 11:32:07 +08:00
Improved docs for callbacks (#1370)
* improved docs for callbacks * class references * make doctest pass * doctests * fix lines too long * fix line too long * fix permission error in doctest * Apply suggestions from code review Co-Authored-By: Jirka Borovec <Borda@users.noreply.github.com> * fix doctest * fix default Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
This commit is contained in:
co-authored by
Jirka Borovec
parent
22bedf9b57
commit
1f2da71069
+20
-18
@@ -7,7 +7,7 @@ Callbacks
|
||||
=========
|
||||
|
||||
Lightning has a callback system to execute arbitrary code. Callbacks should capture NON-ESSENTIAL
|
||||
logic that is NOT required for your LightningModule to run.
|
||||
logic that is NOT required for your :class:`~pytorch_lightning.core.LightningModule` to run.
|
||||
|
||||
An overall Lightning system should have:
|
||||
|
||||
@@ -15,27 +15,29 @@ An overall Lightning system should have:
|
||||
2. LightningModule for all research code.
|
||||
3. Callbacks for non-essential code.
|
||||
|
||||
Example
|
||||
|
||||
.. code-block:: python
|
||||
Example:
|
||||
|
||||
import pytorch_lightning as pl
|
||||
.. doctest::
|
||||
|
||||
class MyPrintingCallback(pl.Callback):
|
||||
>>> import pytorch_lightning as pl
|
||||
>>> class MyPrintingCallback(pl.Callback):
|
||||
...
|
||||
... def on_init_start(self, trainer):
|
||||
... print('Starting to init trainer!')
|
||||
...
|
||||
... def on_init_end(self, trainer):
|
||||
... print('trainer is init now')
|
||||
...
|
||||
... def on_train_end(self, trainer, pl_module):
|
||||
... print('do something when training ends')
|
||||
...
|
||||
>>> trainer = pl.Trainer(callbacks=[MyPrintingCallback()])
|
||||
Starting to init trainer!
|
||||
trainer is init now
|
||||
|
||||
def on_init_start(self, trainer):
|
||||
print('Starting to init trainer!')
|
||||
|
||||
def on_init_end(self, trainer):
|
||||
print('trainer is init now')
|
||||
|
||||
def on_train_end(self, trainer, pl_module):
|
||||
print('do something when training ends')
|
||||
|
||||
# pass to trainer
|
||||
trainer = pl.Trainer(callbacks=[MyPrintingCallback()])
|
||||
|
||||
We successfully extended functionality without polluting our super clean LightningModule research code
|
||||
We successfully extended functionality without polluting our super clean
|
||||
:class:`~pytorch_lightning.core.LightningModule` research code.
|
||||
|
||||
---------
|
||||
|
||||
|
||||
@@ -11,24 +11,23 @@ Enable Early Stopping
|
||||
---------------------
|
||||
There are two ways to enable early stopping.
|
||||
|
||||
.. seealso::
|
||||
:class:`~pytorch_lightning.trainer.trainer.Trainer`
|
||||
.. doctest::
|
||||
|
||||
.. code-block:: python
|
||||
>>> 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.
|
||||
trainer = Trainer(early_stop_callback=True)
|
||||
|
||||
>>> trainer = Trainer(early_stop_callback=True)
|
||||
# B) Or configure your own callback
|
||||
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)
|
||||
>>> 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)
|
||||
|
||||
In any case, the callback will fall back to the training metrics (returned in
|
||||
:meth:`~pytorch_lightning.core.lightning.LightningModule.training_step`,
|
||||
@@ -37,6 +36,8 @@ looking for a key to monitor if validation is disabled or
|
||||
:meth:`~pytorch_lightning.core.lightning.LightningModule.validation_epoch_end`
|
||||
is not defined.
|
||||
|
||||
.. seealso::
|
||||
:class:`~pytorch_lightning.trainer.trainer.Trainer`
|
||||
|
||||
Disable Early Stopping
|
||||
----------------------
|
||||
|
||||
Reference in New Issue
Block a user