RAR#
- class capymoa.ocl.strategy.RAR[source]#
Bases:
BatchClassifier,TrainTaskAware,TestTaskAwareRepeated 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
TrainTaskAwareorTestTaskAware, 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,
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_proba(x: Tensor) Tensor[source]#
Predict the probabilities of the classes for a batch of instances.
- 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
Noneif the classifier is unable to make a prediction.
- predict_proba(
- instance: Instance,
Calls
batch_predict_proba()with a batch of size 1.
- train(instance: LabeledInstance) None[source]#
Calls
batch_train()with a batch of size 1.
- 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.
- 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.