RAR#

class capymoa.ocl.strategy.RAR[source]#

Bases: BatchClassifier, TrainTaskAware, TestTaskAware

Repeated Augmented Rehearsal.

Repeated Augmented Rehearsal (RAR) [1] is a replay continual learning strategy that combines data augmentation with repeated training on each batch to mitigate catastrophic forgetting.

  • Coreset Selection: Reservoir sampling is used to select a fixed-size buffer of past examples.

  • Coreset Retrieval: During training, the learner samples uniformly from the buffer of past examples.

  • Coreset Exploitation: The learner trains on the current batch of examples and the sampled buffer examples, performing multiple optimization steps per-batch using random augmentations of the examples.

  • Not TrainTaskAware or TestTaskAware, but will proxy it to the wrapped learner.

>>> from capymoa.ann import Perceptron
>>> from capymoa.classifier import Finetune
>>> from capymoa.ocl.strategy import RAR
>>> from capymoa.ocl.datasets import TinySplitMNIST
>>> from capymoa.ocl.evaluation import ocl_train_eval_loop
>>> import torch
>>> _ = torch.manual_seed(0)
>>> scenario = TinySplitMNIST()
>>> model = Perceptron(scenario.schema)
>>> learner = RAR(Finetune(scenario.schema, model), augment=nn.Dropout(p=0.2), repeats=2)
>>> results = ocl_train_eval_loop(
...     learner,
...     scenario.train_loaders(32),
...     scenario.test_loaders(32),
... )
>>> results.accuracy_final*100 > 41.5 # PyTorch is nondeterministic across versions
True

Usually more complex augmentations are used such as random crops and rotations.

__init__(
learner: BatchClassifier,
coreset_size: int = 200,
augment: Callable[[Tensor], Tensor] = nn.Identity(),
repeats: int = 1,
) None[source]#

Initialize Repeated Augmented Rehearsal.

Parameters:
  • learner – Underlying learner to be trained with RAR.

  • coreset_size – Size of the coreset buffer.

  • augment – Data augmentation function to apply to the samples. Should take a Tensor of shape (batch_size, *schema.shape) and return a Tensor of the same shape.

  • repeats – Number of times to repeat training on each batch, defaults to 1.

batch_predict(x: Tensor) Tensor[source]#

Predict the labels for a batch of instances.

Parameters:

x – Batch of x_dtype valued feature vectors (batch_size, num_features)

Returns:

Predicted batch of y_dtype valued labels (batch_size,).

batch_predict_proba(x: Tensor) Tensor[source]#

Predict the probabilities of the classes for a batch of instances.

Parameters:

x – Batch of x_dtype valued feature vectors (batch_size, num_features)

Returns:

Batch of x_dtype valued predicted probabilities (batch_size, num_classes).

batch_train(x: Tensor, y: Tensor) None[source]#

Train with a batch of instances.

Parameters:
  • x – Batch of x_dtype valued feature vectors (batch_size, num_features)

  • y – Batch of y_dtype valued labels (batch_size,).

on_test_task(task_id: int)[source]#

Called when testing on a task starts.

on_train_task(task_id: int)[source]#

Called when a new training task starts.

predict(instance: Instance) int | None[source]#

Predict the label of an instance.

The base implementation calls predict_proba() and returns the label with the highest probability.

Parameters:

instance – The instance to predict the label for.

Returns:

The predicted label or None if the classifier is unable to make a prediction.

predict_proba(
instance: Instance,
) ndarray[tuple[Any, ...], dtype[float64]] | None[source]#

Calls batch_predict_proba() with a batch of size 1.

train(instance: LabeledInstance) None[source]#

Calls batch_train() with a batch of size 1.

train_step(x_fresh: Tensor, y_fresh: Tensor) None[source]#
device: torch.device = device(type='cpu')#

Device on which the batch will be processed.

random_seed: int#

The random seed for reproducibility.

When implementing a classifier ensure random number generators are seeded.

schema: Schema#

The schema representing the instances.

x_dtype: torch.dtype = torch.float32#

Data type for the input features.

y_dtype: torch.dtype = torch.int64#

Data type for the target value/labels.