clarena.cul_algorithms

Continual Unlearning Algorithms

This submodule provides the continual unlearning algorithms in CLArena.

Here are the base classes for CUL algorithms:

  • CULAlgorithm: the base class for all continual unlearning algorithms.

Please note that this is an API documantation. Please refer to the main documentation pages for more information about how to configure and implement CUL algorithms:

 1r"""
 2
 3# Continual Unlearning Algorithms
 4
 5This submodule provides the **continual unlearning algorithms** in CLArena.
 6
 7Here are the base classes for CUL algorithms:
 8
 9- `CULAlgorithm`: the base class for all continual unlearning algorithms.
10
11Please note that this is an API documantation. Please refer to the main documentation pages for more information about how to configure and implement CUL algorithms:
12
13- [**Configure CUL Algorithm**](https://pengxiang-wang.com/projects/continual-learning-arena/docs/components/cul-algorithm)
14- [**Implement Custom CUL Algorithm**](https://pengxiang-wang.com/projects/continual-learning-arena/docs/custom-implementation/cul-algorithm)
15
16
17
18"""
19
20from .base import CULAlgorithm
21from .independent_unlearn import IndependentUnlearn
22from .amnesiac_hat_unlearn import AmnesiacHATUnlearn
23
24
25__all__ = ["CULAlgorithm", "independent_unlearn", "amnesiac_hat_unlearn"]
class CULAlgorithm:
17class CULAlgorithm:
18    r"""The base class of continual unlearning algorithms."""
19
20    def __init__(self, model: UnlearnableCLAlgorithm) -> None:
21        r"""
22        **Args:**
23        - **model** (`UnlearnableCLAlgorithm`): the continual learning model.
24        """
25
26        # components
27        self.model: UnlearnableCLAlgorithm = model
28        r"""The continual learning model."""
29
30        # task ID control
31        self.task_id: int
32        r"""Task ID counter indicating which task is being processed. Self updated during the task loop. Valid from 1 to `cl_dataset.num_tasks`."""
33        self.processed_task_ids: list[int] = []
34        r"""Task IDs that have been processed."""
35        self.unlearning_task_ids: list[int] = []
36        r"""The list of task IDs that are requested to be unlearned after training `self.task_id`."""
37        self.unlearned_task_ids: set[int] = set()
38        r"""The list of task IDs that have been unlearned in the experiment. """
39        self.if_permanent_t: bool
40        r"""Whether the task is permanent or not. If `True`, the task will not be unlearned i.e. not shown in future unlearning requests."""
41
42    def setup_task_id(
43        self,
44        task_id: int,
45        unlearning_requests: dict[int, list[int]],
46        if_permanent: bool,
47    ) -> None:
48        r"""Set up which task the CUL experiment is on. This must be done before `unlearn()` method is called.
49
50        **Args:**
51        - **task_id** (`int`): the target task ID to be set up.
52        - **unlearning_requests** (`dict[int, list[int]]`): the entire unlearning requests. Keys are IDs of the tasks that request unlearning after their learning, and values are the list of the previous tasks to be unlearned.
53        - **if_permanent** (`bool`): whether the task is permanent or not. If `True`, the task will not be unlearned i.e. not shown in future unlearning requests.
54        """
55        self.task_id = task_id
56
57        unlearning_task_ids = (
58            unlearning_requests[task_id] if task_id in unlearning_requests else []
59        )
60        self.unlearning_task_ids = unlearning_task_ids
61        self.model.unlearning_task_ids = unlearning_task_ids
62
63        self.if_permanent_t = if_permanent
64
65    def setup_test_task_id(self) -> None:
66        r"""Set up before testing `self.task_id`. This must be done after `unlearn()` method is called."""
67
68        self.unlearned_task_ids.update(
69            self.unlearning_task_ids
70        )  # update the maintained set of unlearned task IDs
71        self.model.unlearned_task_ids = (
72            self.unlearned_task_ids
73        )  # let model know the unlearned task IDs
74
75    @abstractmethod
76    def unlearn(self) -> None:
77        r"""Unlearn the requested unlearning tasks after training `self.task_id`. **It must be implemented in subclasses.**"""

The base class of continual unlearning algorithms.

CULAlgorithm(model: clarena.cl_algorithms.UnlearnableCLAlgorithm)
20    def __init__(self, model: UnlearnableCLAlgorithm) -> None:
21        r"""
22        **Args:**
23        - **model** (`UnlearnableCLAlgorithm`): the continual learning model.
24        """
25
26        # components
27        self.model: UnlearnableCLAlgorithm = model
28        r"""The continual learning model."""
29
30        # task ID control
31        self.task_id: int
32        r"""Task ID counter indicating which task is being processed. Self updated during the task loop. Valid from 1 to `cl_dataset.num_tasks`."""
33        self.processed_task_ids: list[int] = []
34        r"""Task IDs that have been processed."""
35        self.unlearning_task_ids: list[int] = []
36        r"""The list of task IDs that are requested to be unlearned after training `self.task_id`."""
37        self.unlearned_task_ids: set[int] = set()
38        r"""The list of task IDs that have been unlearned in the experiment. """
39        self.if_permanent_t: bool
40        r"""Whether the task is permanent or not. If `True`, the task will not be unlearned i.e. not shown in future unlearning requests."""

Args:

  • model (UnlearnableCLAlgorithm): the continual learning model.

The continual learning model.

task_id: int

Task ID counter indicating which task is being processed. Self updated during the task loop. Valid from 1 to cl_dataset.num_tasks.

processed_task_ids: list[int]

Task IDs that have been processed.

unlearning_task_ids: list[int]

The list of task IDs that are requested to be unlearned after training self.task_id.

unlearned_task_ids: set[int]

The list of task IDs that have been unlearned in the experiment.

if_permanent_t: bool

Whether the task is permanent or not. If True, the task will not be unlearned i.e. not shown in future unlearning requests.

def setup_task_id( self, task_id: int, unlearning_requests: dict[int, list[int]], if_permanent: bool) -> None:
42    def setup_task_id(
43        self,
44        task_id: int,
45        unlearning_requests: dict[int, list[int]],
46        if_permanent: bool,
47    ) -> None:
48        r"""Set up which task the CUL experiment is on. This must be done before `unlearn()` method is called.
49
50        **Args:**
51        - **task_id** (`int`): the target task ID to be set up.
52        - **unlearning_requests** (`dict[int, list[int]]`): the entire unlearning requests. Keys are IDs of the tasks that request unlearning after their learning, and values are the list of the previous tasks to be unlearned.
53        - **if_permanent** (`bool`): whether the task is permanent or not. If `True`, the task will not be unlearned i.e. not shown in future unlearning requests.
54        """
55        self.task_id = task_id
56
57        unlearning_task_ids = (
58            unlearning_requests[task_id] if task_id in unlearning_requests else []
59        )
60        self.unlearning_task_ids = unlearning_task_ids
61        self.model.unlearning_task_ids = unlearning_task_ids
62
63        self.if_permanent_t = if_permanent

Set up which task the CUL experiment is on. This must be done before unlearn() method is called.

Args:

  • task_id (int): the target task ID to be set up.
  • unlearning_requests (dict[int, list[int]]): the entire unlearning requests. Keys are IDs of the tasks that request unlearning after their learning, and values are the list of the previous tasks to be unlearned.
  • if_permanent (bool): whether the task is permanent or not. If True, the task will not be unlearned i.e. not shown in future unlearning requests.
def setup_test_task_id(self) -> None:
65    def setup_test_task_id(self) -> None:
66        r"""Set up before testing `self.task_id`. This must be done after `unlearn()` method is called."""
67
68        self.unlearned_task_ids.update(
69            self.unlearning_task_ids
70        )  # update the maintained set of unlearned task IDs
71        self.model.unlearned_task_ids = (
72            self.unlearned_task_ids
73        )  # let model know the unlearned task IDs

Set up before testing self.task_id. This must be done after unlearn() method is called.

@abstractmethod
def unlearn(self) -> None:
75    @abstractmethod
76    def unlearn(self) -> None:
77        r"""Unlearn the requested unlearning tasks after training `self.task_id`. **It must be implemented in subclasses.**"""

Unlearn the requested unlearning tasks after training self.task_id. It must be implemented in subclasses.