RAR#
- class capymoa.ocl.strategy.RAR[source]#
Bases:
BatchClassifier,HandlerRepeated 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. The original paper uses RandAugment [2] for augmentation but any randomized augmentation can be used. But the choice of augmentation is important and should be chosen based on the problem domain.
>>> from capymoa.core.torch.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 torchvision.transforms as T >>> import torch >>> _ = torch.manual_seed(0) >>> scenario = TinySplitMNIST() >>> model = Perceptron(scenario.schema) >>> # You should use more complex augmentations for more challenging problems. >>> augment = T.Compose([ ... T.RandomRotation(10), ... ]) >>> learner = RAR(Finetune(scenario.schema, model), augment=augment, repeats=5) >>> results = ocl_train_eval_loop( ... learner, ... scenario.train_loaders(32), ... scenario.test_loaders(32), ... ) >>> print(f"{results.accuracy_final*100:.1f}%") 39.5%
Usually more complex augmentations are used such as random crops and rotations.
- __init__(
- learner: BatchClassifier,
- augment: Callable[[Tensor], Tensor],
- coreset_size: int = 200,
- repeats: int = 1,
Initialize Repeated Augmented Rehearsal.
- Parameters:
learner – Underlying learner to be trained with RAR.
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. Take a look at the PyTorch torchvision transforms for some building blocks for your pipeline (https://docs.pytorch.org/vision/main/transforms.html).coreset_size – Size of the coreset buffer.
repeats – Number of times to repeat training on each batch, defaults to 1.
- attach_with(
- source: Dispatcher,
Attach this sink to an event source.
Implementations should call
capymoa.ocl.events.Dispatcher.subscribe()for each event type the sink needs to handle.- Parameters:
dispatcher – The source this sink should subscribe to.
- batch_predict_proba(x: Tensor) Tensor[source]#
Predict the probabilities of the classes for a batch of instances.
- classmethod from_params( ) Any[source]#
Construct an instance from parameters produced by
get_params.
- 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.