class_docstr: In Appendix F, Page 14 of the TracIn paper, they show that the calculation
  of the influence score of between a test example x' and a training example x,
  can be computed much more quickly than naive back-propagation in the special
  case when considering only gradients in the last fully-connected layer. This class
  computes influence scores for that special case. Note that the computed
  influence scores are exactly the same as when naive back-propagation is used -
  there is no loss in accuracy.
  
  In more detail regarding the influence score computation: let :math`x`
  and :math`\nabla_y f(y)` be the input and output-gradient of the last
  fully-connected layer, respectively, for a training example. Similarly, let
  :math`x'` and :math`\nabla_{y'} f(y')` be the corresponding quantities for
  a test example. Then, the influence score of the training example on the test
  example is the sum of the contribution from each checkpoint. The contribution from
  a given checkpoint is :math`(x^T x')(\nabla_y f(y)^T \nabla_{y'} f(y'))`.
class_docstr_tok: ["In","Appendix","F",",","Page","14","of","the","TracIn","paper",",","they","show","that","the","calculation","of","the","influence","score","of","between","a","test","example","x","'","and","a","training","example","x",",","can","be","computed","much","more","quickly","than","naive","back-propagation","in","the","special","case","when","considering","only","gradients","in","the","last","fully-connected","layer",".","This","class","computes","influence","scores","for","that","special","case",".","Note","that","the","computed","influence","scores","are","exactly","the","same","as","when","naive","back-propagation","is","used","-","there","is","no","loss","in","accuracy",".","In","more","detail","regarding","the","influence","score","computation",":","let",":","math","`","x","`","and",":","math","`","\\nabla_y","f","(","y",")","`","be","the","input","and","output-gradient","of","the","last","fully-connected","layer",",","respectively",",","for","a","training","example",".","Similarly",",","let",":","math","`","x","'","`","and",":","math","`","\\nabla_","{","y","'","}","f","(","y","'",")","`","be","the","corresponding","quantities","for","a","test","example",".","Then",",","the","influence","score","of","the","training","example","on","the","test","example","is","the","sum","of","the","contribution","from","each","checkpoint",".","The","contribution","from","a","given","checkpoint","is",":","math","`","(","x^T","x","'",")","(","\\nabla_y","f","(","y",")","^T","\\nabla_","{","y","'","}","f","(","y","'",")",")","`","."]
docstr: Computes self influence scores for the examples in `inputs`, which is
  either a single batch or a Pytorch `DataLoader` that yields batches.
  If `inputs` is not specified or `None` calculates self influence
  score for the training dataset `train_dataset`. Note that if `inputs`
  is a single batch, this will call `model` on that single batch,
  and if `inputs` yields batches, this will call `model`
  on each batch that is yielded. Therefore, please ensure that for both cases,
  the batch(es) that `model` is called with are not too large, so that
  there will not be an out-of-memory error.
  Internally, this computation requires iterating both over the batches in
  `inputs`, as well as different model checkpoints. There are two ways
  this iteration can be done. If `outer_loop_by_checkpoints` is False, the outer
  iteration will be over batches, and the inner iteration will be over
  checkpoints. This has the pro that displaying the progress of the computation
  is more intuitive, involving displaying the number of batches for which self
  influence scores have been computed. If `outer_loop_by_checkpoints` is True,
  the outer iteration will be over checkpoints, and the inner iteration will be
  over batches. This has the pro that the checkpoints do not need to be loaded
  for each batch. For large models, loading checkpoints can be time-intensive.
  
  Args:
      inputs (tuple or DataLoader, optional): This specifies the
              dataset for which self influence scores will be computed.
              Either a single tuple of any, or a `DataLoader`, where each
              batch yielded is a tuple of type any. In either case, the tuple
              represents a single batch, where the last element is assumed to
              be the labels for the batch. That is, `model(*batch[0:-1])`
              produces the output for `model`, and `batch[-1]` are the labels,
              if any. This is the same assumption made for each batch yielded
              by training dataset `train_dataset`. Please see documentation for
              the `train_dataset` argument to `TracInCP.__init__` for
              more details on the assumed structure of a batch. If not provided
              or `None`, self influence scores will be computed for training
              dataset `train_dataset`, which yields batches satisfying the
              above assumptions.
              Default: None.
      show_progress (bool, optional): Computation of self influence scores can
              take a long time if `inputs` represents many examples. If
              `show_progress`is true, the progress of this computation will be
              displayed. In more detail, if `outer_loop_by_checkpoints` is False,
              this computation will iterate over all batches in an outer loop.
              Thus if `show_progress` is True, the number of batches for which
              self influence scores have been computed will be displayed. If
              `outer_loop_by_checkpoints` is True, this computation will iterate
              over all checkpoints (provided as the `checkpoints` initialization
              argument) in an outer loop, and iterate over all batches that
              `inputs` represents in an inner loop. Thus if
              `show_progress` is True, the progress of both the outer
              iteration and the inner iterations will be displayed. To show
              progress, 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.
              Default: False
      outer_loop_by_checkpoints (bool, optional): If performing an outer
              iteration over checkpoints; see method description for more
              details.
              Default: False
docstr_tok: ["Computes","self","influence","scores","for","the","examples","in","`","inputs","`",",","which","is","either","a","single","batch","or","a","Pytorch","`","DataLoader","`","that","yields","batches",".","If","`","inputs","`","is","not","specified","or","`","None","`","calculates","self","influence","score","for","the","training","dataset","`","train_dataset","`",".","Note","that","if","`","inputs","`","is","a","single","batch",",","this","will","call","`","model","`","on","that","single","batch",",","and","if","`","inputs","`","yields","batches",",","this","will","call","`","model","`","on","each","batch","that","is","yielded",".","Therefore",",","please","ensure","that","for","both","cases",",","the","batch","(","es",")","that","`","model","`","is","called","with","are","not","too","large",",","so","that","there","will","not","be","an","out-of-memory","error",".","Internally",",","this","computation","requires","iterating","both","over","the","batches","in","`","inputs","`",",","as","well","as","different","model","checkpoints",".","There","are","two","ways","this","iteration","can","be","done",".","If","`","outer_loop_by_checkpoints","`","is","False",",","the","outer","iteration","will","be","over","batches",",","and","the","inner","iteration","will","be","over","checkpoints",".","This","has","the","pro","that","displaying","the","progress","of","the","computation","is","more","intuitive",",","involving","displaying","the","number","of","batches","for","which","self","influence","scores","have","been","computed",".","If","`","outer_loop_by_checkpoints","`","is","True",",","the","outer","iteration","will","be","over","checkpoints",",","and","the","inner","iteration","will","be","over","batches",".","This","has","the","pro","that","the","checkpoints","do","not","need","to","be","loaded","for","each","batch",".","For","large","models",",","loading","checkpoints","can","be","time-intensive",".","Args",":","inputs","(","tuple","or","DataLoader",",","optional",")",":","This","specifies","the","dataset","for","which","self","influence","scores","will","be","computed",".","Either","a","single","tuple","of","any",",","or","a","`","DataLoader","`",",","where","each","batch","yielded","is","a","tuple","of","type","any",".","In","either","case",",","the","tuple","represents","a","single","batch",",","where","the","last","element","is","assumed","to","be","the","labels","for","the","batch",".","That","is",",","`","model","(","*","batch","[","0",":","-1","]",")","`","produces","the","output","for","`","model","`",",","and","`","batch","[","-1","]","`","are","the","labels",",","if","any",".","This","is","the","same","assumption","made","for","each","batch","yielded","by","training","dataset","`","train_dataset","`",".","Please","see","documentation","for","the","`","train_dataset","`","argument","to","`","TracInCP.__init__","`","for","more","details","on","the","assumed","structure","of","a","batch",".","If","not","provided","or","`","None","`",",","self","influence","scores","will","be","computed","for","training","dataset","`","train_dataset","`",",","which","yields","batches","satisfying","the","above","assumptions",".","Default",":","None",".","show_progress","(","bool",",","optional",")",":","Computation","of","self","influence","scores","can","take","a","long","time","if","`","inputs","`","represents","many","examples",".","If","`","show_progress","`","is","true",",","the","progress","of","this","computation","will","be","displayed",".","In","more","detail",",","if","`","outer_loop_by_checkpoints","`","is","False",",","this","computation","will","iterate","over","all","batches","in","an","outer","loop",".","Thus","if","`","show_progress","`","is","True",",","the","number","of","batches","for","which","self","influence","scores","have","been","computed","will","be","displayed",".","If","`","outer_loop_by_checkpoints","`","is","True",",","this","computation","will","iterate","over","all","checkpoints","(","provided","as","the","`","checkpoints","`","initialization","argument",")","in","an","outer","loop",",","and","iterate","over","all","batches","that","`","inputs","`","represents","in","an","inner","loop",".","Thus","if","`","show_progress","`","is","True",",","the","progress","of","both","the","outer","iteration","and","the","inner","iterations","will","be","displayed",".","To","show","progress",",","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",".","Default",":","False","outer_loop_by_checkpoints","(","bool",",","optional",")",":","If","performing","an","outer","iteration","over","checkpoints",";","see","method","description","for","more","details",".","Default",":","False"]
code: def self_influence(
      self,
      inputs: Optional[Union[Tuple[Any, ...], DataLoader]] = None,
      show_progress: bool = False,
      outer_loop_by_checkpoints: bool = False,
  ) -> Tensor:
      """
      Computes self influence scores for the examples in `inputs`, which is
      either a single batch or a Pytorch `DataLoader` that yields batches.
      If `inputs` is not specified or `None` calculates self influence
      score for the training dataset `train_dataset`. Note that if `inputs`
      is a single batch, this will call `model` on that single batch,
      and if `inputs` yields batches, this will call `model`
      on each batch that is yielded. Therefore, please ensure that for both cases,
      the batch(es) that `model` is called with are not too large, so that
      there will not be an out-of-memory error.
      Internally, this computation requires iterating both over the batches in
      `inputs`, as well as different model checkpoints. There are two ways
      this iteration can be done. If `outer_loop_by_checkpoints` is False, the outer
      iteration will be over batches, and the inner iteration will be over
      checkpoints. This has the pro that displaying the progress of the computation
      is more intuitive, involving displaying the number of batches for which self
      influence scores have been computed. If `outer_loop_by_checkpoints` is True,
      the outer iteration will be over checkpoints, and the inner iteration will be
      over batches. This has the pro that the checkpoints do not need to be loaded
      for each batch. For large models, loading checkpoints can be time-intensive.
  
      Args:
          inputs (tuple or DataLoader, optional): This specifies the
                  dataset for which self influence scores will be computed.
                  Either a single tuple of any, or a `DataLoader`, where each
                  batch yielded is a tuple of type any. In either case, the tuple
                  represents a single batch, where the last element is assumed to
                  be the labels for the batch. That is, `model(*batch[0:-1])`
                  produces the output for `model`, and `batch[-1]` are the labels,
                  if any. This is the same assumption made for each batch yielded
                  by training dataset `train_dataset`. Please see documentation for
                  the `train_dataset` argument to `TracInCP.__init__` for
                  more details on the assumed structure of a batch. If not provided
                  or `None`, self influence scores will be computed for training
                  dataset `train_dataset`, which yields batches satisfying the
                  above assumptions.
                  Default: None.
          show_progress (bool, optional): Computation of self influence scores can
                  take a long time if `inputs` represents many examples. If
                  `show_progress`is true, the progress of this computation will be
                  displayed. In more detail, if `outer_loop_by_checkpoints` is False,
                  this computation will iterate over all batches in an outer loop.
                  Thus if `show_progress` is True, the number of batches for which
                  self influence scores have been computed will be displayed. If
                  `outer_loop_by_checkpoints` is True, this computation will iterate
                  over all checkpoints (provided as the `checkpoints` initialization
                  argument) in an outer loop, and iterate over all batches that
                  `inputs` represents in an inner loop. Thus if
                  `show_progress` is True, the progress of both the outer
                  iteration and the inner iterations will be displayed. To show
                  progress, 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.
                  Default: False
          outer_loop_by_checkpoints (bool, optional): If performing an outer
                  iteration over checkpoints; see method description for more
                  details.
                  Default: False
      """
      inputs = inputs if inputs is not None else self.train_dataloader
      if outer_loop_by_checkpoints:
          return self._self_influence_by_checkpoints(
              inputs, show_progress
          )
      return _self_influence_by_batches_helper(
          self._self_influence_by_checkpoints,
          self.get_name(),
          inputs,
          show_progress,
      )
  
imports: import threading
  import warnings
  from collections import defaultdict
  from typing import Any, Callable, cast, Dict, Iterator, List, Optional, Tuple, Union
  import torch
  from captum._utils.common import _get_module_from_name, _sort_key_list
  from captum._utils.gradient import _gather_distributed_tensors
  from captum._utils.progress import NullProgress, progress
  from captum.influence._core.tracincp import _influence_route_to_helpers, KMostInfluentialResults, TracInCPBase
  from captum.influence._utils.common import _check_loss_fn, _format_inputs_dataset, _get_k_most_influential_helper, _jacobian_loss_wrt_inputs, _load_flexible_state_dict, _self_influence_by_batches_helper, _tensor_batch_dot
  from captum.influence._utils.nearest_neighbors import AnnoyNearestNeighbors, NearestNeighbors
  from captum.log import log_usage
  from torch import device, Tensor
  from torch.nn import Module
  from torch.utils.data import DataLoader, Dataset
label_desc: Use image node_id 7 for calling the TracInCPFast obj's underlying member method code with example usage: obj.self_influence(inputs, show_progress, outer_loop_by_checkpoints) and returns: _self_influence_by_batches_helper, self