avalanche.training

The training module provides a generic continual learning training class (BaseStrategy) and implementations of the most common CL strategies. These are provided either as standalone strategies in training.strategies or as plugins (training.plugins) that can be easily combined with your own strategy.

Subpackages

Submodules

Package Contents

Classes

AR1

The AR1 strategy with Latent Replay.

Cumulative

Cumulative strategy. At each experience,

BaseStrategy

BaseStrategy is the super class of all task-based continual learning

JointTraining

JointTraining performs joint training (also called offline training) on

Naive

The simplest (and least effective) Continual Learning strategy. Naive just

CWRStar

CWR* Strategy.

Replay

Experience replay strategy. See ReplayPlugin for more details.

GDumb

GDumb strategy. See GDumbPlugin for more details.

LwF

Learning without Forgetting strategy.

AGEM

Average Gradient Episodic Memory (A-GEM) strategy.

GEM

Gradient Episodic Memory (GEM) strategy.

EWC

Elastic Weight Consolidation (EWC) strategy.

SynapticIntelligence

The Synaptic Intelligence strategy.

GSS_greedy

Experience replay strategy. See ReplayPlugin for more details.

CoPE

Continual Prototype Evolution strategy.

LFL

Less Forgetful Learning strategy.

StreamingLDA

Init function for the SLDA model.

class avalanche.training.AR1(criterion=None, lr: float = 0.001, momentum=0.9, l2=0.0005, train_epochs: int = 4, init_update_rate: float = 0.01, inc_update_rate=5e-05, max_r_max=1.25, max_d_max=0.5, inc_step=4.1e-05, rm_sz: int = 1500, freeze_below_layer: str = 'lat_features.19.bn.beta', latent_layer_num: int = 19, ewc_lambda: float = 0, train_mb_size: int = 128, eval_mb_size: int = 128, device=None, plugins: Optional[Sequence[StrategyPlugin]] = None, evaluator: EvaluationPlugin = default_logger, eval_every=- 1)[source]

Bases: avalanche.training.strategies.BaseStrategy

The AR1 strategy with Latent Replay.

This implementations allows for the use of both Synaptic Intelligence and Latent Replay to protect the lower level of the model from forgetting.

While the original papers show how to use those two techniques in a mutual exclusive way, this implementation allows for the use of both of them concurrently. This behaviour is controlled by passing proper constructor arguments).

Creates an instance of the AR1 strategy.

Parameters
  • criterion – The loss criterion to use. Defaults to None, in which case the cross entropy loss is used.

  • lr – The learning rate (SGD optimizer).

  • momentum – The momentum (SGD optimizer).

  • l2 – The L2 penalty used for weight decay.

  • train_epochs – The number of training epochs. Defaults to 4.

  • init_update_rate – The initial update rate of BatchReNorm layers.

  • inc_update_rate – The incremental update rate of BatchReNorm layers.

  • max_r_max – The maximum r value of BatchReNorm layers.

  • max_d_max – The maximum d value of BatchReNorm layers.

  • inc_step – The incremental step of r and d values of BatchReNorm layers.

  • rm_sz – The size of the replay buffer. The replay buffer is shared across classes. Defaults to 1500.

  • freeze_below_layer – A string describing the name of the layer to use while freezing the lower (nearest to the input) part of the model. The given layer is not frozen (exclusive).

  • latent_layer_num – The number of the layer to use as the Latent Replay Layer. Usually this is the same of freeze_below_layer.

  • ewc_lambda – The Synaptic Intelligence lambda term. Defaults to 0, which means that the Synaptic Intelligence regularization will not be applied.

  • train_mb_size – The train minibatch size. Defaults to 128.

  • eval_mb_size – The eval minibatch size. Defaults to 128.

  • device – The device to use. Defaults to None (cpu).

  • plugins – (optional) list of StrategyPlugins.

  • evaluator – (optional) instance of EvaluationPlugin for logging and metric computations.

  • eval_every

    the frequency of the calls to eval inside the training loop.

    if -1: no evaluation during training. if 0: calls eval after the final epoch of each training

    experience.

    if >0: calls eval every eval_every epochs and at the end

    of all the epochs for a single experience.

before_training_exp(self, **kwargs)[source]

Called after the dataset and data loader creation and before the training loop.

make_train_dataloader(self, num_workers=0, shuffle=True, **kwargs)[source]

Called after the dataset instantiation. Initialize the data loader.

For AR1 a “custom” dataloader is used: instead of using self.train_mb_size as the batch size, the data loader batch size will be computed ad self.train_mb_size - latent_mb_size. latent_mb_size is in turn computed as:

` len(train_dataset) // ((len(train_dataset) + len(replay_buffer) // self.train_mb_size) `

so that the number of iterations required to run an epoch on the current batch is equal to the number of iterations required to run an epoch on the replay buffer.

Parameters
  • num_workers – number of thread workers for the data loading.

  • shuffle – True if the data should be shuffled, False otherwise.

training_epoch(self, **kwargs)[source]

Training epoch. :param kwargs: :return:

after_training_exp(self, **kwargs)[source]
static filter_bn_and_brn(param_def: LayerAndParameter)[source]
class avalanche.training.Cumulative(model: Module, optimizer: Optimizer, criterion, train_mb_size: int = 1, train_epochs: int = 1, eval_mb_size: int = None, device=None, plugins: Optional[List[StrategyPlugin]] = None, evaluator: EvaluationPlugin = default_logger, eval_every=- 1)[source]

Bases: avalanche.training.strategies.BaseStrategy

Cumulative strategy. At each experience,

train model with data from all previous experiences and current experience.

Parameters
  • model – The model.

  • optimizer – The optimizer to use.

  • criterion – The loss criterion to use.

  • train_mb_size – The train minibatch size. Defaults to 1.

  • train_epochs – The number of training epochs. Defaults to 1.

  • eval_mb_size – The eval minibatch size. Defaults to 1.

  • device – The device to use. Defaults to None (cpu).

  • plugins – Plugins to be added. Defaults to None.

  • evaluator – (optional) instance of EvaluationPlugin for logging and metric computations.

  • eval_every

    the frequency of the calls to eval inside the training loop.

    if -1: no evaluation during training. if 0: calls eval after the final epoch of each training

    experience.

    if >0: calls eval every eval_every epochs and at the end

    of all the epochs for a single experience.

train_dataset_adaptation(self, **kwargs)[source]

Concatenates all the previous experiences.

class avalanche.training.BaseStrategy(model: Module, optimizer: Optimizer, criterion=CrossEntropyLoss(), train_mb_size: int = 1, train_epochs: int = 1, eval_mb_size: int = 1, device='cpu', plugins: Optional[Sequence['StrategyPlugin']] = None, evaluator=default_logger, eval_every=- 1)[source]

BaseStrategy is the super class of all task-based continual learning strategies. It implements a basic training loop and callback system that allows to execute code at each experience of the training loop. Plugins can be used to implement callbacks to augment the training loop with additional behavior (e.g. a memory buffer for replay).

Scenarios This strategy supports several continual learning scenarios:

  • class-incremental scenarios (no task labels)

  • multi-task scenarios, where task labels are provided)

  • multi-incremental scenarios, where the same task may be revisited

The exact scenario depends on the data stream and whether it provides the task labels.

Training loop The training loop is organized as follows:

train
    train_exp  # for each experience
        adapt_train_dataset
        train_dataset_adaptation
        make_train_dataloader
        train_epoch  # for each epoch
            # forward
            # backward
            # model update

Evaluation loop The evaluation loop is organized as follows:

eval
    eval_exp  # for each experience
        adapt_eval_dataset
        eval_dataset_adaptation
        make_eval_dataloader
        eval_epoch  # for each epoch
            # forward
            # backward
            # model update
Parameters
  • model – PyTorch model.

  • optimizer – PyTorch optimizer.

  • criterion – loss function.

  • train_mb_size – mini-batch size for training.

  • train_epochs – number of training epochs.

  • eval_mb_size – mini-batch size for eval.

  • device – PyTorch device where the model will be allocated.

  • plugins – (optional) list of StrategyPlugins.

  • evaluator – (optional) instance of EvaluationPlugin for logging and metric computations. None to remove logging.

  • eval_every

    the frequency of the calls to eval inside the training loop.

    if -1: no evaluation during training. if 0: calls eval after the final epoch of each training

    experience and before training on the first experience.

    if >0: calls eval every eval_every epochs, at the end

    of all the epochs for a single experience and before training on the first experience.

DISABLED_CALLBACKS :Sequence[str] = []
model :Module

PyTorch model.

optimizer

PyTorch optimizer.

train_epochs :int

Number of training epochs.

train_mb_size :int

Training mini-batch size.

eval_mb_size :int

Eval mini-batch size.

device

PyTorch device where the model will be allocated.

plugins

List of `StrategyPlugin`s.

evaluator

EvaluationPlugin used for logging and metric computations.

clock

Incremental counters for strategy events.

eval_every

Frequency of the evaluation during training.

experience

Current experience.

adapted_dataset

Data used to train. It may be modified by plugins. Plugins can append data to it (e.g. for replay).

Note

This dataset may contain samples from different experiences. If you want the original data for the current experience use BaseStrategy.experience.

dataloader

Dataloader.

mbatch

Current mini-batch.

mb_output

Model’s output computed on the current mini-batch.

loss

Loss of the current mini-batch.

is_training :bool = False

True if the strategy is in training mode.

current_eval_stream

User-provided evaluation stream on eval call.

property training_exp_counter(self)

Counts the number of training steps. +1 at the end of each experience.

property epoch(self)

Epoch counter.

property mb_it(self)

Iteration counter. Reset at the start of a new epoch.

property is_eval(self)

True if the strategy is in evaluation mode.

property mb_x(self)

Current mini-batch input.

property mb_y(self)

Current mini-batch target.

property mb_task_id(self)
criterion(self)[source]

Loss function.

train(self, experiences: Union[Experience, Sequence[Experience]], eval_streams: Optional[Sequence[Union[Experience, Sequence[Experience]]]] = None, **kwargs)[source]

Training loop. if experiences is a single element trains on it. If it is a sequence, trains the model on each experience in order. This is different from joint training on the entire stream. It returns a dictionary with last recorded value for each metric.

Parameters
  • experiences – single Experience or sequence.

  • eval_streams – list of streams for evaluation. If None: use training experiences for evaluation. Use [] if you do not want to evaluate during training.

Returns

dictionary containing last recorded value for each metric name.

train_exp(self, experience: Experience, eval_streams=None, **kwargs)[source]

Training loop over a single Experience object.

Parameters
  • experience – CL experience information.

  • eval_streams – list of streams for evaluation. If None: use the training experience for evaluation. Use [] if you do not want to evaluate during training.

  • kwargs – custom arguments.

stop_training(self)[source]

Signals to stop training at the next iteration.

train_dataset_adaptation(self, **kwargs)[source]

Initialize self.adapted_dataset.

eval(self, exp_list: Union[Experience, Sequence[Experience]], **kwargs)[source]

Evaluate the current model on a series of experiences and returns the last recorded value for each metric.

Parameters
  • exp_list – CL experience information.

  • kwargs – custom arguments.

Returns

dictionary containing last recorded value for each metric name

before_training_exp(self, **kwargs)[source]

Called after the dataset and data loader creation and before the training loop.

make_train_dataloader(self, num_workers=0, shuffle=True, pin_memory=True, **kwargs)[source]

Called after the dataset adaptation. Initializes the data loader. :param num_workers: number of thread workers for the data loading. :param shuffle: True if the data should be shuffled, False otherwise. :param pin_memory: If True, the data loader will copy Tensors into CUDA

pinned memory before returning them. Defaults to True.

make_eval_dataloader(self, num_workers=0, pin_memory=True, **kwargs)[source]

Initializes the eval data loader. :param num_workers: How many subprocesses to use for data loading.

0 means that the data will be loaded in the main process. (default: 0).

Parameters
  • pin_memory – If True, the data loader will copy Tensors into CUDA pinned memory before returning them. Defaults to True.

  • kwargs

Returns

after_train_dataset_adaptation(self, **kwargs)[source]

Called after the dataset adaptation and before the dataloader initialization. Allows to customize the dataset. :param kwargs: :return:

before_training_epoch(self, **kwargs)[source]

Called at the beginning of a new training epoch. :param kwargs: :return:

training_epoch(self, **kwargs)[source]

Training epoch. :param kwargs: :return:

before_training(self, **kwargs)[source]
after_training(self, **kwargs)[source]
before_training_iteration(self, **kwargs)[source]
before_forward(self, **kwargs)[source]
after_forward(self, **kwargs)[source]
before_backward(self, **kwargs)[source]
after_backward(self, **kwargs)[source]
after_training_iteration(self, **kwargs)[source]
before_update(self, **kwargs)[source]
after_update(self, **kwargs)[source]
after_training_epoch(self, **kwargs)[source]
after_training_exp(self, **kwargs)[source]
before_eval(self, **kwargs)[source]
before_eval_exp(self, **kwargs)[source]
eval_dataset_adaptation(self, **kwargs)[source]

Initialize self.adapted_dataset.

before_eval_dataset_adaptation(self, **kwargs)[source]
after_eval_dataset_adaptation(self, **kwargs)[source]
eval_epoch(self, **kwargs)[source]
after_eval_exp(self, **kwargs)[source]
after_eval(self, **kwargs)[source]
before_eval_iteration(self, **kwargs)[source]
before_eval_forward(self, **kwargs)[source]
after_eval_forward(self, **kwargs)[source]
after_eval_iteration(self, **kwargs)[source]
before_train_dataset_adaptation(self, **kwargs)[source]
model_adaptation(self)[source]
forward(self)[source]
make_optimizer(self)[source]
class avalanche.training.JointTraining(model: Module, optimizer: Optimizer, criterion, train_mb_size: int = 1, train_epochs: int = 1, eval_mb_size: int = 1, device='cpu', plugins: Optional[Sequence['StrategyPlugin']] = None, evaluator=default_logger)[source]

Bases: avalanche.training.strategies.BaseStrategy

JointTraining performs joint training (also called offline training) on the entire stream of data. This means that it is not a continual learning strategy but it can be used as an “offline” upper bound for them.

Parameters
  • model – PyTorch model.

  • optimizer – PyTorch optimizer.

  • criterion – loss function.

  • train_mb_size – mini-batch size for training.

  • train_epochs – number of training epochs.

  • eval_mb_size – mini-batch size for eval.

  • device – PyTorch device to run the model.

  • plugins – (optional) list of StrategyPlugins.

  • evaluator – (optional) instance of EvaluationPlugin for logging and metric computations. None to remove logging.

train(self, experiences: Union[Experience, Sequence[Experience]], eval_streams: Optional[Sequence[Union[Experience, Sequence[Experience]]]] = None, **kwargs)[source]

Training loop. if experiences is a single element trains on it. If it is a sequence, trains the model on each experience in order. This is different from joint training on the entire stream. It returns a dictionary with last recorded value for each metric.

Parameters
  • experiences – single Experience or sequence.

  • eval_streams – list of streams for evaluation. If None: use training experiences for evaluation. Use [] if you do not want to evaluate during training.

Returns

dictionary containing last recorded value for each metric name.

train_dataset_adaptation(self, **kwargs)[source]

Concatenates all the datastream.

class avalanche.training.Naive(model: Module, optimizer: Optimizer, criterion=CrossEntropyLoss(), train_mb_size: int = 1, train_epochs: int = 1, eval_mb_size: int = None, device=None, plugins: Optional[List[StrategyPlugin]] = None, evaluator: EvaluationPlugin = default_logger, eval_every=- 1)[source]

Bases: avalanche.training.strategies.base_strategy.BaseStrategy

The simplest (and least effective) Continual Learning strategy. Naive just incrementally fine tunes a single model without employing any method to contrast the catastrophic forgetting of previous knowledge. This strategy does not use task identities.

Naive is easy to set up and its results are commonly used to show the worst performing baseline.

Creates an instance of the Naive strategy.

Parameters
  • model – The model.

  • optimizer – The optimizer to use.

  • criterion – The loss criterion to use.

  • train_mb_size – The train minibatch size. Defaults to 1.

  • train_epochs – The number of training epochs. Defaults to 1.

  • eval_mb_size – The eval minibatch size. Defaults to 1.

  • device – The device to use. Defaults to None (cpu).

  • plugins – Plugins to be added. Defaults to None.

  • evaluator – (optional) instance of EvaluationPlugin for logging and metric computations.

  • eval_every

    the frequency of the calls to eval inside the training loop.

    if -1: no evaluation during training. if 0: calls eval after the final epoch of each training

    experience.

    if >0: calls eval every eval_every epochs and at the end

    of all the epochs for a single experience.

class avalanche.training.CWRStar(model: Module, optimizer: Optimizer, criterion, cwr_layer_name: str, train_mb_size: int = 1, train_epochs: int = 1, eval_mb_size: int = None, device=None, plugins: Optional[List[StrategyPlugin]] = None, evaluator: EvaluationPlugin = default_logger, eval_every=- 1)[source]

Bases: avalanche.training.strategies.base_strategy.BaseStrategy

CWR* Strategy. This strategy does not use task identities.

Parameters
  • model – The model.

  • optimizer – The optimizer to use.

  • criterion – The loss criterion to use.

  • cwr_layer_name – name of the CWR layer. Defaults to None, which means that the last fully connected layer will be used.

  • train_mb_size – The train minibatch size. Defaults to 1.

  • train_epochs – The number of training epochs. Defaults to 1.

  • eval_mb_size – The eval minibatch size. Defaults to 1.

  • device – The device to use. Defaults to None (cpu).

  • plugins – Plugins to be added. Defaults to None.

  • evaluator – (optional) instance of EvaluationPlugin for logging and metric computations.

  • eval_every

    the frequency of the calls to eval inside the training loop.

    if -1: no evaluation during training. if 0: calls eval after the final epoch of each training

    experience.

    if >0: calls eval every eval_every epochs and at the end

    of all the epochs for a single experience.

class avalanche.training.Replay(model: Module, optimizer: Optimizer, criterion, mem_size: int = 200, train_mb_size: int = 1, train_epochs: int = 1, eval_mb_size: int = None, device=None, plugins: Optional[List[StrategyPlugin]] = None, evaluator: EvaluationPlugin = default_logger, eval_every=- 1)[source]

Bases: avalanche.training.strategies.base_strategy.BaseStrategy

Experience replay strategy. See ReplayPlugin for more details. This strategy does not use task identities.

Parameters
  • model – The model.

  • optimizer – The optimizer to use.

  • criterion – The loss criterion to use.

  • mem_size – replay buffer size.

  • train_mb_size – The train minibatch size. Defaults to 1.

  • train_epochs – The number of training epochs. Defaults to 1.

  • eval_mb_size – The eval minibatch size. Defaults to 1.

  • device – The device to use. Defaults to None (cpu).

  • plugins – Plugins to be added. Defaults to None.

  • evaluator – (optional) instance of EvaluationPlugin for logging and metric computations.

  • eval_every

    the frequency of the calls to eval inside the training loop.

    if -1: no evaluation during training. if 0: calls eval after the final epoch of each training

    experience.

    if >0: calls eval every eval_every epochs and at the end

    of all the epochs for a single experience.

class avalanche.training.GDumb(model: Module, optimizer: Optimizer, criterion, mem_size: int = 200, train_mb_size: int = 1, train_epochs: int = 1, eval_mb_size: int = None, device=None, plugins: Optional[List[StrategyPlugin]] = None, evaluator: EvaluationPlugin = default_logger, eval_every=- 1)[source]

Bases: avalanche.training.strategies.base_strategy.BaseStrategy

GDumb strategy. See GDumbPlugin for more details. This strategy does not use task identities.

Parameters
  • model – The model.

  • optimizer – The optimizer to use.

  • criterion – The loss criterion to use.

  • mem_size – replay buffer size.

  • train_mb_size – The train minibatch size. Defaults to 1.

  • train_epochs – The number of training epochs. Defaults to 1.

  • eval_mb_size – The eval minibatch size. Defaults to 1.

  • device – The device to use. Defaults to None (cpu).

  • plugins – Plugins to be added. Defaults to None.

  • evaluator – (optional) instance of EvaluationPlugin for logging and metric computations.

  • eval_every

    the frequency of the calls to eval inside the training loop.

    if -1: no evaluation during training. if 0: calls eval after the final epoch of each training

    experience.

    if >0: calls eval every eval_every epochs and at the end

    of all the epochs for a single experience.

class avalanche.training.LwF(model: Module, optimizer: Optimizer, criterion, alpha: Union[float, Sequence[float]], temperature: float, train_mb_size: int = 1, train_epochs: int = 1, eval_mb_size: int = None, device=None, plugins: Optional[List[StrategyPlugin]] = None, evaluator: EvaluationPlugin = default_logger, eval_every=- 1)[source]

Bases: avalanche.training.strategies.base_strategy.BaseStrategy

Learning without Forgetting strategy.

See LwF plugin for details. This strategy does not use task identities.

Parameters
  • model – The model.

  • optimizer – The optimizer to use.

  • criterion – The loss criterion to use.

  • alpha – distillation hyperparameter. It can be either a float number or a list containing alpha for each experience.

  • temperature – softmax temperature for distillation

  • train_mb_size – The train minibatch size. Defaults to 1.

  • train_epochs – The number of training epochs. Defaults to 1.

  • eval_mb_size – The eval minibatch size. Defaults to 1.

  • device – The device to use. Defaults to None (cpu).

  • plugins – Plugins to be added. Defaults to None.

  • evaluator – (optional) instance of EvaluationPlugin for logging and metric computations.

  • eval_every

    the frequency of the calls to eval inside the training loop.

    if -1: no evaluation during training. if 0: calls eval after the final epoch of each training

    experience.

    if >0: calls eval every eval_every epochs and at the end

    of all the epochs for a single experience.

class avalanche.training.AGEM(model: Module, optimizer: Optimizer, criterion, patterns_per_exp: int, sample_size: int = 64, train_mb_size: int = 1, train_epochs: int = 1, eval_mb_size: int = None, device=None, plugins: Optional[List[StrategyPlugin]] = None, evaluator: EvaluationPlugin = default_logger, eval_every=- 1)[source]

Bases: avalanche.training.strategies.base_strategy.BaseStrategy

Average Gradient Episodic Memory (A-GEM) strategy.

See AGEM plugin for details. This strategy does not use task identities.

Parameters
  • model – The model.

  • optimizer – The optimizer to use.

  • criterion – The loss criterion to use.

  • patterns_per_exp – number of patterns per experience in the memory

  • sample_size – number of patterns in memory sample when computing reference gradient.

  • train_mb_size – The train minibatch size. Defaults to 1.

  • train_epochs – The number of training epochs. Defaults to 1.

  • eval_mb_size – The eval minibatch size. Defaults to 1.

  • device – The device to use. Defaults to None (cpu).

  • plugins – Plugins to be added. Defaults to None.

  • evaluator – (optional) instance of EvaluationPlugin for logging and metric computations.

  • eval_every

    the frequency of the calls to eval inside the training loop.

    if -1: no evaluation during training. if 0: calls eval after the final epoch of each training

    experience.

    if >0: calls eval every eval_every epochs and at the end

    of all the epochs for a single experience.

class avalanche.training.GEM(model: Module, optimizer: Optimizer, criterion, patterns_per_exp: int, memory_strength: float = 0.5, train_mb_size: int = 1, train_epochs: int = 1, eval_mb_size: int = None, device=None, plugins: Optional[List[StrategyPlugin]] = None, evaluator: EvaluationPlugin = default_logger, eval_every=- 1)[source]

Bases: avalanche.training.strategies.base_strategy.BaseStrategy

Gradient Episodic Memory (GEM) strategy.

See GEM plugin for details. This strategy does not use task identities.

Parameters
  • model – The model.

  • optimizer – The optimizer to use.

  • criterion – The loss criterion to use.

  • patterns_per_exp – number of patterns per experience in the memory

  • memory_strength – offset to add to the projection direction in order to favour backward transfer (gamma in original paper).

  • train_mb_size – The train minibatch size. Defaults to 1.

  • train_epochs – The number of training epochs. Defaults to 1.

  • eval_mb_size – The eval minibatch size. Defaults to 1.

  • device – The device to use. Defaults to None (cpu).

  • plugins – Plugins to be added. Defaults to None.

  • evaluator – (optional) instance of EvaluationPlugin for logging and metric computations.

  • eval_every

    the frequency of the calls to eval inside the training loop.

    if -1: no evaluation during training. if 0: calls eval after the final epoch of each training

    experience.

    if >0: calls eval every eval_every epochs and at the end

    of all the epochs for a single experience.

class avalanche.training.EWC(model: Module, optimizer: Optimizer, criterion, ewc_lambda: float, mode: str = 'separate', decay_factor: Optional[float] = None, keep_importance_data: bool = False, train_mb_size: int = 1, train_epochs: int = 1, eval_mb_size: int = None, device=None, plugins: Optional[List[StrategyPlugin]] = None, evaluator: EvaluationPlugin = default_logger, eval_every=- 1)[source]

Bases: avalanche.training.strategies.base_strategy.BaseStrategy

Elastic Weight Consolidation (EWC) strategy.

See EWC plugin for details. This strategy does not use task identities.

Parameters
  • model – The model.

  • optimizer – The optimizer to use.

  • criterion – The loss criterion to use.

  • ewc_lambda – hyperparameter to weigh the penalty inside the total loss. The larger the lambda, the larger the regularization.

  • modeseparate to keep a separate penalty for each previous experience. onlinesum to keep a single penalty summed over all previous tasks. onlineweightedsum to keep a single penalty summed with a decay factor over all previous tasks.

  • decay_factor – used only if mode is onlineweightedsum. It specify the decay term of the importance matrix.

  • keep_importance_data – if True, keep in memory both parameter values and importances for all previous task, for all modes. If False, keep only last parameter values and importances. If mode is separate, the value of keep_importance_data is set to be True.

  • train_mb_size – The train minibatch size. Defaults to 1.

  • train_epochs – The number of training epochs. Defaults to 1.

  • eval_mb_size – The eval minibatch size. Defaults to 1.

  • device – The device to use. Defaults to None (cpu).

  • plugins – Plugins to be added. Defaults to None.

  • evaluator – (optional) instance of EvaluationPlugin for logging and metric computations.

  • eval_every

    the frequency of the calls to eval inside the training loop.

    if -1: no evaluation during training. if 0: calls eval after the final epoch of each training

    experience.

    if >0: calls eval every eval_every epochs and at the end

    of all the epochs for a single experience.

class avalanche.training.SynapticIntelligence(model: Module, optimizer: Optimizer, criterion, si_lambda: Union[float, Sequence[float]], eps: float = 1e-07, train_mb_size: int = 1, train_epochs: int = 1, eval_mb_size: int = 1, device='cpu', plugins: Optional[Sequence['StrategyPlugin']] = None, evaluator=default_logger, eval_every=- 1)[source]

Bases: avalanche.training.strategies.base_strategy.BaseStrategy

The Synaptic Intelligence strategy.

This is the Synaptic Intelligence PyTorch implementation of the algorithm described in the paper “Continuous Learning in Single-Incremental-Task Scenarios” (https://arxiv.org/abs/1806.08568)

The original implementation has been proposed in the paper “Continual Learning Through Synaptic Intelligence” (https://arxiv.org/abs/1703.04200).

The Synaptic Intelligence regularization can also be used in a different strategy by applying the SynapticIntelligencePlugin plugin.

Creates an instance of the Synaptic Intelligence strategy.

Parameters
  • model – PyTorch model.

  • optimizer – PyTorch optimizer.

  • criterion – loss function.

  • si_lambda – Synaptic Intelligence lambda term. If list, one lambda for each experience. If the list has less elements than the number of experiences, last lambda will be used for the remaining experiences.

  • eps – Synaptic Intelligence damping parameter.

  • train_mb_size – mini-batch size for training.

  • train_epochs – number of training epochs.

  • eval_mb_size – mini-batch size for eval.

  • device – PyTorch device to run the model.

  • plugins – (optional) list of StrategyPlugins.

  • evaluator – (optional) instance of EvaluationPlugin for logging and metric computations.

  • eval_every

    the frequency of the calls to eval inside the training loop.

    if -1: no evaluation during training. if 0: calls eval after the final epoch of each training

    experience.

    if >0: calls eval every eval_every epochs and at the end

    of all the epochs for a single experience.

class avalanche.training.GSS_greedy(model: Module, optimizer: Optimizer, criterion, mem_size: int = 200, mem_strength=1, input_size=[], train_mb_size: int = 1, train_epochs: int = 1, eval_mb_size: int = None, device=None, plugins: Optional[List[StrategyPlugin]] = None, evaluator: EvaluationPlugin = default_logger, eval_every=- 1)[source]

Bases: avalanche.training.strategies.base_strategy.BaseStrategy

Experience replay strategy. See ReplayPlugin for more details. This strategy does not use task identities.

Parameters
  • model – The model.

  • optimizer – The optimizer to use.

  • criterion – The loss criterion to use.

  • mem_size – replay buffer size.

  • n – memory random set size.

  • train_mb_size – The train minibatch size. Defaults to 1.

  • train_epochs – The number of training epochs. Defaults to 1.

  • eval_mb_size – The eval minibatch size. Defaults to 1.

  • device – The device to use. Defaults to None (cpu).

  • plugins – Plugins to be added. Defaults to None.

  • evaluator – (optional) instance of EvaluationPlugin for logging and metric computations.

  • eval_every

    the frequency of the calls to eval inside the training loop.

    if -1: no evaluation during training. if 0: calls eval after the final epoch of each training

    experience.

    if >0: calls eval every eval_every epochs and at the end

    of all the epochs for a single experience.

class avalanche.training.CoPE(model: Module, optimizer: Optimizer, criterion, mem_size: int = 200, n_classes: int = 10, p_size: int = 100, alpha: float = 0.99, T: float = 0.1, train_mb_size: int = 1, train_epochs: int = 1, eval_mb_size: int = None, device=None, plugins: Optional[List[StrategyPlugin]] = None, evaluator: EvaluationPlugin = default_logger, eval_every=- 1)[source]

Bases: avalanche.training.strategies.base_strategy.BaseStrategy

Continual Prototype Evolution strategy. See CoPEPlugin for more details. This strategy does not use task identities during training.

Parameters
  • model – The model.

  • optimizer – The optimizer to use.

  • criterion – Loss criterion to use. Standard overwritten by

PPPloss (see CoPEPlugin). :param mem_size: replay buffer size. :param n_classes: total number of classes that will be encountered. This is used to output predictions for all classes, with zero probability for unseen classes. :param p_size: The prototype size, which equals the feature size of the last layer. :param alpha: The momentum for the exponentially moving average of the prototypes. :param T: The softmax temperature, used as a concentration parameter. :param train_mb_size: The train minibatch size. Defaults to 1. :param train_epochs: The number of training epochs. Defaults to 1. :param eval_mb_size: The eval minibatch size. Defaults to 1. :param device: The device to use. Defaults to None (cpu). :param plugins: Plugins to be added. Defaults to None. :param evaluator: (optional) instance of EvaluationPlugin for logging

and metric computations.

Parameters

eval_every

the frequency of the calls to eval inside the training loop.

if -1: no evaluation during training. if 0: calls eval after the final epoch of each training

experience.

if >0: calls eval every eval_every epochs and at the end

of all the epochs for a single experience.

class avalanche.training.LFL(model: Module, optimizer: Optimizer, criterion, lambda_e: Union[float, Sequence[float]], train_mb_size: int = 1, train_epochs: int = 1, eval_mb_size: int = None, device=None, plugins: Optional[List[StrategyPlugin]] = None, evaluator: EvaluationPlugin = default_logger, eval_every=- 1)[source]

Bases: avalanche.training.strategies.base_strategy.BaseStrategy

Less Forgetful Learning strategy.

See LFL plugin for details. Refer Paper: https://arxiv.org/pdf/1607.00122.pdf This strategy does not use task identities.

Parameters
  • model – The model.

  • optimizer – The optimizer to use.

  • criterion – The loss criterion to use.

  • lambda_e – euclidean loss hyper parameter. It can be either a float number or a list containing lambda_e for each experience.

  • train_mb_size – The train minibatch size. Defaults to 1.

  • train_epochs – The number of training epochs. Defaults to 1.

  • eval_mb_size – The eval minibatch size. Defaults to 1.

  • device – The device to use. Defaults to None (cpu).

  • plugins – Plugins to be added. Defaults to None.

  • evaluator – (optional) instance of EvaluationPlugin for logging and metric computations.

  • eval_every

    the frequency of the calls to eval inside the training loop.

    if -1: no evaluation during training. if 0: calls eval after the final epoch of each training

    experience.

    if >0: calls eval every eval_every epochs and at the end

    of all the epochs for a single experience.

class avalanche.training.StreamingLDA(slda_model, criterion, input_size, num_classes, output_layer_name=None, shrinkage_param=0.0001, streaming_update_sigma=True, train_epochs: int = 1, train_mb_size: int = 1, eval_mb_size: int = 1, device='cpu', plugins: Optional[Sequence['StrategyPlugin']] = None, evaluator=default_logger, eval_every=- 1)[source]

Bases: avalanche.training.strategies.BaseStrategy

Init function for the SLDA model. :param slda_model: a PyTorch model :param criterion: loss function :param output_layer_name: if not None, wrap model to retrieve

only the output_layer_name output. If None, the strategy assumes that the model already produces a valid output. You can use FeatureExtractorBackbone class to create your custom SLDA-compatible model.

Parameters
  • input_size – feature dimension

  • num_classes – number of total classes in stream

  • train_mb_size – batch size for feature extractor during training. Fit will be called on a single pattern at a time.

  • eval_mb_size – batch size for inference

  • shrinkage_param – value of the shrinkage parameter

  • streaming_update_sigma – True if sigma is plastic else False

feature extraction in self.feature_extraction_wrapper’ :param plugins: list of StrategyPlugins :param evaluator: Evaluation Plugin instance :param eval_every: run eval every `eval_every epochs.

See BaseStrategy for details.

DISABLED_CALLBACKS = ['before_backward', 'after_backward']

Deep Streaming Linear Discriminant Analysis. This strategy does not use backpropagation. Minibatches are first passed to the pretrained feature extractor. The result is processed one element at a time to fit the LDA. Original paper: “Hayes et. al., Lifelong Machine Learning with Deep Streaming Linear Discriminant Analysis, CVPR Workshop, 2020” https://openaccess.thecvf.com/content_CVPRW_2020/papers/w15/Hayes_Lifelong_Machine_Learning_With_Deep_Streaming_Linear_Discriminant_Analysis_CVPRW_2020_paper.pdf

forward(self, return_features=False)[source]
training_epoch(self, **kwargs)[source]

Training epoch. :param kwargs: :return:

make_optimizer(self)[source]
fit(self, x, y)[source]

Fit the SLDA model to a new sample (x,y). :param x: a torch tensor of the input data (must be a vector) :param y: a torch tensor of the input label :return: None

predict(self, X)[source]

Make predictions on test data X. :param X: a torch tensor that contains N data samples (N x d) :param return_probas: True if the user would like probabilities instead of predictions returned :return: the test predictions or probabilities

fit_base(self, X, y)[source]

Fit the SLDA model to the base data. :param X: an Nxd torch tensor of base initialization data :param y: an Nx1-dimensional torch tensor of the associated labels for X :return: None

save_model(self, save_path, save_name)[source]

Save the model parameters to a torch file. :param save_path: the path where the model will be saved :param save_name: the name for the saved file :return:

load_model(self, save_path, save_name)[source]

Load the model parameters into StreamingLDA object. :param save_path: the path where the model is saved :param save_name: the name of the saved file :return: