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. CUL Algorithm
  • 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
  • FAQs

On this page

  • Base Classes
  • Implement CL Algorithm
    • Implement Your Regularizers
  1. Custom Implementation
  2. CUL Algorithm

Implement Custom CL Algorithm

Modified

September 6, 2025

This section guides you through implementing custom continual learning (CL) algorithms for use in CLArena.

Continual learning algorithms define the process of learning a sequence of tasks.

Base Classes

In CLArena, continual learning algorithms are implemented as subclasses of the base classes in clarena/cl_algorithms/base.py. The base classes are implemented inheriting Lightning module with additional features for continual learning:

  • clarena.cul_algorithms.CULAlgorithm: the base class for all continual learning algorithms.

Implement CL Algorithm

To implement CL algorithms:

  • Inherit CLAlgorithm.
  • Put your algorithm’s hyperparameters in save_hyperparameters() in __init__(). This enables Lightning loggers to manage them automatically.
  • Implement training_step(), validation_step(), test_step() to define the training, validation and test steps respectively. Note that these steps are for the current task indicated by self.task_id, which is updated automatically at the beginning of each task.
  • Other hooks like on_train_start(), on_validation_start(), on_test_start() are also free to customize if needed.
Tip

CLAlgorithm works the same as Lightning module. All the other hooks are free to customize. Please refer to the Lightning module documentation for details about the hooks.

Note that configure_optimizers() is already implemented in CLAlgorithm to manage the optimizer and learning rate scheduler for each task automatically. You don’t have to implement it unless you want to override the default behaviour.

Implement Your Regularizers

Some CL algorithms require regularizers to manage the interactions between tasks. You can implement your regularizers if the CL algorithms need them. A regularizer is a torch.nn.Module whose forward() method define the calculation of the loss. The regularizer is called in the training step to calculate the regularization loss, which is added to the classification loss to form the total loss.

Tip

We recommend including the regularization factor inside the regularizer. Below is the archetype of a regularizer:

from torch import nn

class Reg(nn.Module):

    def __init__(self, factor: float, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.factor = factor

    def forward(self, *args, **kwargs):
        reg = ...
        return reg * self.factor


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

API Reference (CL Algorithms) Source Code (CL Algorithms) GitHub Pull Request

Back to top
CL Algorithm
MTL Algorithm
 
 

©️ 2025 Pengxiang Wang. All rights reserved.