Shawn’s Blog
  • 🗂️ Collections
    • 🖥️ Slides Gallery
    • 🧑‍🍳️ Cooking Ideas
    • 🍱 Cookbook
    • 💬 Language Learning
    • 🎼 Songbook
  • ⚙️ Projects
    • ⚛ Continual Learning Arena
  • 📄 Papers
    • AdaHAT
    • FG-AdaHAT
  • 🎓 CV
    • CV (English)
    • CV (Mandarin)
  • About
  1. Custom Implementation
  2. Callback
  • Welcome to CLArena
  • Getting Started
  • Configure Pipelines
  • Continual Learning (CL)
    • CL Main Experiment
    • Save and Evaluate Model
    • Full Experiment
    • Output Results
  • Continual Unlearning (CUL)
    • CUL Main Experiment
    • Full Experiment
    • Output Results
  • Multi-Task Learning (MTL)
    • MTL Experiment
    • Save and Evaluate Model
    • Output Results
  • Single-Task Learning (STL)
    • STL Experiment
    • Save and Evaluate Model
    • Output Results
  • Components
    • CL Dataset
    • MTL Dataset
    • STL Dataset
    • CL Algorithm
    • CUL Algorithm
    • MTL Algorithm
    • STL Algorithm
    • Backbone Network
    • Optimizer
    • Learning Rate Scheduler
    • Trainer
    • Metrics
    • Lightning Loggers
    • Callbacks
    • Other Configs
  • Custom Implementation
    • CL Dataset
    • MTL Dataset
    • STL Dataset
    • CL Algorithm
    • CUL Algorithm
    • MTL Algorithm
    • STL Algorithm
    • Backbone Network
    • Callback
  • API Reference

On this page

  • Base Classes
  • Implement Callback
  1. Custom Implementation
  2. Callback

Implement Custom Callback

Modified

September 5, 2025

This section guides you through implementing custom callbacks (including metric callbacks) for use in CLArena.

Callback is a feature provided by PyTorch Lightning. Callbacks are additional operations that can be applied at various stages of the training and evaluation process, such as at the beginning or end of an epoch, before or after a training step, etc. Procedures that are not directly related to the algorithms, such as logging, model checkpointing, early stopping, etc., can be implemented as callbacks, and applied separately. This allows for a cleaner separation of concerns and makes the code more modular and easier to maintain.

Base Classes

In CLArena, callbacks are implemented as subclasses of the Lightning callback class:

  • lightning.Callback: the base class for all callbacks.
    • clarena.metrics.MetricCallback: the base class for all metric callbacks.

Implement Callback

Callbacks are exactly Lightning Callback object, which can customize actions before, during, or after training, validating, or testing process. You can do this by overriding the hooks such as on_train_start(), on_train_batch_end(), on_test_start(). Please refer to the Lightning callback documentation for details about the hooks. Please note for continual learning, the hooks are called in each task.

These hooks take 2 arguments: trainer and pl_module, which are the PyTorch Lightning trainer and the CLAlgorithm module in your pipeline respectively, where you can get the information like current task ID and so on.


For more details, please refer to the API Reference and source code. You may take implemented callbacks in CLArena as examples. Feel free to contribute by submitting pull requests in GitHub!

API Reference (Callbacks) Source Code (Callbacks)

API Reference (Metrics) Source Code (Metrics)

GitHub Pull Request

Back to top
Backbone Network
API Reference
 
 

©️ 2025 Pengxiang Wang. All rights reserved.