class_docstr: To implement the `influence` method, classes inheriting from `TracInCPBase` will
  separately implement the private `_self_influence`, `_get_k_most_influential`,
  and `_influence` methods. The public `influence` method is a wrapper for these
  private methods.
docstr: Args:
  
      model (torch.nn.Module): An instance of pytorch model. This model should
              define all of its layers as attributes of the 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.
              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 to satisfy the same constraints as `loss_fn`.
              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`.
              Default: None
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",".","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",".","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","to","satisfy","the","same","constraints","as","`","loss_fn","`",".","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","`",".","Default",":","None"]
code: def __init__(
      self,
      model: Module,
      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,
  ) -> 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.
          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.
                  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 to satisfy the same constraints as `loss_fn`.
                  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`.
                  Default: None
      """
  
      self.model = model
  
      if isinstance(checkpoints, str):
          self.checkpoints = AV.sort_files(
              glob.glob(join(checkpoints, "*"))
          )
      elif isinstance(checkpoints, List) and isinstance(
          checkpoints[0], str
      ):
          self.checkpoints = AV.sort_files(checkpoints)
      else:
          self.checkpoints = list(
              checkpoints
          )  # cast to avoid mypy error
      if isinstance(self.checkpoints, List):
          assert len(self.checkpoints) > 0, "No checkpoints saved!"
  
      self.checkpoints_load_func = checkpoints_load_func
      self.loss_fn = loss_fn
      # If test_loss_fn not provided, it's assumed to be same as loss_fn
      self.test_loss_fn = (
          loss_fn if test_loss_fn is None else test_loss_fn
      )
      self.batch_size = batch_size
  
      if not isinstance(train_dataset, DataLoader):
          assert isinstance(batch_size, int), (
              "since the `train_dataset` argument was a `Dataset`, "
              "`batch_size` must be an int."
          )
          self.train_dataloader = DataLoader(
              train_dataset, batch_size, shuffle=False
          )
      else:
          self.train_dataloader = train_dataset
  
      self.train_dataloader_len: Optional[int] = None
      try:
          # since we will calculate the number of batches in
          # `self.train_dataloader` whenever we use progress bar, calculate
          # it once in initialization, for re-use.
          self.train_dataloader_len = len(self.train_dataloader)
      except TypeError:
          warnings.warn(
              "Unable to determine the number of batches in training dataset "
              "`train_dataset`. Therefore, if showing the progress of computations, "
              "only the number of batches processed can be displayed, and not the "
              "percentage completion of the computation, nor any time estimates."
          )
  
imports: import glob
  import warnings
  from abc import abstractmethod
  from os.path import join
  from typing import Any, Callable, Iterator, List, Optional, Tuple, Type, Union
  import torch
  from captum._utils.av import AV
  from captum._utils.common import _parse_version
  from captum._utils.progress import NullProgress, progress
  from captum.influence._core.influence import DataInfluence
  from captum.influence._utils.common import _check_loss_fn, _compute_jacobian_sample_wise_grads_per_batch, _format_inputs_dataset, _get_k_most_influential_helper, _gradient_dot_product, _influence_route_to_helpers, _load_flexible_state_dict, _self_influence_by_batches_helper, _set_active_parameters, KMostInfluentialResults
  from captum.log import log_usage
  from torch import Tensor
  from torch.nn import Module
  from torch.utils.data import DataLoader, Dataset
label_desc: Use image node_id 1 to create a new TracInCPBase object from inherited base classes: DataInfluence with example: obj = TracInCPBase(model, train_dataset, checkpoints, checkpoints_load_func, loss_fn, batch_size, test_loss_fn)