class_docstr: This class implements ConceptInterpreter abstract class using an
  approach called Testing with Concept Activation Vectors (TCAVs),
  as described in the paper:
  https://arxiv.org/abs/1711.11279
  
  TCAV scores for a given layer, a list of concepts and input example
  are computed using the dot product between prediction's layer
  sensitivities for given input examples and Concept Activation Vectors
  (CAVs) in that same layer.
  
  CAVs are defined as vectors that are orthogonal to the classification boundary
  hyperplane that separate given concepts in a given layer from each other.
  For a given layer, CAVs are computed by training a classifier that uses the
  layer activation vectors for a set of concept examples as input examples and
  concept ids as corresponding input labels. Trained weights of
  that classifier represent CAVs.
  
  CAVs are represented as a learned weight matrix with the dimensionality
  C X F, where:
  F represents the number of input features in the classifier.
  C is the number of concepts used for the classification. Concept
  ids are used as labels for concept examples during the training.
  
  We can use any layer attribution algorithm to compute layer sensitivities
  of a model prediction.
  For example, the gradients of an output prediction w.r.t. the outputs of
  the layer.
  The CAVs and the Sensitivities (SENS) are used to compute the TCAV score:
  
  0. TCAV = CAV • SENS, a dot product between those two vectors
  
  The final TCAV score can be computed by aggregating the TCAV scores
  for each input concept based on the sign or magnitude of the tcav scores.
  
  1. sign_count_score = | TCAV > 0 | / | TCAV |
  2. magnitude_score = SUM(ABS(TCAV * (TCAV > 0))) / SUM(ABS(TCAV))
class_docstr_tok: ["This","class","implements","ConceptInterpreter","abstract","class","using","an","approach","called","Testing","with","Concept","Activation","Vectors","(","TCAVs",")",",","as","described","in","the","paper",":","https",":","\/\/arxiv.org\/abs\/1711.11279","TCAV","scores","for","a","given","layer",",","a","list","of","concepts","and","input","example","are","computed","using","the","dot","product","between","prediction","'s","layer","sensitivities","for","given","input","examples","and","Concept","Activation","Vectors","(","CAVs",")","in","that","same","layer",".","CAVs","are","defined","as","vectors","that","are","orthogonal","to","the","classification","boundary","hyperplane","that","separate","given","concepts","in","a","given","layer","from","each","other",".","For","a","given","layer",",","CAVs","are","computed","by","training","a","classifier","that","uses","the","layer","activation","vectors","for","a","set","of","concept","examples","as","input","examples","and","concept","ids","as","corresponding","input","labels",".","Trained","weights","of","that","classifier","represent","CAVs",".","CAVs","are","represented","as","a","learned","weight","matrix","with","the","dimensionality","C","X","F",",","where",":","F","represents","the","number","of","input","features","in","the","classifier",".","C","is","the","number","of","concepts","used","for","the","classification",".","Concept","ids","are","used","as","labels","for","concept","examples","during","the","training",".","We","can","use","any","layer","attribution","algorithm","to","compute","layer","sensitivities","of","a","model","prediction",".","For","example",",","the","gradients","of","an","output","prediction","w.r.t",".","the","outputs","of","the","layer",".","The","CAVs","and","the","Sensitivities","(","SENS",")","are","used","to","compute","the","TCAV","score",":","0",".","TCAV","=","CAV","\u2022","SENS",",","a","dot","product","between","those","two","vectors","The","final","TCAV","score","can","be","computed","by","aggregating","the","TCAV","scores","for","each","input","concept","based","on","the","sign","or","magnitude","of","the","tcav","scores",".","1.","sign_count_score","=","|","TCAV",">","0","|","\/","|","TCAV","|","2.","magnitude_score","=","SUM","(","ABS","(","TCAV","*","(","TCAV",">","0",")",")",")","\/","SUM","(","ABS","(","TCAV",")",")"]
docstr: Args:
  
      model (Module): An instance of pytorch model that is used to compute
              layer activations and attributions.
      layers (str or list[str]): A list of layer name(s) that are
              used for computing concept activations (cavs) and layer
              attributions.
      model_id (str, optional): A unique identifier for the PyTorch `model`
              passed as first argument to the constructor of TCAV class. It
              is used to store and load activations for given input `model`
              and associated `layers`.
      classifier (Classifier, optional): A custom classifier class, such as the
              Sklearn "linear_model" that allows us to train a model
              using the activation vectors extracted for a layer per concept.
              It also allows us to access trained weights of the model
              and the list of prediction classes.
      layer_attr_method (LayerAttribution, optional): An instance of a layer
              attribution algorithm that helps us to compute model prediction
              sensitivity scores.
  
              Default: None
              If `layer_attr_method` is None, we default it to gradients
              for the layers using `LayerGradientXActivation` layer
              attribution algorithm.
      save_path (str, optional): The path for storing CAVs and
              Activation Vectors (AVs).
      classifier_kwargs (Any, optional): Additional arguments such as
              `test_split_ratio` that are passed to concept `classifier`.
  
  Examples::
      >>>
      >>> # TCAV use example:
      >>>
      >>> # Define the concepts
      >>> stripes = Concept(0, "stripes", striped_data_iter)
      >>> random = Concept(1, "random", random_data_iter)
      >>>
      >>>
      >>> mytcav = TCAV(model=imagenet,
      >>>     layers=['inception4c', 'inception4d'])
      >>>
      >>> scores = mytcav.interpret(inputs, [[stripes, random]], target = 0)
      >>>
      For more thorough examples, please check out TCAV tutorial and test cases.
docstr_tok: ["Args",":","model","(","Module",")",":","An","instance","of","pytorch","model","that","is","used","to","compute","layer","activations","and","attributions",".","layers","(","str","or","list","[","str","]",")",":","A","list","of","layer","name","(","s",")","that","are","used","for","computing","concept","activations","(","cavs",")","and","layer","attributions",".","model_id","(","str",",","optional",")",":","A","unique","identifier","for","the","PyTorch","`","model","`","passed","as","first","argument","to","the","constructor","of","TCAV","class",".","It","is","used","to","store","and","load","activations","for","given","input","`","model","`","and","associated","`","layers","`",".","classifier","(","Classifier",",","optional",")",":","A","custom","classifier","class",",","such","as","the","Sklearn","``","linear_model","''","that","allows","us","to","train","a","model","using","the","activation","vectors","extracted","for","a","layer","per","concept",".","It","also","allows","us","to","access","trained","weights","of","the","model","and","the","list","of","prediction","classes",".","layer_attr_method","(","LayerAttribution",",","optional",")",":","An","instance","of","a","layer","attribution","algorithm","that","helps","us","to","compute","model","prediction","sensitivity","scores",".","Default",":","None","If","`","layer_attr_method","`","is","None",",","we","default","it","to","gradients","for","the","layers","using","`","LayerGradientXActivation","`","layer","attribution","algorithm",".","save_path","(","str",",","optional",")",":","The","path","for","storing","CAVs","and","Activation","Vectors","(","AVs",")",".","classifier_kwargs","(","Any",",","optional",")",":","Additional","arguments","such","as","`","test_split_ratio","`","that","are","passed","to","concept","`","classifier","`",".","Examples",":",":",">",">",">",">",">",">","#","TCAV","use","example",":",">",">",">",">",">",">","#","Define","the","concepts",">",">",">","stripes","=","Concept","(","0",",","``","stripes","''",",","striped_data_iter",")",">",">",">","random","=","Concept","(","1",",","``","random","''",",","random_data_iter",")",">",">",">",">",">",">",">",">",">","mytcav","=","TCAV","(","model=imagenet",",",">",">",">","layers=","[","'inception4c","'",",","'inception4d","'","]",")",">",">",">",">",">",">","scores","=","mytcav.interpret","(","inputs",",","[","[","stripes",",","random","]","]",",","target","=","0",")",">",">",">","For","more","thorough","examples",",","please","check","out","TCAV","tutorial","and","test","cases","."]
code: def __init__(
      self,
      model: Module,
      layers: Union[str, List[str]],
      model_id: str = "default_model_id",
      classifier: Classifier = None,
      layer_attr_method: LayerAttribution = None,
      attribute_to_layer_input=False,
      save_path: str = "./cav/",
      **classifier_kwargs: Any,
  ) -> None:
      r"""
      Args:
  
          model (Module): An instance of pytorch model that is used to compute
                  layer activations and attributions.
          layers (str or list[str]): A list of layer name(s) that are
                  used for computing concept activations (cavs) and layer
                  attributions.
          model_id (str, optional): A unique identifier for the PyTorch `model`
                  passed as first argument to the constructor of TCAV class. It
                  is used to store and load activations for given input `model`
                  and associated `layers`.
          classifier (Classifier, optional): A custom classifier class, such as the
                  Sklearn "linear_model" that allows us to train a model
                  using the activation vectors extracted for a layer per concept.
                  It also allows us to access trained weights of the model
                  and the list of prediction classes.
          layer_attr_method (LayerAttribution, optional): An instance of a layer
                  attribution algorithm that helps us to compute model prediction
                  sensitivity scores.
  
                  Default: None
                  If `layer_attr_method` is None, we default it to gradients
                  for the layers using `LayerGradientXActivation` layer
                  attribution algorithm.
          save_path (str, optional): The path for storing CAVs and
                  Activation Vectors (AVs).
          classifier_kwargs (Any, optional): Additional arguments such as
                  `test_split_ratio` that are passed to concept `classifier`.
  
      Examples::
          >>>
          >>> # TCAV use example:
          >>>
          >>> # Define the concepts
          >>> stripes = Concept(0, "stripes", striped_data_iter)
          >>> random = Concept(1, "random", random_data_iter)
          >>>
          >>>
          >>> mytcav = TCAV(model=imagenet,
          >>>     layers=['inception4c', 'inception4d'])
          >>>
          >>> scores = mytcav.interpret(inputs, [[stripes, random]], target = 0)
          >>>
          For more thorough examples, please check out TCAV tutorial and test cases.
      """
      ConceptInterpreter.__init__(self, model)
      self.layers = [layers] if isinstance(layers, str) else layers
      self.model_id = model_id
      self.concepts: Set[Concept] = set()
      self.classifier = classifier
      self.classifier_kwargs = classifier_kwargs
      self.cavs: Dict[str, Dict[str, CAV]] = defaultdict(
          lambda: defaultdict()
      )
      if self.classifier is None:
          self.classifier = DefaultClassifier()
      if layer_attr_method is None:
          self.layer_attr_method = cast(
              LayerAttribution,
              LayerGradientXActivation(  # type: ignore
                  model, None, multiply_by_inputs=False
              ),
          )
      else:
          self.layer_attr_method = layer_attr_method
  
      assert model_id, (
          "`model_id` cannot be None or empty. Consider giving `model_id` "
          "a meaningful name or leave it unspecified. If model_id is unspecified we "
          "will use `default_model_id` as its default value."
      )
  
      self.attribute_to_layer_input = attribute_to_layer_input
      self.save_path = save_path
  
      # Creates CAV save directory if it doesn't exist. It is created once in the
      # constructor before generating the CAVs.
      # It is assumed that `model_id` can be used as a valid directory name
      # otherwise `create_cav_dir_if_missing` will raise an error
      CAV.create_cav_dir_if_missing(self.save_path, model_id)
  
imports: from collections import defaultdict
  from typing import Any, cast, Dict, List, Set, Tuple, Union
  import numpy
  import torch
  import torch.multiprocessing
  from captum._utils.av import AV
  from captum._utils.common import _format_tensor_into_tuples, _get_module_from_name
  from captum._utils.typing import TargetType, TensorOrTupleOfTensorsGeneric
  from captum.attr import LayerActivation, LayerAttribution, LayerGradientXActivation
  from captum.concept._core.cav import CAV
  from captum.concept._core.concept import Concept, ConceptInterpreter
  from captum.concept._utils.classifier import Classifier, DefaultClassifier
  from captum.concept._utils.common import concepts_to_str
  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 TCAV object from inherited base classes: ConceptInterpreter with example: obj = TCAV(model, layers, model_id, classifier, layer_attr_method, attribute_to_layer_input, save_path)