2020-01-21 20:18:32 +00:00
.. role :: hidden
:class: hidden-section
2020-03-20 19:49:01 +00:00
.. _callbacks:
2022-04-19 18:15:47 +00:00
########
2020-08-16 03:57:33 +00:00
Callback
2022-04-19 18:15:47 +00:00
########
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
2021-11-29 20:38:23 +00:00
Lightning has a callback system to execute them when needed. Callbacks should capture NON-ESSENTIAL
2021-01-26 20:07:07 +00:00
logic that is NOT required for your :doc: `lightning module <../common/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
2021-07-28 16:08:31 +00:00
class MyPrintingCallback(Callback):
2021-12-08 07:42:19 +00:00
def on_train_start(self, trainer, pl_module):
print("Training is starting")
2020-05-05 02:16:54 +00:00
def on_train_end(self, trainer, pl_module):
2021-12-08 07:42:19 +00:00
print("Training is ending")
2021-07-28 16:08:31 +00:00
2020-05-05 02:16:54 +00:00
trainer = Trainer(callbacks=[MyPrintingCallback()])
2020-04-05 09:38:52 +00:00
We successfully extended functionality without polluting our super clean
2021-01-26 20:07:07 +00:00
:doc: `lightning module <../common/lightning_module>` research code.
2020-02-27 22:21:51 +00:00
2020-08-13 13:58:05 +00:00
-----------
2022-04-19 18:15:47 +00:00
***** ***
2020-08-13 13:58:05 +00:00
Examples
2022-04-19 18:15:47 +00:00
***** ***
2020-08-13 13:58:05 +00:00
You can do pretty much anything with callbacks.
2022-01-18 15:47:48 +00:00
- `Add a MLP to fine-tune self-supervised networks <https://lightning-bolts.readthedocs.io/en/stable/deprecated/callbacks/self_supervised.html#sslonlineevaluator> `_ .
- `Find how to modify an image input to trick the classification result <https://lightning-bolts.readthedocs.io/en/stable/deprecated/callbacks/vision.html#confused-logit> `_ .
- `Interpolate the latent space of any variational model <https://lightning-bolts.readthedocs.io/en/stable/deprecated/callbacks/variational.html#latent-dim-interpolator> `_ .
- `Log images to Tensorboard for any model <https://lightning-bolts.readthedocs.io/en/stable/deprecated/callbacks/vision.html#tensorboard-image-generator> `_ .
2020-08-13 13:58:05 +00:00
--------------
2022-04-19 18:15:47 +00:00
***** ***** ***** ***
2020-08-13 13:58:05 +00:00
Built-in Callbacks
2022-04-19 18:15:47 +00:00
***** ***** ***** ***
2020-08-13 13:58:05 +00:00
Lightning has a few built-in callbacks.
.. note ::
For a richer collection of callbacks, check out our
2022-01-18 15:47:48 +00:00
`bolts library <https://lightning-bolts.readthedocs.io/en/stable/index.html> `_ .
2020-08-13 13:58:05 +00:00
2020-10-06 21:28:45 +00:00
.. currentmodule :: pytorch_lightning.callbacks
.. autosummary ::
:nosignatures:
:template: classtemplate.rst
2021-02-04 18:36:54 +00:00
BackboneFinetuning
BaseFinetuning
2021-09-03 00:14:36 +00:00
BasePredictionWriter
2020-10-06 21:28:45 +00:00
Callback
2021-10-13 18:29:36 +00:00
DeviceStatsMonitor
2020-10-06 21:28:45 +00:00
EarlyStopping
GradientAccumulationScheduler
2021-01-13 09:42:49 +00:00
LambdaCallback
2020-10-06 21:28:45 +00:00
LearningRateMonitor
ModelCheckpoint
2021-01-27 06:00:42 +00:00
ModelPruning
2021-09-17 10:54:16 +00:00
ModelSummary
2020-10-06 21:28:45 +00:00
ProgressBarBase
2021-11-29 20:38:23 +00:00
QuantizationAwareTraining
2021-09-17 10:54:16 +00:00
RichModelSummary
2021-09-03 00:14:36 +00:00
RichProgressBar
2021-02-11 00:05:59 +00:00
StochasticWeightAveraging
2021-11-29 20:38:23 +00:00
Timer
TQDMProgressBar
2020-08-02 02:56:34 +00:00
2020-08-13 13:58:05 +00:00
----------
2020-08-02 02:56:34 +00:00
2022-04-19 18:15:47 +00:00
.. include :: callbacks_state.rst
2020-08-28 14:50:52 +00:00
2022-04-19 18:15:47 +00:00
----------
2020-08-28 14:50:52 +00:00
2022-04-19 18:15:47 +00:00
***** ***** *** *
2020-08-02 02:56:34 +00:00
Best Practices
2022-04-19 18:15:47 +00:00
***** ***** *** *
2020-08-02 02:56:34 +00:00
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
-----------
2022-01-13 21:11:43 +00:00
.. _callback_hooks:
2020-10-12 21:57:51 +00:00
2022-04-19 18:15:47 +00:00
***** ***** **
2021-11-29 20:38:23 +00:00
Callback API
2022-04-19 18:15:47 +00:00
***** ***** **
2021-11-29 20:38:23 +00:00
Here is the full API of methods available in the Callback base class.
2020-10-12 21:57:51 +00:00
2021-11-29 20:38:23 +00:00
The :class: `~pytorch_lightning.callbacks.Callback` class is the base for all the callbacks in Lightning just like the :class: `~pytorch_lightning.core.lightning.LightningModule` is the base for all models.
It defines a public interface that each callback implementation must follow, the key ones are:
Properties
2022-04-19 18:15:47 +00:00
==========
2021-11-29 20:38:23 +00:00
state_key
2022-04-19 18:15:47 +00:00
^^^^^^^^^
2021-11-29 20:38:23 +00:00
.. autoattribute :: pytorch_lightning.callbacks.Callback.state_key
:noindex:
Hooks
2022-04-19 18:15:47 +00:00
=====
2020-10-20 15:01:08 +00:00
2021-11-29 20:38:23 +00:00
on_configure_sharded_model
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^^^^^^^^^
2021-11-29 20:38:23 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_configure_sharded_model
:noindex:
setup
2022-04-19 18:15:47 +00:00
^^^^^
2021-11-29 20:38:23 +00:00
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.setup
:noindex:
teardown
2022-04-19 18:15:47 +00:00
^^^^^^^^
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.teardown
:noindex:
on_init_start
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^
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
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^
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
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^
2020-10-12 21:57:51 +00:00
2021-04-13 23:19:27 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_fit_start
2020-10-12 21:57:51 +00:00
:noindex:
on_fit_end
2022-04-19 18:15:47 +00:00
^^^^^^^^^^
2020-10-12 21:57:51 +00:00
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
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^^^^
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_sanity_check_start
:noindex:
on_sanity_check_end
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^^
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_sanity_check_end
:noindex:
on_train_batch_start
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^^^
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_train_batch_start
:noindex:
on_train_batch_end
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^
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
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^^^
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_train_epoch_start
:noindex:
on_train_epoch_end
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^
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
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^^^^^^^^
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
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^^^^^^
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
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^^
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
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^
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:
2021-11-29 20:38:23 +00:00
on_predict_epoch_start
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^^^^^
2021-11-29 20:38:23 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_predict_epoch_start
:noindex:
on_predict_epoch_end
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^^^
2021-11-29 20:38:23 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_predict_epoch_end
:noindex:
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_validation_batch_start
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^^^^^^^^
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
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^^^^^^
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
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^^
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
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^
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:
2021-11-29 20:38:23 +00:00
on_predict_batch_start
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^^^^^
2020-10-12 21:57:51 +00:00
2021-11-29 20:38:23 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_predict_batch_start
:noindex:
on_predict_batch_end
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^^^
2021-11-29 20:38:23 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_predict_batch_end
2020-10-12 21:57:51 +00:00
:noindex:
2020-10-20 15:01:08 +00:00
on_train_start
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^
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
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^
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_validation_start
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^^
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
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^
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
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^
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
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^
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:
2021-11-29 20:38:23 +00:00
on_predict_start
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^
2021-11-29 20:38:23 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_predict_start
:noindex:
on_predict_end
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^
2021-11-29 20:38:23 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_predict_end
:noindex:
2020-10-20 15:01:08 +00:00
on_keyboard_interrupt
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^^^^
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:
2021-09-11 23:25:42 +00:00
on_exception
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^
2021-09-11 23:25:42 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_exception
:noindex:
2022-03-25 00:20:21 +00:00
state_dict
2022-04-19 18:15:47 +00:00
^^^^^^^^^^
2022-03-25 00:20:21 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.state_dict
:noindex:
2020-10-20 15:01:08 +00:00
on_save_checkpoint
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^
2020-10-20 15:01:08 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_save_checkpoint
:noindex:
2022-03-25 00:20:21 +00:00
load_state_dict
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^
2022-03-25 00:20:21 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.load_state_dict
:noindex:
2020-10-20 15:01:08 +00:00
on_load_checkpoint
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^
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:
2021-03-25 13:20:49 +00:00
2021-07-09 06:15:57 +00:00
on_before_backward
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^
2021-07-09 06:15:57 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_before_backward
:noindex:
2021-03-25 13:20:49 +00:00
on_after_backward
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^
2021-03-25 13:20:49 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_after_backward
:noindex:
2021-07-09 11:30:52 +00:00
on_before_optimizer_step
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^^^^^^^
2021-07-09 11:30:52 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_before_optimizer_step
:noindex:
2021-03-25 13:20:49 +00:00
on_before_zero_grad
2022-04-19 18:15:47 +00:00
^^^^^^^^^^^^^^^^^^^
2021-03-25 13:20:49 +00:00
.. automethod :: pytorch_lightning.callbacks.Callback.on_before_zero_grad
:noindex: