EWC#
- class capymoa.ocl.strategy.EWC[source]#
Bases:
BatchClassifier,Module,TrainTaskAware,TestTaskAwareElastic Weight Consolidation learner.
Elastic Weight Consolidation (EWC) is a regularisation-based continual learning strategy that mitigates catastrophic forgetting by penalising changes to important parameters for previous tasks [1]. We incorporate Online EWC-style [2] updates to the Fisher diagonals, which decay the importance of previous tasks’ parameters over time based on the
gammahyperparameter.Usually the EWC strategy has access to the entire active task’s data when estimating the Fisher diagonals, but instead we use a replay buffer to approximate the active task distribution.
- __init__(
- schema: Schema,
- model: Module,
- optimiser: Optimizer,
- lambda_: float,
- fim_buffer: int = 256,
- fim_batch_size: int = 32,
- device: device = torch.device('cpu'),
- mask_test: bool = False,
- mask_train: bool = False,
- gamma: float = 1.0,
- task_mask: Tensor | None = None,
Construct an EWC learner.
- Parameters:
schema – Stream schema used by the classifier interface.
model – Torch model that outputs class logits.
optimiser – Optimiser used to update
modelparameters.lambda – Weight of the EWC regularisation term.
fim_buffer – Replay window size for Fisher estimation.
fim_batch_size – Mini-batch size used when estimating Fisher diagonals.
device – Compute device.
mask_test – Whether to apply per-task masking during testing. This is a task incremental scenario.
mask_train – Whether to apply per-task masking during training. This is also known as the labels trick.
task_mask – Optional per-task mask applied to output logits.
- Raises:
ValueError – If task-specific masking is requested without
task_mask.
- 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.
- T_destination = ~T_destination#
- 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.