code: def _add_activation_hook(model, input, output, *, recs, layer_name):
      """Helper function for adding activation hooks"""
  
      # only record training data
      if not model.training:
          return
  
      # very first batch
      if not recs:
          recs.append({})
  
      if layer_name in recs[-1]:
          # if the layer is already there, the entry is from the previous training
          # step, thus start a new one
          recs.append({})
  
      val = to_numpy(output)
  
      # disambiguate activations when the output is not a simple array
      if isinstance(val, np.ndarray):
          recs[-1][layer_name] = val
      elif isinstance(val, (list, tuple)):
          for i, v in enumerate(val):
              if not isinstance(v, np.ndarray):
                  raise TypeError(
                      f"activations of type {type(v)} are not supported"
                  )
              recs[-1][layer_name + f"[{i}]"] = v
      elif isinstance(val, Mapping):
          for k, v in val.items():
              if not isinstance(v, np.ndarray):
                  raise TypeError(
                      f"Activations of type {type(v)} are not supported"
                  )
              recs[-1][layer_name + f'["{k}"]'] = v
      else:
          raise TypeError(
              f"Activations of type {type(v)} are not supported"
          )
  
imports: from collections.abc import Mapping
  from functools import partial
  import numpy
  from sklearn.exceptions import NotFittedError
  from sklearn.utils.validation import check_is_fitted
  import torch
  from skorch.exceptions import NotInitializedError
  from skorch.utils import to_numpy
label_desc: Use image node_id 2 for calling a global function with example usage: _add_activation_hook(model, input, output) without return types