# `MAS`

### *class* capymoa.ocl.strategy.MAS[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/ocl/strategy/_mas.py#L90)

Bases: [`BatchClassifier`](capymoa.base.BatchClassifier.md#capymoa.base.BatchClassifier), [`Module`](https://docs.pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module), [`Handler`](capymoa.ocl.events.Handler.md#capymoa.ocl.events.Handler)

Memory Aware Synapses learner.

Memory Aware Synapses (MAS) is a regularisation-based continual learning strategy
that estimates per-parameter importance from the sensitivity of the model’s output
to small parameter perturbations, then penalises changes to parameters that were
important for previous tasks <sup>[1](#f1)</sup>.

Unlike EWC and SI, MAS estimates importance from the squared L2 norm of the model’s
output rather than the task loss, so importance can be estimated without labels.
We use a replay buffer to approximate the active task distribution when estimating
importance.

Alternative implementations:

* [Original](https://github.com/rahafaljundi/MAS-Memory-Aware-Synapses)
* [FACIL](https://github.com/mmasana/FACIL/blob/e09d2c83320a1aa945a6157d4875437515824dc9/src/approach/mas.py)
* [Avalanche Lib](https://github.com/ContinualAI/avalanche/blob/eb075be393e1f458b2c352514ff6c17b5a2c0f4e/avalanche/training/plugins/mas.py)

* <a id='f1'>**[1]**</a> Aljundi, R., Babiloni, F., Elhoseiny, M., Rohrbach, M., & Tuytelaars, T. (2018). Memory Aware Synapses: Learning What (Not) to Forget. In V. Ferrari, M. Hebert, C. Sminchisescu, & Y. Weiss (Eds.), Computer Vision – ECCV 2018 (pp. 144-161). Springer International Publishing. [https://doi.org/10.1007/978-3-030-01219-9_9](https://doi.org/10.1007/978-3-030-01219-9_9)

#### \_\_init_\_(schema: [Schema](capymoa.stream.Schema.md#capymoa.stream.Schema), model: [Module](https://docs.pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module), optimiser: [Optimizer](https://docs.pytorch.org/docs/stable/optim.html#torch.optim.Optimizer), lambda_: [float](https://docs.python.org/3/builtins/functions.html#float), alpha: [float](https://docs.python.org/3/builtins/functions.html#float) = 0.5, buffer_capacity: [int](https://docs.python.org/3/builtins/functions.html#int) = 256, importance_batch_size: [int](https://docs.python.org/3/builtins/functions.html#int) = 32, device: [device](https://docs.pytorch.org/docs/stable/tensor_attributes.html#torch.device) | [None](https://docs.python.org/3/builtins/constants.html#None) = None, mask_test: [bool](https://docs.python.org/3/builtins/functions.html#bool) = False, mask_train: [bool](https://docs.python.org/3/builtins/functions.html#bool) = False, task_mask: [Tensor](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor) | [None](https://docs.python.org/3/builtins/constants.html#None) = None) → [None](https://docs.python.org/3/builtins/constants.html#None)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/ocl/strategy/_mas.py#L116)

Construct a MAS learner.

* **Parameters:**
  * **schema** – Stream schema used by the classifier interface.
  * **model** – Torch model that outputs class logits.
  * **optimiser** – Optimiser used to update `model` parameters.
  * **lambda** – Weight of the MAS regularisation term.
  * **alpha** – EMA factor weighting the *old* importance estimate (`alpha=1.0`
    means the importance never updates past its initial zero value;
    `alpha=0.0` keeps only the most recent estimate). RWalk’s `alpha`
    weights the new estimate instead.
  * **buffer_capacity** – Replay window size used to estimate importance.
  * **importance_batch_size** – Mini-batch size used when estimating importance.
  * **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**](https://docs.python.org/3/builtins/exceptions.html#ValueError) – If `lambda_` is negative, `alpha` is outside `[0, 1]`,
  or task-specific masking is requested without `task_mask`.

#### attach_with(source: [Dispatcher](capymoa.ocl.events.Dispatcher.md#capymoa.ocl.events.Dispatcher)) → [MAS](#capymoa.ocl.strategy.MAS)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/ocl/strategy/_mas.py#L214)

Attach this sink to an event source.

Implementations should call
[`capymoa.ocl.events.Dispatcher.subscribe()`](capymoa.ocl.events.Dispatcher.md#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(x: [Tensor](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor)) → [Tensor](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_batch_classifier.py#L115)

Predict the labels for a batch of instances.

* **Parameters:**
  **x** – Batch of [`x_dtype`](#capymoa.ocl.strategy.MAS.x_dtype) valued feature vectors
  `(batch_size, num_features)`
* **Returns:**
  Predicted batch of [`y_dtype`](#capymoa.ocl.strategy.MAS.y_dtype) valued labels
  `(batch_size,)`.

#### batch_predict_proba(x: [Tensor](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor)) → [Tensor](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/ocl/strategy/_mas.py#L208)

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

* **Parameters:**
  **x** – Batch of [`x_dtype`](#capymoa.ocl.strategy.MAS.x_dtype) valued feature vectors
  `(batch_size, num_features)`
* **Returns:**
  Batch of [`x_dtype`](#capymoa.ocl.strategy.MAS.x_dtype) valued predicted probabilities
  `(batch_size, num_classes)`.

#### batch_train(x: [Tensor](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor), y: [Tensor](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor)) → [None](https://docs.python.org/3/builtins/constants.html#None)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/ocl/strategy/_mas.py#L198)

Train with a batch of instances.

* **Parameters:**
  * **x** – Batch of [`x_dtype`](#capymoa.ocl.strategy.MAS.x_dtype) valued feature vectors
    `(batch_size, num_features)`
  * **y** – Batch of [`y_dtype`](#capymoa.ocl.strategy.MAS.y_dtype) valued labels `(batch_size,)`.

#### *classmethod* from_params(schema: [Any](https://docs.python.org/3/library/typing.html#typing.Any) = None, params: [dict](https://docs.python.org/3/builtins/stdtypes.html#dict)[[str](https://docs.python.org/3/builtins/stdtypes.html#str), [Any](https://docs.python.org/3/library/typing.html#typing.Any)] | [None](https://docs.python.org/3/builtins/constants.html#None) = None, random_seed: [int](https://docs.python.org/3/builtins/functions.html#int) = 1) → [Any](https://docs.python.org/3/library/typing.html#typing.Any)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_learner_params.py#L170)

Construct an instance from parameters produced by `get_params`.

#### get_params() → [dict](https://docs.python.org/3/builtins/stdtypes.html#dict)[[str](https://docs.python.org/3/builtins/stdtypes.html#str), [Any](https://docs.python.org/3/library/typing.html#typing.Any)][[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_learner_params.py#L163)

Return the hyper-parameters captured from the constructor.

#### predict(instance: [Instance](capymoa.core.Instance.md#capymoa.core.Instance)) → [int](https://docs.python.org/3/builtins/functions.html#int) | [None](https://docs.python.org/3/builtins/constants.html#None)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_classifier.py#L56)

Predict the label of an instance.

The base implementation calls [`predict_proba()`](#capymoa.ocl.strategy.MAS.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](capymoa.core.Instance.md#capymoa.core.Instance)) → [ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)[[tuple](https://docs.python.org/3/builtins/stdtypes.html#tuple)[[Any](https://docs.python.org/3/library/typing.html#typing.Any), ...], [dtype](https://numpy.org/doc/stable/reference/generated/numpy.dtype.html#numpy.dtype)[float64]] | [None](https://docs.python.org/3/builtins/constants.html#None)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_batch_classifier.py#L134)

Calls [`batch_predict_proba()`](#capymoa.ocl.strategy.MAS.batch_predict_proba) with a batch of size 1.

#### train(instance: [LabeledInstance](capymoa.core.LabeledInstance.md#capymoa.core.LabeledInstance)) → [None](https://docs.python.org/3/builtins/constants.html#None)[[source]](https://github.com/adaptive-machine-learning/CapyMOA/blob/3e255b1/src/capymoa/base/_batch_classifier.py#L125)

Calls [`batch_train()`](#capymoa.ocl.strategy.MAS.batch_train) with a batch of size 1.

#### T_destination *= ~T_destination*

#### call_super_init *: [bool](https://docs.python.org/3/builtins/functions.html#bool)* *= False*

#### device *: [torch.device](https://docs.pytorch.org/docs/stable/tensor_attributes.html#torch.device)* *= device(type='cpu')*

Device on which the batch will be processed.

#### dump_patches *: [bool](https://docs.python.org/3/builtins/functions.html#bool)* *= False*

#### random_seed *: [int](https://docs.python.org/3/builtins/functions.html#int)*

The random seed for reproducibility.

When implementing a classifier ensure random number generators are seeded.

#### schema *: [Schema](capymoa.stream.Schema.md#capymoa.stream.Schema)*

The schema representing the instances.

#### training *: [bool](https://docs.python.org/3/builtins/functions.html#bool)*

#### x_dtype *: [torch.dtype](https://docs.pytorch.org/docs/stable/tensor_attributes.html#torch.dtype)* *= torch.float32*

Data type for the input features.

#### y_dtype *: [torch.dtype](https://docs.pytorch.org/docs/stable/tensor_attributes.html#torch.dtype)* *= torch.int64*

Data type for the target value/labels.
