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 influence scores for a single training batch, when only considering
  gradients in the last fully-connected layer, using the computation trick
  described in the `TracInCPFast` class description.
docstr_tok: ["computes","influence","scores","for","a","single","training","batch",",","when","only","considering","gradients","in","the","last","fully-connected","layer",",","using","the","computation","trick","described","in","the","`","TracInCPFast","`","class","description","."]
code: def _influence_batch_tracincp_fast(
      self,
      test_batch: Tuple[Any, ...],
      train_batch: Tuple[Any, ...],
  ):
      """
      computes influence scores for a single training batch, when only considering
      gradients in the last fully-connected layer, using the computation trick
      described in the `TracInCPFast` class description.
      """
  
      def get_checkpoint_contribution(checkpoint):
          assert (
              checkpoint is not None
          ), "None returned from `checkpoints`, cannot load."
  
          learning_rate = self.checkpoints_load_func(
              self.model, checkpoint
          )
  
          (
              input_jacobians,
              input_layer_inputs,
          ) = _basic_computation_tracincp_fast(
              self,
              test_batch[0:-1],
              test_batch[-1],
              self.test_loss_fn,
              self.test_reduction_type,
          )
  
          (
              src_jacobian,
              src_layer_input,
          ) = _basic_computation_tracincp_fast(
              self,
              train_batch[0:-1],
              train_batch[-1],
              self.loss_fn,
              self.reduction_type,
          )
          return (
              _tensor_batch_dot(
                  input_jacobians, src_jacobian
              )  # shape is (test batch size, training batch size), containing x^T x'
              # for every example x in the training batch and example x' in the test
              # batch
              * _tensor_batch_dot(input_layer_inputs, src_layer_input)
              # shape is (test batch size, training batch size), containing
              # (\nabla_y f(y)^T \nabla_{y'} f(y')) for every label y in the training
              # batch and label y' in the test batch
              * learning_rate
          )
  
      batch_tracin_scores = get_checkpoint_contribution(
          self.checkpoints[0]
      )
  
      for checkpoint in self.checkpoints[1:]:
          batch_tracin_scores += get_checkpoint_contribution(checkpoint)
  
      return batch_tracin_scores
  
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 3 for calling the TracInCPFast obj's underlying member method code with example usage: obj._influence_batch_tracincp_fast(test_batch, train_batch) and returns: batch_tracin_scores, unknown