2020-05-05 02:16:54 +00:00
.. testsetup :: *
from pytorch_lightning.trainer.trainer import Trainer
from pytorch_lightning.callbacks.base import Callback
2020-01-21 20:18:32 +00:00
.. role :: hidden
:class: hidden-section
2020-03-20 19:49:01 +00:00
.. _callbacks:
2020-08-16 03:57:33 +00:00
Callback
========
2020-10-08 09:49:56 +00:00
.. raw :: html
2020-10-12 21:57:51 +00:00
<video width="100%" max-width="400px" controls
2020-10-08 09:49:56 +00:00
poster="https://pl-bolts-doc-images.s3.us-east-2.amazonaws.com/pl_docs/trainer_flags/thumb/callbacks.jpg"
src="https://pl-bolts-doc-images.s3.us-east-2.amazonaws.com/pl_docs/trainer_flags/callbacks.mp4"></video>
|
2020-08-13 13:58:05 +00:00
A callback is a self-contained program that can be reused across projects.
2020-02-27 22:21:51 +00:00
2020-08-13 13:58:05 +00:00
Lightning has a callback system to execute callbacks when needed. Callbacks should capture NON-ESSENTIAL
2020-10-13 10:42:33 +00:00
logic that is NOT required for your :ref: `lightning_module` to run.
2020-02-27 22:21:51 +00:00
2020-08-13 22:52:47 +00:00
Here's the flow of how the callback hooks are executed:
.. raw :: html
2020-10-12 21:57:51 +00:00
<video width="100%" max-width="400px" controls autoplay muted playsinline src="https://pl-bolts-doc-images.s3.us-east-2.amazonaws.com/pl_docs/pt_callbacks_mov.m4v"></video>
2020-08-13 22:52:47 +00:00
2020-02-27 22:21:51 +00:00
An overall Lightning system should have:
1. Trainer for all engineering
2. LightningModule for all research code.
3. Callbacks for non-essential code.
2020-08-02 02:56:34 +00:00
|
2020-02-27 22:21:51 +00:00
2020-04-05 09:38:52 +00:00
Example:
2020-05-05 02:16:54 +00:00
.. testcode ::
2020-10-13 00:22:28 +00:00
from pytorch_lightning.callbacks import Callback
2020-10-12 22:30:57 +00:00
2020-05-05 02:16:54 +00:00
class MyPrintingCallback(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 = Trainer(callbacks=[MyPrintingCallback()])
.. testoutput ::
2020-04-05 09:38:52 +00:00
Starting to init trainer!
trainer is init now
We successfully extended functionality without polluting our super clean
2020-10-13 10:42:33 +00:00
:ref: `lightning_module` research code.
2020-02-27 22:21:51 +00:00
2020-08-13 13:58:05 +00:00
-----------
Examples
--------
You can do pretty much anything with callbacks.
- `Add a MLP to fine-tune self-supervised networks <https://pytorch-lightning-bolts.readthedocs.io/en/latest/self_supervised_callbacks.html#sslonlineevaluator> `_ .
- `Find how to modify an image input to trick the classification result <https://pytorch-lightning-bolts.readthedocs.io/en/latest/vision_callbacks.html#confused-logit> `_ .
- `Interpolate the latent space of any variational model <https://pytorch-lightning-bolts.readthedocs.io/en/latest/variational_callbacks.html#latent-dim-interpolator> `_ .
- `Log images to Tensorboard for any model <https://pytorch-lightning-bolts.readthedocs.io/en/latest/vision_callbacks.html#tensorboard-image-generator> `_ .
--------------
Built-in Callbacks
------------------
Lightning has a few built-in callbacks.
.. note ::
For a richer collection of callbacks, check out our
`bolts library <https://pytorch-lightning-bolts.readthedocs.io/en/latest/callbacks.html> `_ .
2020-10-06 21:28:45 +00:00
.. currentmodule :: pytorch_lightning.callbacks
.. autosummary ::
:toctree: generated
:nosignatures:
:template: classtemplate.rst
2021-01-25 12:07:02 +00:00
BackboneLambdaFinetuningCallback
BaseFinetuningCallback
2020-10-06 21:28:45 +00:00
Callback
EarlyStopping
GPUStatsMonitor
GradientAccumulationScheduler
2021-01-13 09:42:49 +00:00
LambdaCallback
2020-10-06 21:28:45 +00:00
LearningRateMonitor
ModelCheckpoint
ProgressBar
ProgressBarBase
2020-08-02 02:56:34 +00:00
2020-08-13 13:58:05 +00:00
----------
2020-08-02 02:56:34 +00:00
2020-08-28 14:50:52 +00:00
Persisting State
----------------
Some callbacks require internal state in order to function properly. You can optionally
choose to persist your callback's state as part of model checkpoint files using the callback hooks
:meth: `~pytorch_lightning.callbacks.Callback.on_save_checkpoint` and :meth: `~pytorch_lightning.callbacks.Callback.on_load_checkpoint` .
However, you must follow two constraints:
1. Your returned state must be able to be pickled.
2. You can only use one instance of that class in the Trainer callbacks list. We don't support persisting state for multiple callbacks of the same class.
2020-08-02 02:56:34 +00:00
Best Practices
--------------
The following are best practices when using/designing callbacks.
1. Callbacks should be isolated in their functionality.
2. Your callback should not rely on the behavior of other callbacks in order to work properly.
3. Do not manually call methods from the callback.
4. Directly calling methods (eg. `on_validation_end` ) is strongly discouraged.
5. Whenever possible, your callbacks should not depend on the order in which they are executed.
2020-10-12 21:57:51 +00:00
-----------
.. _hooks:
Available Callback hooks
------------------------
2020-10-20 15:01:08 +00:00
setup
^^^^^
.. automethod :: pytorch_lightning.callbacks.Callback.setup
:noindex:
teardown
^^^^^^^^
.. automethod :: pytorch_lightning.callbacks.Callback.teardown
:noindex:
on_init_start
2020-10-12 21:57:51 +00:00
^^^^^^^^^^^^^^
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_init_start
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_init_end
^^^^^^^^^^^
2020-10-12 21:57:51 +00:00
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_init_end
2020-10-12 21:57:51 +00:00
:noindex:
on_fit_start
^^^^^^^^^^^^
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_save_checkpoint
2020-10-12 21:57:51 +00:00
:noindex:
on_fit_end
^^^^^^^^^^
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_fit_end
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_sanity_check_start
^^^^^^^^^^^^^^^^^^^^^
.. automethod :: pytorch_lightning.callbacks.Callback.on_sanity_check_start
:noindex:
on_sanity_check_end
^^^^^^^^^^^^^^^^^^^
.. automethod :: pytorch_lightning.callbacks.Callback.on_sanity_check_end
:noindex:
on_train_batch_start
^^^^^^^^^^^^^^^^^^^^
.. automethod :: pytorch_lightning.callbacks.Callback.on_train_batch_start
:noindex:
on_train_batch_end
2020-10-12 21:57:51 +00:00
^^^^^^^^^^^^^^^^^^
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_train_batch_end
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_train_epoch_start
^^^^^^^^^^^^^^^^^^^^
.. automethod :: pytorch_lightning.callbacks.Callback.on_train_epoch_start
:noindex:
on_train_epoch_end
2020-10-12 21:57:51 +00:00
^^^^^^^^^^^^^^^^^^
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_train_epoch_end
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_validation_epoch_start
2020-10-12 21:57:51 +00:00
^^^^^^^^^^^^^^^^^^^^^^^^^
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_validation_epoch_start
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_validation_epoch_end
2020-10-12 21:57:51 +00:00
^^^^^^^^^^^^^^^^^^^^^^^
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_validation_epoch_end
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_test_epoch_start
2020-10-12 21:57:51 +00:00
^^^^^^^^^^^^^^^^^^^
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_test_epoch_start
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_test_epoch_end
2020-10-12 21:57:51 +00:00
^^^^^^^^^^^^^^^^^
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_test_epoch_end
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_epoch_start
^^^^^^^^^^^^^^
2020-10-12 21:57:51 +00:00
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_epoch_start
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_epoch_end
^^^^^^^^^^^^
2020-10-12 21:57:51 +00:00
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_epoch_end
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_batch_start
^^^^^^^^^^^^^^
2020-10-12 21:57:51 +00:00
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_batch_start
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_validation_batch_start
^^^^^^^^^^^^^^^^^^^^^^^^^
2020-10-12 21:57:51 +00:00
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_validation_batch_start
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_validation_batch_end
^^^^^^^^^^^^^^^^^^^^^^^
2020-10-12 21:57:51 +00:00
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_validation_batch_end
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_test_batch_start
^^^^^^^^^^^^^^^^^^^
2020-10-12 21:57:51 +00:00
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_test_batch_start
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_test_batch_end
^^^^^^^^^^^^^^^^^
2020-10-12 21:57:51 +00:00
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_test_batch_end
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_batch_end
2020-10-12 21:57:51 +00:00
^^^^^^^^^^^^
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_batch_end
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_train_start
^^^^^^^^^^^^^^
2020-10-12 21:57:51 +00:00
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_train_start
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_train_end
^^^^^^^^^^^^
2020-10-12 21:57:51 +00:00
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_train_end
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_pretrain_routine_start
2020-10-12 21:57:51 +00:00
^^^^^^^^^^^^^^^^^^^^^^^^^
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_pretrain_routine_start
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_pretrain_routine_end
2020-10-12 21:57:51 +00:00
^^^^^^^^^^^^^^^^^^^^^^^
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_pretrain_routine_end
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_validation_start
^^^^^^^^^^^^^^^^^^^
2020-10-12 21:57:51 +00:00
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_validation_start
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_validation_end
^^^^^^^^^^^^^^^^^
2020-10-12 21:57:51 +00:00
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_validation_end
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_test_start
^^^^^^^^^^^^^
2020-10-12 21:57:51 +00:00
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_test_start
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_test_end
^^^^^^^^^^^
2020-10-12 21:57:51 +00:00
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_test_end
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_keyboard_interrupt
^^^^^^^^^^^^^^^^^^^^^
2020-10-12 21:57:51 +00:00
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_keyboard_interrupt
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_save_checkpoint
^^^^^^^^^^^^^^^^^^
.. automethod :: pytorch_lightning.callbacks.Callback.on_save_checkpoint
:noindex:
on_load_checkpoint
^^^^^^^^^^^^^^^^^^
2020-10-12 21:57:51 +00:00
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_load_checkpoint
2020-10-12 21:57:51 +00:00
:noindex: