docstr: Args:
      module (torch.nn.Module): An instance of pytorch model. This model should
              define all of its layers as attributes of the model.
      layers (str or list[str]): The fully qualified layer(s) for which the
              activation vectors are computed.
      influence_src_dataset (torch.utils.data.Dataset): PyTorch Dataset that is
              used to create a PyTorch Dataloader to iterate over the dataset and
              its labels. This is the dataset for which we will be seeking for
              influential instances. In most cases this is the training dataset.
      activation_dir (str): The directory of the path to store
              and retrieve activation computations. Best practice would be to use
              an absolute path.
      model_id (str): The name/version of the model for which layer
              activations are being computed. Activations will be stored and
              loaded under the subdirectory with this name if provided.
      similarity_metric (Callable): This is a callable function that computes a
              similarity metric between two representations. For example, the
              representations pair could be from the training and test sets.
  
              This function must adhere to certain standards. The inputs should be
              torch Tensors with shape (batch_size_i/j, feature dimensions). The
              output Tensor should have shape (batch_size_i, batch_size_j) with
              scalar values corresponding to the similarity metric used for each
              pairwise combination from the two batches.
  
              For example, suppose we use `batch_size_1 = 16` for iterating
              through `influence_src_dataset`, and for the `inputs` argument
              we pass in a Tensor with 3 examples, i.e. batch_size_2 = 3. Also,
              suppose that our inputs and intermediate activations throughout the
              model will have dimension (N, C, H, W). Then, the feature dimensions
              should be flattened within this function. For example::
  
                  >>> av_test.shape
                  torch.Size([3, N, C, H, W])
                  >>> av_src.shape
                  torch.Size([16, N, C, H, W])
                  >>> av_test = torch.view(av_test.shape[0], -1)
                  >>> av_test.shape
                  torch.Size([3, N x C x H x W])
  
              and similarly for av_src. The similarity_metric should then use
              these flattened tensors to return the pairwise similarity matrix.
              For example, `similarity_metric(av_test, av_src)` should return a
              tensor of shape (3, 16).
  
      batch_size (int): Batch size for iterating through `influence_src_dataset`.
      **kwargs: Additional key-value arguments that are necessary for specific
              implementation of `DataInfluence` abstract class.
docstr_tok: ["Args",":","module","(","torch.nn.Module",")",":","An","instance","of","pytorch","model",".","This","model","should","define","all","of","its","layers","as","attributes","of","the","model",".","layers","(","str","or","list","[","str","]",")",":","The","fully","qualified","layer","(","s",")","for","which","the","activation","vectors","are","computed",".","influence_src_dataset","(","torch.utils.data.Dataset",")",":","PyTorch","Dataset","that","is","used","to","create","a","PyTorch","Dataloader","to","iterate","over","the","dataset","and","its","labels",".","This","is","the","dataset","for","which","we","will","be","seeking","for","influential","instances",".","In","most","cases","this","is","the","training","dataset",".","activation_dir","(","str",")",":","The","directory","of","the","path","to","store","and","retrieve","activation","computations",".","Best","practice","would","be","to","use","an","absolute","path",".","model_id","(","str",")",":","The","name\/version","of","the","model","for","which","layer","activations","are","being","computed",".","Activations","will","be","stored","and","loaded","under","the","subdirectory","with","this","name","if","provided",".","similarity_metric","(","Callable",")",":","This","is","a","callable","function","that","computes","a","similarity","metric","between","two","representations",".","For","example",",","the","representations","pair","could","be","from","the","training","and","test","sets",".","This","function","must","adhere","to","certain","standards",".","The","inputs","should","be","torch","Tensors","with","shape","(","batch_size_i\/j",",","feature","dimensions",")",".","The","output","Tensor","should","have","shape","(","batch_size_i",",","batch_size_j",")","with","scalar","values","corresponding","to","the","similarity","metric","used","for","each","pairwise","combination","from","the","two","batches",".","For","example",",","suppose","we","use","`","batch_size_1","=","16","`","for","iterating","through","`","influence_src_dataset","`",",","and","for","the","`","inputs","`","argument","we","pass","in","a","Tensor","with","3","examples",",","i.e",".","batch_size_2","=","3",".","Also",",","suppose","that","our","inputs","and","intermediate","activations","throughout","the","model","will","have","dimension","(","N",",","C",",","H",",","W",")",".","Then",",","the","feature","dimensions","should","be","flattened","within","this","function",".","For","example",":",":",">",">",">","av_test.shape","torch.Size","(","[","3",",","N",",","C",",","H",",","W","]",")",">",">",">","av_src.shape","torch.Size","(","[","16",",","N",",","C",",","H",",","W","]",")",">",">",">","av_test","=","torch.view","(","av_test.shape","[","0","]",",","-1",")",">",">",">","av_test.shape","torch.Size","(","[","3",",","N","x","C","x","H","x","W","]",")","and","similarly","for","av_src",".","The","similarity_metric","should","then","use","these","flattened","tensors","to","return","the","pairwise","similarity","matrix",".","For","example",",","`","similarity_metric","(","av_test",",","av_src",")","`","should","return","a","tensor","of","shape","(","3",",","16",")",".","batch_size","(","int",")",":","Batch","size","for","iterating","through","`","influence_src_dataset","`",".","*","*","kwargs",":","Additional","key-value","arguments","that","are","necessary","for","specific","implementation","of","`","DataInfluence","`","abstract","class","."]
code: def __init__(
      self,
      module: Module,
      layers: Union[str, List[str]],
      influence_src_dataset: Dataset,
      activation_dir: str,
      model_id: str = "",
      similarity_metric: Callable = cosine_similarity,
      similarity_direction: str = "max",
      batch_size: int = 1,
      **kwargs: Any,
  ) -> None:
      r"""
      Args:
          module (torch.nn.Module): An instance of pytorch model. This model should
                  define all of its layers as attributes of the model.
          layers (str or list[str]): The fully qualified layer(s) for which the
                  activation vectors are computed.
          influence_src_dataset (torch.utils.data.Dataset): PyTorch Dataset that is
                  used to create a PyTorch Dataloader to iterate over the dataset and
                  its labels. This is the dataset for which we will be seeking for
                  influential instances. In most cases this is the training dataset.
          activation_dir (str): The directory of the path to store
                  and retrieve activation computations. Best practice would be to use
                  an absolute path.
          model_id (str): The name/version of the model for which layer
                  activations are being computed. Activations will be stored and
                  loaded under the subdirectory with this name if provided.
          similarity_metric (Callable): This is a callable function that computes a
                  similarity metric between two representations. For example, the
                  representations pair could be from the training and test sets.
  
                  This function must adhere to certain standards. The inputs should be
                  torch Tensors with shape (batch_size_i/j, feature dimensions). The
                  output Tensor should have shape (batch_size_i, batch_size_j) with
                  scalar values corresponding to the similarity metric used for each
                  pairwise combination from the two batches.
  
                  For example, suppose we use `batch_size_1 = 16` for iterating
                  through `influence_src_dataset`, and for the `inputs` argument
                  we pass in a Tensor with 3 examples, i.e. batch_size_2 = 3. Also,
                  suppose that our inputs and intermediate activations throughout the
                  model will have dimension (N, C, H, W). Then, the feature dimensions
                  should be flattened within this function. For example::
  
                      >>> av_test.shape
                      torch.Size([3, N, C, H, W])
                      >>> av_src.shape
                      torch.Size([16, N, C, H, W])
                      >>> av_test = torch.view(av_test.shape[0], -1)
                      >>> av_test.shape
                      torch.Size([3, N x C x H x W])
  
                  and similarly for av_src. The similarity_metric should then use
                  these flattened tensors to return the pairwise similarity matrix.
                  For example, `similarity_metric(av_test, av_src)` should return a
                  tensor of shape (3, 16).
  
          batch_size (int): Batch size for iterating through `influence_src_dataset`.
          **kwargs: Additional key-value arguments that are necessary for specific
                  implementation of `DataInfluence` abstract class.
      """
      self.module = module
      self.layers = [layers] if isinstance(layers, str) else layers
      self.influence_src_dataset = influence_src_dataset
      self.activation_dir = activation_dir
      self.model_id = model_id
      self.batch_size = batch_size
  
      if similarity_direction == "max" or similarity_direction == "min":
          self.similarity_direction = similarity_direction
      else:
          raise ValueError(
              f"{similarity_direction} is not a valid value. "
              "Must be either 'max' or 'min'"
          )
  
      if similarity_metric is cosine_similarity:
          if "replace_nan" in kwargs:
              self.replace_nan = kwargs["replace_nan"]
          else:
              self.replace_nan = (
                  -2 if self.similarity_direction == "max" else 2
              )
          similarity_metric = partial(
              cosine_similarity, replace_nan=self.replace_nan
          )
  
      self.similarity_metric = similarity_metric
  
      self.influence_src_dataloader = DataLoader(
          influence_src_dataset, batch_size, shuffle=False
      )
  
file_path: captum/captum/influence/_core/similarity_influence.py
imports: import warnings
  from functools import partial
  from typing import Any, Callable, Dict, List, Optional, Tuple, Union
  import captum._utils.common
  import torch
  from captum._utils.av import AV
  from captum.attr import LayerActivation
  from captum.influence._core.influence import DataInfluence
  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 SimilarityInfluence object from inherited base classes: DataInfluence with example: obj = SimilarityInfluence(module, layers, influence_src_dataset, activation_dir, model_id, similarity_metric, similarity_direction, batch_size)