code: def add_activation_hooks(net, match_fn=None):
      """Add forward hooks to all layers to record activations
  
      Ignore the top level modules like ``net.module_`` and ``net.criterion_``.
  
      If an output is not a simple array, it is disambiguated. E.g. if it's a
      list, the name get a suffix of ``[i]`` where ``i`` designates the index in
      the list. Similary, when the output is a dict, a ``[key]`` suffix is added,
      where ``[key]`` is the key of the corresponding value in the dictionary.
  
      Parameters
      ----------
      net : skorch.NeuralNet
        The neural net instance (has to be initialized).
  
      match_fn : callable or None (default=None)
        If not ``None``, this should be a callable/function that takes the name of
        a layer as input and returns a bool as output, where ``False`` indicates
        that this layer should be excluded.
  
      Returns
      -------
      recs : dict of list of dict
        Data structure containing the recorded activations. For each module, for
        each training step, for each layer, there is an entry of the recorded
        activations.
  
      handles : list of torch.utils.hooks.RemovableHandle
        These handles are returned by torch when adding hooks. They can be used to
        remove the hooks.
  
      Raises
      ------
      TypeError
        When the output of a layer is not a simple array and if it cannot be
        disambiguated, a ``TypeError`` is raised. Disambiguation works for lists,
        tuples, and dicts (but not nested ones).
  
      """
      recs = {}
      handles = []
  
      for module_name, module in named_modules(net):
          recs[module_name] = []
          for layer_name, submodule in module.named_modules():
              if match_fn and not match_fn(layer_name):
                  continue
  
              if submodule is module:
                  # is recording activations for whole module useful? skip for now
                  continue
  
              handle = submodule.register_forward_hook(
                  partial(
                      _add_activation_hook,
                      recs=recs[module_name],
                      layer_name=layer_name,
                  )
              )
              handles.append(handle)
  
      return recs, handles
  
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 3 for calling a global function with example usage: add_activation_hooks(net, match_fn) and returns: recs,  handles