class_docstr: Maximally Interfered Retrieval plugin,
  Implements the strategy defined in
  "Online Continual Learning with Maximally Interfered Retrieval"
  https://arxiv.org/abs/1908.04742
  
  This strategy has been designed and tested in the
  Online Setting (OnlineCLScenario). However, it
  can also be used in non-online scenarios
code: def before_backward(self, strategy, **kwargs):
      if self.replay_loader is None:
          return
      samples_x, samples_y, samples_tid = next(self.replay_loader)
      samples_x, samples_y, samples_tid = (
          samples_x.to(strategy.device),
          samples_y.to(strategy.device),
          samples_tid.to(strategy.device),
      )
      # Perform the temporary update with current data
      grad = torch.autograd.grad(
          strategy.loss,
          strategy.model.parameters(),
          retain_graph=True,
          allow_unused=True,
      )
      model_updated = update_temp(
          strategy.model, grad, strategy.optimizer.param_groups[0]["lr"]
      )
      # Selection of the most interfering samples, no grad required
      # plus we put the model in eval mode so that the additional
      # forward pass don't influence the batch norm statistics
      # strategy.model.eval()
      # model_updated.eval()
      with torch.no_grad():
          _old_red_strategy = strategy._criterion.reduction
          strategy._criterion.reduction = "none"
          old_output = avalanche_forward(
              strategy.model, samples_x, samples_tid
          )
          old_loss = strategy._criterion(old_output, samples_y)
          new_output = avalanche_forward(
              model_updated, samples_x, samples_tid
          )
          new_loss = strategy._criterion(new_output, samples_y)
          loss_diff = new_loss - old_loss
          chosen_samples_indexes = torch.argsort(loss_diff)[
              len(samples_x) - self.batch_size_mem :
          ]
          strategy._criterion.reduction = _old_red_strategy
      # strategy.model.train()
      # Choose the samples and add their loss to the current loss
      chosen_samples_x, chosen_samples_y, chosen_samples_tid = (
          samples_x[chosen_samples_indexes],
          samples_y[chosen_samples_indexes],
          samples_tid[chosen_samples_indexes],
      )
      replay_output = avalanche_forward(
          strategy.model, chosen_samples_x, chosen_samples_tid
      )
      replay_loss = strategy._criterion(replay_output, chosen_samples_y)
      strategy.loss += replay_loss
  
file_path: avalanche/avalanche/training/plugins/mir.py
imports: import copy
  from typing import TYPE_CHECKING
  import torch
  from avalanche.benchmarks.utils import concat_datasets
  from avalanche.models.utils import avalanche_forward
  from avalanche.training.plugins.strategy_plugin import SupervisedPlugin
  from avalanche.training.storage_policy import ClassBalancedBuffer
label_desc: Use image node_id 2 for calling the MIRPlugin obj's underlying member method code with example usage: obj.before_backward(strategy) without return types