class_docstr: Average Gradient Episodic Memory Plugin.
  
  AGEM projects the gradient on the current minibatch by using an external
  episodic memory of patterns from previous experiences. If the dot product
  between the current gradient and the (average) gradient of a randomly
  sampled set of memory examples is negative, the gradient is projected.
  This plugin does not use task identities.
class_docstr_tok: ["Average","Gradient","Episodic","Memory","Plugin",".","AGEM","projects","the","gradient","on","the","current","minibatch","by","using","an","external","episodic","memory","of","patterns","from","previous","experiences",".","If","the","dot","product","between","the","current","gradient","and","the","(","average",")","gradient","of","a","randomly","sampled","set","of","memory","examples","is","negative",",","the","gradient","is","projected",".","This","plugin","does","not","use","task","identities","."]
docstr: Compute reference gradient on memory sample.
docstr_tok: ["Compute","reference","gradient","on","memory","sample","."]
code: def before_training_iteration(self, strategy, **kwargs):
      """
      Compute reference gradient on memory sample.
      """
  
      if len(self.buffers) > 0:
          strategy.model.train()
          strategy.optimizer.zero_grad()
          mb = self.sample_from_memory()
          xref, yref, tid = mb[0], mb[1], mb[-1]
          xref, yref = xref.to(strategy.device), yref.to(
              strategy.device
          )
  
          out = avalanche_forward(strategy.model, xref, tid)
          loss = strategy._criterion(out, yref)
          loss.backward()
          # gradient can be None for some head on multi-headed models
          reference_gradients_list = [
              p.grad.view(-1)
              if p.grad is not None
              else torch.zeros(p.numel(), device=strategy.device)
              for n, p in strategy.model.named_parameters()
          ]
          self.reference_gradients = torch.cat(reference_gradients_list)
          strategy.optimizer.zero_grad()
  
file_path: avalanche/avalanche/training/plugins/agem.py
imports: import warnings
  import random
  from typing import Any, Iterator, List, Optional
  import torch
  from torch import Tensor
  from avalanche.benchmarks.utils import _make_taskaware_classification_dataset
  from avalanche.benchmarks.utils.data import AvalancheDataset
  from avalanche.benchmarks.utils.data_loader import GroupBalancedInfiniteDataLoader
  from avalanche.models import avalanche_forward
  from avalanche.training.plugins.strategy_plugin import SupervisedPlugin
label_desc: Use image node_id 2 for calling the AGEMPlugin obj's underlying member method code with example usage: obj.before_training_iteration(strategy) without return types