Lightning AI Studios: Never set up a local environment again →

Log in or create a free Lightning.ai account to track your progress and access additional course materials  

5.8 Add Functionality with Callbacks

References

What we covered in this video lecture

In this lecture, we learned how we can extend the Trainer functionality using pre-built and custom callbacks. A callback is a function or program that is passed as an argument to another function or class (in this case, the Lightning Trainer) and is executed after the outer function has completed.

In other words, a callback is a function that is called at the completion of a given task. This allows other code to be run in the meantime and prevents any blocking or waiting for the task to complete.

The Lightning Trainer has a callback mechanism for executing functions as needed. Callbacks are intended to handle non-critical logic that is not necessary for the proper functioning of a Lightning module. These functions can be executed after the main module has completed its execution.

For example, as we have seen in the video, we can execute callback functions when the training starts or finishes. But there are many additional entry points (see the on_..._start and on_..._end methods in the documentation [here].

Additional resources if you want to learn more

Before building your own callbacks, it maybe a good idea to check the gallery of pre-built callbacks.

Log in or create a free Lightning.ai account to access:

  • Quizzes
  • Completion badges
  • Progress tracking
  • Additional downloadable content
  • Additional AI education resources
  • Notifications when new units are released
  • Free cloud computing credits

Quiz: 5.8 Adding Functionality with Callbacks

Which of the following statements is/are true about custom callbacks in the Lightning Trainer?

We can implement a callback that …

Correct. While other forms of notifications like email or text may be more convenient, it is of course easily possible using a Python phone call & voice API.

Correct. We actually have a pre-built callback for this if useful: https://lightning-bolts.readthedocs.io/en/latest/callbacks/vision.html#tensorboard-image-generator

Correct. We actually already have a pre-built callback for this if useful: https://pytorch-lightning.readthedocs.io/en/stable/api/pytorch_lightning.callbacks.DeviceStatsMonitor.html#pytorch_lightning.callbacks.DeviceStatsMonitor

Please answer all questions to proceed.
Watch Video 1

Unit 5.8

Videos
Follow along in a Lightning Studio

DL Fundamentals 5: PyTorch Lightning

Sebastian
Launch Studio →
Questions or Feedback?

Join the Discussion