class_docstr: Elastic Weight Consolidation (EWC) plugin.
  EWC computes importance of each weight at the end of training on current
  experience. During training on each minibatch, the loss is augmented
  with a penalty which keeps the value of the current weights close to the
  value they had on previous experiences in proportion to their importance
  on that experience. Importances are computed with an additional pass on the
  training set. This plugin does not use task identities.
docstr: Compute EWC importance matrix for each parameter
docstr_tok: ["Compute","EWC","importance","matrix","for","each","parameter"]
code: def compute_importances(
      self,
      model,
      criterion,
      optimizer,
      dataset,
      device,
      batch_size,
      num_workers=0,
  ) -> Dict[str, ParamData]:
      """
      Compute EWC importance matrix for each parameter
      """
  
      model.eval()
  
      # Set RNN-like modules on GPU to training mode to avoid CUDA error
      if device == "cuda":
          for module in model.modules():
              if isinstance(module, torch.nn.RNNBase):
                  warnings.warn(
                      "RNN-like modules do not support "
                      "backward calls while in `eval` mode on CUDA "
                      "devices. Setting all `RNNBase` modules to "
                      "`train` mode. May produce inconsistent "
                      "output if such modules have `dropout` > 0."
                  )
                  module.train()
  
      # list of list
      importances = zerolike_params_dict(model)
      collate_fn = (
          dataset.collate_fn if hasattr(dataset, "collate_fn") else None
      )
      dataloader = DataLoader(
          dataset,
          batch_size=batch_size,
          collate_fn=collate_fn,
          num_workers=num_workers,
      )
      for i, batch in enumerate(dataloader):
          # get only input, target and task_id from the batch
          x, y, task_labels = batch[0], batch[1], batch[-1]
          x, y = x.to(device), y.to(device)
  
          optimizer.zero_grad()
          out = avalanche_forward(model, x, task_labels)
          loss = criterion(out, y)
          loss.backward()
  
          for (k1, p), (k2, imp) in zip(
              model.named_parameters(), importances.items()
          ):
              assert k1 == k2
              if p.grad is not None:
                  imp.data += p.grad.data.clone().pow(2)
  
      # average over mini batch length
      for _, imp in importances.items():
          imp.data /= float(len(dataloader))
  
      model.train()
  
      return importances
  
file_path: avalanche/avalanche/training/plugins/ewc.py
imports: from collections import defaultdict
  from typing import Dict, Tuple, Union
  import warnings
  import itertools
  import torch
  from torch.utils.data import DataLoader
  from avalanche.models.utils import avalanche_forward
  from avalanche.training.plugins.strategy_plugin import SupervisedPlugin
  from avalanche.training.utils import copy_params_dict, zerolike_params_dict, ParamData
label_desc: Use image node_id 4 for calling the EWCPlugin obj's underlying member method code with example usage: obj.compute_importances(model, criterion, optimizer, dataset, device, batch_size, num_workers) and returns: importances