class_docstr: A version of TracInCPFast which is optimized for "interactive" calls to
  `influence` for the purpose of calculating proponents / opponents, or
  influence scores. "Interactive" means there will be multiple calls to
  `influence`, with each call for a different batch of test examples, and
  subsequent calls rely on the results of previous calls. The implementation in
  this class has been optimized so that each call to `influence` is fast, so that
  it can be used for interactive analysis. This class should only be used for
  interactive use cases. It should not be used if `influence` will only be
  called once, because to enable fast calls to `influence`, time and memory
  intensive preprocessing is required in `__init__`. Furthermore, it should not
  be used to calculate self influence scores - `TracInCPFast` should be used
  instead for that purpose. To enable interactive analysis, this implementation
  computes and saves "embedding" vectors for all training examples in
  `train_dataset`. Crucially, the influence score of a training
  example on a test example is simply the dot-product of their corresponding
  vectors, and proponents / opponents can be found by first storing vectors for
  training examples in a nearest-neighbor data structure, and then finding the
  nearest-neighbors for a test example in terms of dot-product (see appendix F
  of the TracIn paper). This class should only be used if calls to `influence`
  to obtain proponents / opponents or influence scores will be made in an
  "interactive" manner, and there is sufficient memory to store vectors for the
  entire `train_dataset`. This is because in order to enable interactive
  analysis, this implementation incures overhead in `__init__` to setup the
  nearest-neighbors data structure, which is both time and memory intensive, as
  vectors corresponding to all training examples needed to be stored. To reduce
  memory usage, this implementation enables random projections of those vectors.
  Note that the influence scores computed with random projections are less
  accurate, though correct in expectation.
  
  In more detail regarding the "embedding" vectors - the influence of a training
  example on a test example, when only considering gradients in the last
  fully-connected layer, 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'))`, using the notation in the
  description of `TracInCPFast`. As is, this is not a dot-product of 2 vectors.
  However, we can rewrite that contribution as
  :math`(x \nabla_y f(y)^T) \dot (x' f(y')^T)`. Both terms in this
  product are 2D matrices, as they are outer products, and the "product" is actually
  a dot-product, treating both matrices as vectors. Therefore, for a given
  checkpoint, its contribution to the "embedding" of an example is just the
  outer-product :math`(x \nabla_y f(y)^T)`, flattened. Furthemore, to reduce the
  dimension of this contribution, we can right-multiply and
  left-multiply the outer-product with two separate projection matrices. These
  transform :math`\nabla_y f(y)` and :math`x` to lower dimensional vectors. While
  the dimension of these two lower dimensional vectors do not necessarily need to
  be the same, in our implementation, we let them be the same, both equal to the
  square root of the desired projection dimension. Finally, the embedding of an
  example is the concatenation of the contributions from each checkpoint.
class_docstr_tok: ["A","version","of","TracInCPFast","which","is","optimized","for","``","interactive","''","calls","to","`","influence","`","for","the","purpose","of","calculating","proponents","\/","opponents",",","or","influence","scores",".","``","Interactive","''","means","there","will","be","multiple","calls","to","`","influence","`",",","with","each","call","for","a","different","batch","of","test","examples",",","and","subsequent","calls","rely","on","the","results","of","previous","calls",".","The","implementation","in","this","class","has","been","optimized","so","that","each","call","to","`","influence","`","is","fast",",","so","that","it","can","be","used","for","interactive","analysis",".","This","class","should","only","be","used","for","interactive","use","cases",".","It","should","not","be","used","if","`","influence","`","will","only","be","called","once",",","because","to","enable","fast","calls","to","`","influence","`",",","time","and","memory","intensive","preprocessing","is","required","in","`","__init__","`",".","Furthermore",",","it","should","not","be","used","to","calculate","self","influence","scores","-","`","TracInCPFast","`","should","be","used","instead","for","that","purpose",".","To","enable","interactive","analysis",",","this","implementation","computes","and","saves","``","embedding","''","vectors","for","all","training","examples","in","`","train_dataset","`",".","Crucially",",","the","influence","score","of","a","training","example","on","a","test","example","is","simply","the","dot-product","of","their","corresponding","vectors",",","and","proponents","\/","opponents","can","be","found","by","first","storing","vectors","for","training","examples","in","a","nearest-neighbor","data","structure",",","and","then","finding","the","nearest-neighbors","for","a","test","example","in","terms","of","dot-product","(","see","appendix","F","of","the","TracIn","paper",")",".","This","class","should","only","be","used","if","calls","to","`","influence","`","to","obtain","proponents","\/","opponents","or","influence","scores","will","be","made","in","an","''","interactive","''","manner",",","and","there","is","sufficient","memory","to","store","vectors","for","the","entire","`","train_dataset","`",".","This","is","because","in","order","to","enable","interactive","analysis",",","this","implementation","incures","overhead","in","`","__init__","`","to","setup","the","nearest-neighbors","data","structure",",","which","is","both","time","and","memory","intensive",",","as","vectors","corresponding","to","all","training","examples","needed","to","be","stored",".","To","reduce","memory","usage",",","this","implementation","enables","random","projections","of","those","vectors",".","Note","that","the","influence","scores","computed","with","random","projections","are","less","accurate",",","though","correct","in","expectation",".","In","more","detail","regarding","the","``","embedding","''","vectors","-","the","influence","of","a","training","example","on","a","test","example",",","when","only","considering","gradients","in","the","last","fully-connected","layer",",","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","'",")",")","`",",","using","the","notation","in","the","description","of","`","TracInCPFast","`",".","As","is",",","this","is","not","a","dot-product","of","2","vectors",".","However",",","we","can","rewrite","that","contribution","as",":","math","`","(","x","\\nabla_y","f","(","y",")","^T",")","\\dot","(","x","'","f","(","y","'",")","^T",")","`",".","Both","terms","in","this","product","are","2D","matrices",",","as","they","are","outer","products",",","and","the","``","product","''","is","actually","a","dot-product",",","treating","both","matrices","as","vectors",".","Therefore",",","for","a","given","checkpoint",",","its","contribution","to","the","``","embedding","''","of","an","example","is","just","the","outer-product",":","math","`","(","x","\\nabla_y","f","(","y",")","^T",")","`",",","flattened",".","Furthemore",",","to","reduce","the","dimension","of","this","contribution",",","we","can","right-multiply","and","left-multiply","the","outer-product","with","two","separate","projection","matrices",".","These","transform",":","math","`","\\nabla_y","f","(","y",")","`","and",":","math","`","x","`","to","lower","dimensional","vectors",".","While","the","dimension","of","these","two","lower","dimensional","vectors","do","not","necessarily","need","to","be","the","same",",","in","our","implementation",",","we","let","them","be","the","same",",","both","equal","to","the","square","root","of","the","desired","projection","dimension",".","Finally",",","the","embedding","of","an","example","is","the","concatenation","of","the","contributions","from","each","checkpoint","."]
docstr: Args:
  
      model (torch.nn.Module): An instance of pytorch model. This model should
              define all of its layers as attributes of the model.
      final_fc_layer (torch.nn.Module or str): The last fully connected layer in
              the network for which gradients will be approximated via fast random
              projection method. Can be either the layer module itself, or the
              fully qualified name of the layer if it is a defined attribute of
              the passed `model`.
      train_dataset (torch.utils.data.Dataset or torch.utils.data.DataLoader):
              In the `influence` method, we compute the influence score of
              training examples on examples in a test batch.
              This argument represents the training dataset containing those
              training examples. In order to compute those influence scores, we
              will create a Pytorch DataLoader yielding batches of training
              examples that is then used for processing. If this argument is
              already a Pytorch Dataloader, that DataLoader can be directly
              used for processing. If it is instead a Pytorch Dataset, we will
              create a DataLoader using it, with batch size specified by
              `batch_size`. For efficiency purposes, the batch size of the
              DataLoader used for processing should be as large as possible, but
              not too large, so that certain intermediate quantities created
              from a batch still fit in memory. Therefore, if
              `train_dataset` is a Dataset, `batch_size` should be large.
              If `train_dataset` was already a DataLoader to begin with,
              it should have been constructed to have a large batch size. It is
              assumed that the Dataloader (regardless of whether it is created
              from a Pytorch Dataset or not) yields tuples. For a `batch` that is
              yielded, of length `L`, it is assumed that the forward function of
              `model` accepts `L-1` arguments, and the last element of `batch` is
              the label. In other words, `model(*batch[:-1])` gives the output of
              `model`, and `batch[-1]` are the labels for the batch.
      checkpoints (str, list[str], or Iterator): Either the directory of the
              path to store and retrieve model checkpoints, a list of
              filepaths with checkpoints from which to load, or an iterator which
              returns objects from which to load checkpoints.
      checkpoints_load_func (Callable, optional): The function to load a saved
              checkpoint into a model to update its parameters, and get the
              learning rate if it is saved. By default uses a utility to load a
              model saved as a state dict.
              Default: _load_flexible_state_dict
      loss_fn (Callable, optional): The loss function applied to model. `loss_fn`
              must be a "reduction" loss function that reduces the per-example
              losses in a batch, and returns a single scalar Tensor. Furthermore,
              the reduction must be the *sum* of the per-example losses. For
              instance, `nn.BCELoss(reduction="sum")` is acceptable, but
              `nn.BCELoss(reduction="mean")` is *not* acceptable.
              Default: None
      batch_size (int or None, optional): Batch size of the DataLoader created to
              iterate through `train_dataset`, if it is a Dataset.
              `batch_size` should be chosen as large as possible so that certain
              intermediate quantities created from a batch still fit in memory.
              Specific implementations of `TracInCPBase` will detail the size of
              the intermediate quantities. `batch_size` must be an int if
              `train_dataset` is a Dataset. If `train_dataset`
              is a DataLoader, then `batch_size` is ignored as an argument.
              Default: 1
      test_loss_fn (Callable, optional): In some cases, one may want to use a
              separate loss functions for training examples, i.e. those in
              `train_dataset`, and for test examples, i.e. those
              represented by the `inputs` and `targets` arguments to the
              `influence` method. For example, if one wants to calculate the
              influence score of a training example on a test example's
              prediction for a fixed class, `test_loss_fn` could map from the
              logits for all classes to the logits for a fixed class.
              `test_loss_fn` needs satisfy the same constraints as `loss_fn`.
              Thus, the same checks that we apply to `loss_fn` are also applied
              to `test_loss_fn`, if the latter is provided. If not provided, the
              loss function for test examples is assumed to be the same as the
              loss function for training examples, i.e. `loss_fn`.
      vectorize (bool): Flag to use experimental vectorize functionality
              for `torch.autograd.functional.jacobian`.
              Default: False
      nearest_neighbors (NearestNeighbors, optional): The NearestNeighbors
              instance for finding nearest neighbors. If None, defaults to
              `AnnoyNearestNeighbors(n_trees=10)`.
              Default: None
      projection_dim (int, optional): Each example will be represented in
              the nearest neighbors data structure with a vector. This vector
              is the concatenation of several "checkpoint vectors", each of which
              is computed using a different checkpoint in the `checkpoints`
              argument. If `projection_dim` is an int, it represents the
              dimension we will project each "checkpoint vector" to, so that the
              vector for each example will be of dimension at most
              `projection_dim` * C, where C is the number of checkpoints.
              Regarding the dimension of each vector, D: Let I be the dimension
              of the output of the last fully-connected layer times the dimension
              of the input of the last fully-connected layer. If `projection_dim`
              is not `None`, then D = min(I * C, `projection_dim` * C).
              Otherwise, D = I * C. In summary, if `projection_dim` is None, the
              dimension of this vector will be determined by the size of the
              input and output of the last fully-connected layer of `model`, and
              the number of checkpoints. Otherwise, `projection_dim` must be an
              int, and random projection will be performed to ensure that the
              vector is of dimension no more than `projection_dim` * C.
              `projection_dim` corresponds to the variable d in the top of page
              15 of the TracIn paper: https://arxiv.org/abs/2002.08484.
              Default: None
      seed (int, optional): Because this implementation chooses a random
              projection, its output is random. Setting this seed specifies the
              random seed when choosing the random projection.
              Default: 0
docstr_tok: ["Args",":","model","(","torch.nn.Module",")",":","An","instance","of","pytorch","model",".","This","model","should","define","all","of","its","layers","as","attributes","of","the","model",".","final_fc_layer","(","torch.nn.Module","or","str",")",":","The","last","fully","connected","layer","in","the","network","for","which","gradients","will","be","approximated","via","fast","random","projection","method",".","Can","be","either","the","layer","module","itself",",","or","the","fully","qualified","name","of","the","layer","if","it","is","a","defined","attribute","of","the","passed","`","model","`",".","train_dataset","(","torch.utils.data.Dataset","or","torch.utils.data.DataLoader",")",":","In","the","`","influence","`","method",",","we","compute","the","influence","score","of","training","examples","on","examples","in","a","test","batch",".","This","argument","represents","the","training","dataset","containing","those","training","examples",".","In","order","to","compute","those","influence","scores",",","we","will","create","a","Pytorch","DataLoader","yielding","batches","of","training","examples","that","is","then","used","for","processing",".","If","this","argument","is","already","a","Pytorch","Dataloader",",","that","DataLoader","can","be","directly","used","for","processing",".","If","it","is","instead","a","Pytorch","Dataset",",","we","will","create","a","DataLoader","using","it",",","with","batch","size","specified","by","`","batch_size","`",".","For","efficiency","purposes",",","the","batch","size","of","the","DataLoader","used","for","processing","should","be","as","large","as","possible",",","but","not","too","large",",","so","that","certain","intermediate","quantities","created","from","a","batch","still","fit","in","memory",".","Therefore",",","if","`","train_dataset","`","is","a","Dataset",",","`","batch_size","`","should","be","large",".","If","`","train_dataset","`","was","already","a","DataLoader","to","begin","with",",","it","should","have","been","constructed","to","have","a","large","batch","size",".","It","is","assumed","that","the","Dataloader","(","regardless","of","whether","it","is","created","from","a","Pytorch","Dataset","or","not",")","yields","tuples",".","For","a","`","batch","`","that","is","yielded",",","of","length","`","L","`",",","it","is","assumed","that","the","forward","function","of","`","model","`","accepts","`","L-1","`","arguments",",","and","the","last","element","of","`","batch","`","is","the","label",".","In","other","words",",","`","model","(","*","batch","[",":","-1","]",")","`","gives","the","output","of","`","model","`",",","and","`","batch","[","-1","]","`","are","the","labels","for","the","batch",".","checkpoints","(","str",",","list","[","str","]",",","or","Iterator",")",":","Either","the","directory","of","the","path","to","store","and","retrieve","model","checkpoints",",","a","list","of","filepaths","with","checkpoints","from","which","to","load",",","or","an","iterator","which","returns","objects","from","which","to","load","checkpoints",".","checkpoints_load_func","(","Callable",",","optional",")",":","The","function","to","load","a","saved","checkpoint","into","a","model","to","update","its","parameters",",","and","get","the","learning","rate","if","it","is","saved",".","By","default","uses","a","utility","to","load","a","model","saved","as","a","state","dict",".","Default",":","_load_flexible_state_dict","loss_fn","(","Callable",",","optional",")",":","The","loss","function","applied","to","model",".","`","loss_fn","`","must","be","a","``","reduction","''","loss","function","that","reduces","the","per-example","losses","in","a","batch",",","and","returns","a","single","scalar","Tensor",".","Furthermore",",","the","reduction","must","be","the","*","sum","*","of","the","per-example","losses",".","For","instance",",","`","nn.BCELoss","(","reduction=","''","sum","''",")","`","is","acceptable",",","but","`","nn.BCELoss","(","reduction=","''","mean","''",")","`","is","*","not","*","acceptable",".","Default",":","None","batch_size","(","int","or","None",",","optional",")",":","Batch","size","of","the","DataLoader","created","to","iterate","through","`","train_dataset","`",",","if","it","is","a","Dataset",".","`","batch_size","`","should","be","chosen","as","large","as","possible","so","that","certain","intermediate","quantities","created","from","a","batch","still","fit","in","memory",".","Specific","implementations","of","`","TracInCPBase","`","will","detail","the","size","of","the","intermediate","quantities",".","`","batch_size","`","must","be","an","int","if","`","train_dataset","`","is","a","Dataset",".","If","`","train_dataset","`","is","a","DataLoader",",","then","`","batch_size","`","is","ignored","as","an","argument",".","Default",":","1","test_loss_fn","(","Callable",",","optional",")",":","In","some","cases",",","one","may","want","to","use","a","separate","loss","functions","for","training","examples",",","i.e",".","those","in","`","train_dataset","`",",","and","for","test","examples",",","i.e",".","those","represented","by","the","`","inputs","`","and","`","targets","`","arguments","to","the","`","influence","`","method",".","For","example",",","if","one","wants","to","calculate","the","influence","score","of","a","training","example","on","a","test","example's","prediction","for","a","fixed","class",",","`","test_loss_fn","`","could","map","from","the","logits","for","all","classes","to","the","logits","for","a","fixed","class",".","`","test_loss_fn","`","needs","satisfy","the","same","constraints","as","`","loss_fn","`",".","Thus",",","the","same","checks","that","we","apply","to","`","loss_fn","`","are","also","applied","to","`","test_loss_fn","`",",","if","the","latter","is","provided",".","If","not","provided",",","the","loss","function","for","test","examples","is","assumed","to","be","the","same","as","the","loss","function","for","training","examples",",","i.e",".","`","loss_fn","`",".","vectorize","(","bool",")",":","Flag","to","use","experimental","vectorize","functionality","for","`","torch.autograd.functional.jacobian","`",".","Default",":","False","nearest_neighbors","(","NearestNeighbors",",","optional",")",":","The","NearestNeighbors","instance","for","finding","nearest","neighbors",".","If","None",",","defaults","to","`","AnnoyNearestNeighbors","(","n_trees=10",")","`",".","Default",":","None","projection_dim","(","int",",","optional",")",":","Each","example","will","be","represented","in","the","nearest","neighbors","data","structure","with","a","vector",".","This","vector","is","the","concatenation","of","several","``","checkpoint","vectors","''",",","each","of","which","is","computed","using","a","different","checkpoint","in","the","`","checkpoints","`","argument",".","If","`","projection_dim","`","is","an","int",",","it","represents","the","dimension","we","will","project","each","``","checkpoint","vector","''","to",",","so","that","the","vector","for","each","example","will","be","of","dimension","at","most","`","projection_dim","`","*","C",",","where","C","is","the","number","of","checkpoints",".","Regarding","the","dimension","of","each","vector",",","D",":","Let","I","be","the","dimension","of","the","output","of","the","last","fully-connected","layer","times","the","dimension","of","the","input","of","the","last","fully-connected","layer",".","If","`","projection_dim","`","is","not","`","None","`",",","then","D","=","min","(","I","*","C",",","`","projection_dim","`","*","C",")",".","Otherwise",",","D","=","I","*","C.","In","summary",",","if","`","projection_dim","`","is","None",",","the","dimension","of","this","vector","will","be","determined","by","the","size","of","the","input","and","output","of","the","last","fully-connected","layer","of","`","model","`",",","and","the","number","of","checkpoints",".","Otherwise",",","`","projection_dim","`","must","be","an","int",",","and","random","projection","will","be","performed","to","ensure","that","the","vector","is","of","dimension","no","more","than","`","projection_dim","`","*","C.","`","projection_dim","`","corresponds","to","the","variable","d","in","the","top","of","page","15","of","the","TracIn","paper",":","https",":","\/\/arxiv.org\/abs\/2002.08484",".","Default",":","None","seed","(","int",",","optional",")",":","Because","this","implementation","chooses","a","random","projection",",","its","output","is","random",".","Setting","this","seed","specifies","the","random","seed","when","choosing","the","random","projection",".","Default",":","0"]
code: def __init__(
      self,
      model: Module,
      final_fc_layer: Union[Module, str],
      train_dataset: Union[Dataset, DataLoader],
      checkpoints: Union[str, List[str], Iterator],
      checkpoints_load_func: Callable = _load_flexible_state_dict,
      loss_fn: Optional[Union[Module, Callable]] = None,
      batch_size: Union[int, None] = 1,
      test_loss_fn: Optional[Union[Module, Callable]] = None,
      vectorize: bool = False,
      nearest_neighbors: Optional[NearestNeighbors] = None,
      projection_dim: int = None,
      seed: int = 0,
  ) -> None:
      r"""
      Args:
  
          model (torch.nn.Module): An instance of pytorch model. This model should
                  define all of its layers as attributes of the model.
          final_fc_layer (torch.nn.Module or str): The last fully connected layer in
                  the network for which gradients will be approximated via fast random
                  projection method. Can be either the layer module itself, or the
                  fully qualified name of the layer if it is a defined attribute of
                  the passed `model`.
          train_dataset (torch.utils.data.Dataset or torch.utils.data.DataLoader):
                  In the `influence` method, we compute the influence score of
                  training examples on examples in a test batch.
                  This argument represents the training dataset containing those
                  training examples. In order to compute those influence scores, we
                  will create a Pytorch DataLoader yielding batches of training
                  examples that is then used for processing. If this argument is
                  already a Pytorch Dataloader, that DataLoader can be directly
                  used for processing. If it is instead a Pytorch Dataset, we will
                  create a DataLoader using it, with batch size specified by
                  `batch_size`. For efficiency purposes, the batch size of the
                  DataLoader used for processing should be as large as possible, but
                  not too large, so that certain intermediate quantities created
                  from a batch still fit in memory. Therefore, if
                  `train_dataset` is a Dataset, `batch_size` should be large.
                  If `train_dataset` was already a DataLoader to begin with,
                  it should have been constructed to have a large batch size. It is
                  assumed that the Dataloader (regardless of whether it is created
                  from a Pytorch Dataset or not) yields tuples. For a `batch` that is
                  yielded, of length `L`, it is assumed that the forward function of
                  `model` accepts `L-1` arguments, and the last element of `batch` is
                  the label. In other words, `model(*batch[:-1])` gives the output of
                  `model`, and `batch[-1]` are the labels for the batch.
          checkpoints (str, list[str], or Iterator): Either the directory of the
                  path to store and retrieve model checkpoints, a list of
                  filepaths with checkpoints from which to load, or an iterator which
                  returns objects from which to load checkpoints.
          checkpoints_load_func (Callable, optional): The function to load a saved
                  checkpoint into a model to update its parameters, and get the
                  learning rate if it is saved. By default uses a utility to load a
                  model saved as a state dict.
                  Default: _load_flexible_state_dict
          loss_fn (Callable, optional): The loss function applied to model. `loss_fn`
                  must be a "reduction" loss function that reduces the per-example
                  losses in a batch, and returns a single scalar Tensor. Furthermore,
                  the reduction must be the *sum* of the per-example losses. For
                  instance, `nn.BCELoss(reduction="sum")` is acceptable, but
                  `nn.BCELoss(reduction="mean")` is *not* acceptable.
                  Default: None
          batch_size (int or None, optional): Batch size of the DataLoader created to
                  iterate through `train_dataset`, if it is a Dataset.
                  `batch_size` should be chosen as large as possible so that certain
                  intermediate quantities created from a batch still fit in memory.
                  Specific implementations of `TracInCPBase` will detail the size of
                  the intermediate quantities. `batch_size` must be an int if
                  `train_dataset` is a Dataset. If `train_dataset`
                  is a DataLoader, then `batch_size` is ignored as an argument.
                  Default: 1
          test_loss_fn (Callable, optional): In some cases, one may want to use a
                  separate loss functions for training examples, i.e. those in
                  `train_dataset`, and for test examples, i.e. those
                  represented by the `inputs` and `targets` arguments to the
                  `influence` method. For example, if one wants to calculate the
                  influence score of a training example on a test example's
                  prediction for a fixed class, `test_loss_fn` could map from the
                  logits for all classes to the logits for a fixed class.
                  `test_loss_fn` needs satisfy the same constraints as `loss_fn`.
                  Thus, the same checks that we apply to `loss_fn` are also applied
                  to `test_loss_fn`, if the latter is provided. If not provided, the
                  loss function for test examples is assumed to be the same as the
                  loss function for training examples, i.e. `loss_fn`.
          vectorize (bool): Flag to use experimental vectorize functionality
                  for `torch.autograd.functional.jacobian`.
                  Default: False
          nearest_neighbors (NearestNeighbors, optional): The NearestNeighbors
                  instance for finding nearest neighbors. If None, defaults to
                  `AnnoyNearestNeighbors(n_trees=10)`.
                  Default: None
          projection_dim (int, optional): Each example will be represented in
                  the nearest neighbors data structure with a vector. This vector
                  is the concatenation of several "checkpoint vectors", each of which
                  is computed using a different checkpoint in the `checkpoints`
                  argument. If `projection_dim` is an int, it represents the
                  dimension we will project each "checkpoint vector" to, so that the
                  vector for each example will be of dimension at most
                  `projection_dim` * C, where C is the number of checkpoints.
                  Regarding the dimension of each vector, D: Let I be the dimension
                  of the output of the last fully-connected layer times the dimension
                  of the input of the last fully-connected layer. If `projection_dim`
                  is not `None`, then D = min(I * C, `projection_dim` * C).
                  Otherwise, D = I * C. In summary, if `projection_dim` is None, the
                  dimension of this vector will be determined by the size of the
                  input and output of the last fully-connected layer of `model`, and
                  the number of checkpoints. Otherwise, `projection_dim` must be an
                  int, and random projection will be performed to ensure that the
                  vector is of dimension no more than `projection_dim` * C.
                  `projection_dim` corresponds to the variable d in the top of page
                  15 of the TracIn paper: https://arxiv.org/abs/2002.08484.
                  Default: None
          seed (int, optional): Because this implementation chooses a random
                  projection, its output is random. Setting this seed specifies the
                  random seed when choosing the random projection.
                  Default: 0
      """
  
      TracInCPFast.__init__(
          self,
          model,
          final_fc_layer,
          train_dataset,
          checkpoints,
          checkpoints_load_func,
          loss_fn,
          batch_size,
          test_loss_fn,
          vectorize,
      )
  
      warnings.warn(
          (
              "WARNING: Using this implementation stores quantities related to the "
              "entire `train_dataset` in memory, and may results in running "
              "out of memory. If this happens, consider using %s instead, for which "
              "each call to `influence` to compute influence scores or proponents "
              "will be slower, but may avoid running out of memory."
          )
          % "`TracInCPFast`"
      )
  
      self.nearest_neighbors = (
          AnnoyNearestNeighbors()
          if nearest_neighbors is None
          else nearest_neighbors
      )
  
      self.projection_dim = projection_dim
  
      torch.manual_seed(seed)  # for reproducibility
      self.projection_quantities = (
          self._set_projections_tracincp_fast_rand_proj(
              self.train_dataloader,
          )
      )
  
      self.src_intermediate_quantities = (
          self._get_intermediate_quantities_tracincp_fast_rand_proj(
              self.train_dataloader,
              self.projection_quantities,
          )
      )
  
      self._process_src_intermediate_quantities_tracincp_fast_rand_proj(
          self.src_intermediate_quantities,
      )
  
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 1 to create a new TracInCPFastRandProj object from inherited base classes: TracInCPFast with example: obj = TracInCPFastRandProj(model, final_fc_layer, train_dataset, checkpoints, checkpoints_load_func, loss_fn, batch_size, test_loss_fn, vectorize, nearest_neighbors, projection_dim, seed)