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-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-04-05 09:38:52 +00:00
logic that is NOT required for your :class: `~pytorch_lightning.core.LightningModule` 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-08-16 02:36:53 +00:00
<video width="100%" controls autoplay 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 ::
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
:class: `~pytorch_lightning.core.LightningModule` 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> `_ .
--------------
Callback Hooks
2020-07-01 11:38:00 +00:00
--------------
2020-06-29 01:36:46 +00:00
2020-03-03 16:39:43 +00:00
.. automodule :: pytorch_lightning.callbacks.base
:noindex:
:exclude-members:
_del_model,
_save_model,
_abc_impl,
check_monitor_top_k,
2020-06-18 21:54:29 +00:00
----------------
2020-03-03 21:42:49 +00:00
2020-08-13 13:58:05 +00:00
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-03-03 16:39:43 +00:00
.. automodule :: pytorch_lightning.callbacks.early_stopping
:noindex:
:exclude-members:
_del_model,
_save_model,
_abc_impl,
check_monitor_top_k,
2020-06-18 21:54:29 +00:00
----------------
2020-03-03 21:42:49 +00:00
2020-08-13 22:58:14 +00:00
.. automodule :: pytorch_lightning.callbacks.gpu_usage_logger
:noindex:
:exclude-members:
_get_gpu_stat,
_log_gpu,
_log_memory
----------------
2020-06-17 17:42:28 +00:00
.. automodule :: pytorch_lightning.callbacks.gradient_accumulation_scheduler
2020-03-03 16:39:43 +00:00
:noindex:
:exclude-members:
_del_model,
_save_model,
_abc_impl,
check_monitor_top_k,
2020-02-27 22:21:51 +00:00
2020-06-18 21:54:29 +00:00
----------------
2020-03-03 21:42:49 +00:00
2020-06-17 17:42:28 +00:00
.. automodule :: pytorch_lightning.callbacks.lr_logger
:noindex:
:exclude-members:
_extract_lr,
_find_names
2020-06-18 21:54:29 +00:00
----------------
2020-06-17 17:42:28 +00:00
.. automodule :: pytorch_lightning.callbacks.model_checkpoint
2020-02-27 21:07:51 +00:00
:noindex:
2020-01-21 20:18:32 +00:00
:exclude-members:
_del_model,
_save_model,
2020-02-27 22:21:51 +00:00
_abc_impl,
2020-01-21 20:18:32 +00:00
check_monitor_top_k,
2020-04-24 00:46:18 +00:00
2020-06-18 21:54:29 +00:00
----------------
2020-04-24 00:46:18 +00:00
.. automodule :: pytorch_lightning.callbacks.progress
:noindex:
:exclude-members:
2020-08-02 02:56:34 +00:00
2020-08-13 13:58:05 +00:00
----------
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.