class_docstr: This is a computationally-inefficient implementation that computes the type of
  "infinitesimal" influence scores defined in the paper "Understanding Black-box
  Predictions via Influence Functions" by Koh et al
  (https://arxiv.org/pdf/1703.04730.pdf). The computational bottleneck in computing
  infinitesimal influence scores is computing inverse Hessian-vector products, as can
  be seen from its definition in `InfluenceFunctionBase`. This implementation is
  inefficient / naive in that it explicitly forms the Hessian :math`H`, unlike other
  implementations which compute inverse Hessian-vector products without explicitly
  forming the Hessian. The purpose of this implementation is to have a way to
  generate the "ground-truth" influence scores, to which other implementations,
  which are more efficient but return only approximations of the influence score, can
  be compared.
  
  This implementation computes a low-rank approximation of the inverse Hessian, i.e.
  a tall and skinny (with width k) matrix :math`R` such that
  :math`H^{-1} \approx RR'`, where k is small. In particular, let :math`L` be the
  matrix of width k whose columns contain the top-k eigenvectors of :math`H`, and let
  :math`V` be the k by k matrix whose diagonals contain the corresponding eigenvalues.
  This implementation lets :math`R=LV^{-1}L'`. Thus, the core computational step is
  computing the top-k eigenvalues / eigenvectors.
  
  This low-rank approximation is useful for several reasons:
  - It avoids numerical issues associated with inverting small eigenvalues.
  - Since the influence score is given by
    :math`\nabla_\theta L(x)' H^{-1} \nabla_\theta L(z)`, which is approximated by
    :math`(\nabla_\theta L(x)' R) (\nabla_\theta L(z)' R)`, we can compute an
    "influence embedding" for a given example :math`x`, :math`\nabla_\theta L(x)' R`,
    such that the influence score of one example on another is approximately the
    dot-product of their respective embeddings.
  
  This implementation is "naive" in that it computes the top-k eigenvalues /
  eigenvectors by explicitly forming the Hessian, converting it to a 2D tensor,
  computing its eigenvectors / eigenvalues, and then sorting. See documentation of the
  `_retrieve_projections_naive_influence_function` method for more details.
docstr: Returns the matrix `R` described in the documentation for
  `NaiveInfluenceFunction`. In short, `R` has the property that
  :math`H^{-1} \approx RR'`, where `H` is the Hessian. Since this is a "naive"
  implementation, it does so by explicitly forming the Hessian, converting
  it to a 2D tensor, and computing its eigenvectors / eigenvalues, before
  filtering out some eigenvalues and then inverting them. The returned matrix
  `R` represents a set of parameters in parameter space. Since the Hessian
  is obtained by first flattening the parameters, each column of `R` corresponds
  to a *flattened* parameter in parameter space.
  
  Args:
      dataloader (DataLoader): The returned matrix `R` gives a low-rank
              approximation of the Hessian `H`. This dataloader defines the
              dataset used to compute the Hessian that is being approximated.
      projection_on_cpu (bool, optional): Whether to move the projection,
              i.e. low-rank approximation of the inverse Hessian, to cpu, to save
              gpu memory.
      show_progress (bool): Computing the Hessian that is being approximated
              requires summing up the Hessians computed using different batches,
              which may take a long time. If `show_progress` is true, the number
              of batches that have been processed will be displayed. It will try
              to use tqdm if available for advanced features (e.g. time
              estimation). Otherwise, it will fallback to a simple output of
              progress.
  
  Returns:
      R (Tensor): Tall and skinny tensor with width `projection_dim`
              (initialization argument). Each column corresponds to a flattened
              parameter in parameter-space. `R` has the property that
              :math`H^{-1} \approx RR'`.
docstr_tok: ["Returns","the","matrix","`","R","`","described","in","the","documentation","for","`","NaiveInfluenceFunction","`",".","In","short",",","`","R","`","has","the","property","that",":","math","`","H^","{","-1","}","\\approx","RR","'","`",",","where","`","H","`","is","the","Hessian",".","Since","this","is","a","``","naive","''","implementation",",","it","does","so","by","explicitly","forming","the","Hessian",",","converting","it","to","a","2D","tensor",",","and","computing","its","eigenvectors","\/","eigenvalues",",","before","filtering","out","some","eigenvalues","and","then","inverting","them",".","The","returned","matrix","`","R","`","represents","a","set","of","parameters","in","parameter","space",".","Since","the","Hessian","is","obtained","by","first","flattening","the","parameters",",","each","column","of","`","R","`","corresponds","to","a","*","flattened","*","parameter","in","parameter","space",".","Args",":","dataloader","(","DataLoader",")",":","The","returned","matrix","`","R","`","gives","a","low-rank","approximation","of","the","Hessian","`","H","`",".","This","dataloader","defines","the","dataset","used","to","compute","the","Hessian","that","is","being","approximated",".","projection_on_cpu","(","bool",",","optional",")",":","Whether","to","move","the","projection",",","i.e",".","low-rank","approximation","of","the","inverse","Hessian",",","to","cpu",",","to","save","gpu","memory",".","show_progress","(","bool",")",":","Computing","the","Hessian","that","is","being","approximated","requires","summing","up","the","Hessians","computed","using","different","batches",",","which","may","take","a","long","time",".","If","`","show_progress","`","is","true",",","the","number","of","batches","that","have","been","processed","will","be","displayed",".","It","will","try","to","use","tqdm","if","available","for","advanced","features","(","e.g",".","time","estimation",")",".","Otherwise",",","it","will","fallback","to","a","simple","output","of","progress",".","Returns",":","R","(","Tensor",")",":","Tall","and","skinny","tensor","with","width","`","projection_dim","`","(","initialization","argument",")",".","Each","column","corresponds","to","a","flattened","parameter","in","parameter-space",".","`","R","`","has","the","property","that",":","math","`","H^","{","-1","}","\\approx","RR","'","`","."]
code: def _retrieve_projections_naive_influence_function(
      self,
      dataloader: DataLoader,
      projection_on_cpu: bool,
      show_progress: bool,
  ) -> Tensor:
      r"""
      Returns the matrix `R` described in the documentation for
      `NaiveInfluenceFunction`. In short, `R` has the property that
      :math`H^{-1} \approx RR'`, where `H` is the Hessian. Since this is a "naive"
      implementation, it does so by explicitly forming the Hessian, converting
      it to a 2D tensor, and computing its eigenvectors / eigenvalues, before
      filtering out some eigenvalues and then inverting them. The returned matrix
      `R` represents a set of parameters in parameter space. Since the Hessian
      is obtained by first flattening the parameters, each column of `R` corresponds
      to a *flattened* parameter in parameter space.
  
      Args:
          dataloader (DataLoader): The returned matrix `R` gives a low-rank
                  approximation of the Hessian `H`. This dataloader defines the
                  dataset used to compute the Hessian that is being approximated.
          projection_on_cpu (bool, optional): Whether to move the projection,
                  i.e. low-rank approximation of the inverse Hessian, to cpu, to save
                  gpu memory.
          show_progress (bool): Computing the Hessian that is being approximated
                  requires summing up the Hessians computed using different batches,
                  which may take a long time. If `show_progress` is true, the number
                  of batches that have been processed will be displayed. It will try
                  to use tqdm if available for advanced features (e.g. time
                  estimation). Otherwise, it will fallback to a simple output of
                  progress.
  
      Returns:
          R (Tensor): Tall and skinny tensor with width `projection_dim`
                  (initialization argument). Each column corresponds to a flattened
                  parameter in parameter-space. `R` has the property that
                  :math`H^{-1} \approx RR'`.
      """
      # compute the hessian using the dataloader. hessian is always computed using
      # the training loss function. H is 2D, with each column / row corresponding to
      # a different parameter. we cannot directly use
      # `torch.autograd.functional.hessian`, because it does not return a 2D tensor.
      # instead, to compute H, we first create a function that accepts *flattened*
      # model parameters (i.e. a 1D tensor), and outputs the loss of `self.model`,
      # using those parameters, aggregated over `dataloader`. this function is then
      # passed to `torch.autograd.functional.hessian`. because its input is 1D, the
      # resulting hessian is 2D, as desired. all this functionality is handled by
      # `_compute_dataset_func`.
      H = _compute_dataset_func(
          dataloader,
          self.model,
          self.loss_fn,
          self.reduction_type,
          self.layer_modules,
          torch.autograd.functional.hessian,
          show_progress,
      )
  
      # H is approximately `vs @ torch.diag(ls) @ vs.T``, using eigendecomposition
      ls, vs = _top_eigen(
          H,
          self.projection_dim,
          self.hessian_reg,
          self.hessian_inverse_tol,
      )
  
      # if no positive eigenvalues exist, we cannot compute a low-rank
      # approximation of the square root of the hessian H, so raise exception
      if len(ls) == 0:
          raise Exception(
              "Hessian has no positive "
              "eigenvalues, so cannot take its square root."
          )
  
      # `R` is `vs @ torch.diag(ls ** -0.5)`, since H^{-1} is approximately
      #  `vs @ torch.diag(ls ** -1) @ vs.T`
      # see https://en.wikipedia.org/wiki/Eigendecomposition_of_a_matrix#Matrix_inverse_via_eigendecomposition # noqa: E501
      # for details, which mentions that discarding small eigenvalues (as done in
      # `_top_eigen`) reduces noisiness of the inverse.
      ls = (1.0 / ls) ** 0.5
      return (ls.unsqueeze(0) * vs).to(
          device=torch.device("cpu")
          if projection_on_cpu
          else self.model_device
      )
  
imports: import functools
  from abc import abstractmethod
  from operator import add
  from typing import Any, Callable, List, Optional, Tuple, Union
  import torch
  import torch.nn
  from captum._utils.gradient import _extract_parameters_from_layers
  from captum.influence._core.influence import DataInfluence
  from captum.influence._utils.common import _check_loss_fn, _compute_batch_loss_influence_function_base, _compute_jacobian_sample_wise_grads_per_batch, _dataset_fn, _flatten_params, _format_inputs_dataset, _functional_call, _get_k_most_influential_helper, _influence_batch_intermediate_quantities_influence_function, _influence_helper_intermediate_quantities_influence_function, _influence_route_to_helpers, _load_flexible_state_dict, _params_to_names, _progress_bar_constructor, _self_influence_helper_intermediate_quantities_influence_function, _set_active_parameters, _top_eigen, _unflatten_params_factory, KMostInfluentialResults
  from captum.log import log_usage
  from torch import Tensor
  from torch.nn import Module
  from torch.utils.data import DataLoader, Dataset
  from tqdm import tqdm
label_desc: Use image node_id 2 for calling the NaiveInfluenceFunction obj's underlying member method code with example usage: obj._retrieve_projections_naive_influence_function(dataloader, projection_on_cpu, show_progress) and returns: unknown