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: This method computes CAVs for given `experiments_sets` and layers
  specified in `self.layers` instance variable. Internally, it
  trains a classifier and creates an instance of CAV class using the
  weights of the trained classifier for each experimental set.
  
  It also allows to compute the CAVs in parallel using python's
  multiprocessing API and the number of processes specified in
  the argument.
  
  Args:
  
      experimental_sets (list[list[Concept]]): A list of lists of concept
              instances for which the cavs will be computed.
      force_train (bool, optional): A flag that indicates whether to
              train the CAVs regardless of whether they are saved or not.
              Default: False
      processes (int, optional): The number of processes to be created
              when running in multi-processing mode. If processes > 0 then
              CAV computation will be performed in parallel using
              multi-processing, otherwise it will be performed sequentially
              in a single process.
              Default: None
  
  Returns:
      cavs (dict) : A mapping of concept ids and layers to CAV objects.
              If CAVs for the concept_ids-layer pairs are present in the
              data storage they will be loaded into the memory, otherwise
              they will be computed using a training process and stored
              in the data storage that can be configured using `save_path`
              input argument.
docstr_tok: ["This","method","computes","CAVs","for","given","`","experiments_sets","`","and","layers","specified","in","`","self.layers","`","instance","variable",".","Internally",",","it","trains","a","classifier","and","creates","an","instance","of","CAV","class","using","the","weights","of","the","trained","classifier","for","each","experimental","set",".","It","also","allows","to","compute","the","CAVs","in","parallel","using","python's","multiprocessing","API","and","the","number","of","processes","specified","in","the","argument",".","Args",":","experimental_sets","(","list","[","list","[","Concept","]","]",")",":","A","list","of","lists","of","concept","instances","for","which","the","cavs","will","be","computed",".","force_train","(","bool",",","optional",")",":","A","flag","that","indicates","whether","to","train","the","CAVs","regardless","of","whether","they","are","saved","or","not",".","Default",":","False","processes","(","int",",","optional",")",":","The","number","of","processes","to","be","created","when","running","in","multi-processing","mode",".","If","processes",">","0","then","CAV","computation","will","be","performed","in","parallel","using","multi-processing",",","otherwise","it","will","be","performed","sequentially","in","a","single","process",".","Default",":","None","Returns",":","cavs","(","dict",")",":","A","mapping","of","concept","ids","and","layers","to","CAV","objects",".","If","CAVs","for","the","concept_ids-layer","pairs","are","present","in","the","data","storage","they","will","be","loaded","into","the","memory",",","otherwise","they","will","be","computed","using","a","training","process","and","stored","in","the","data","storage","that","can","be","configured","using","`","save_path","`","input","argument","."]
code: def compute_cavs(
      self,
      experimental_sets: List[List[Concept]],
      force_train: bool = False,
      processes: int = None,
  ):
      r"""
      This method computes CAVs for given `experiments_sets` and layers
      specified in `self.layers` instance variable. Internally, it
      trains a classifier and creates an instance of CAV class using the
      weights of the trained classifier for each experimental set.
  
      It also allows to compute the CAVs in parallel using python's
      multiprocessing API and the number of processes specified in
      the argument.
  
      Args:
  
          experimental_sets (list[list[Concept]]): A list of lists of concept
                  instances for which the cavs will be computed.
          force_train (bool, optional): A flag that indicates whether to
                  train the CAVs regardless of whether they are saved or not.
                  Default: False
          processes (int, optional): The number of processes to be created
                  when running in multi-processing mode. If processes > 0 then
                  CAV computation will be performed in parallel using
                  multi-processing, otherwise it will be performed sequentially
                  in a single process.
                  Default: None
  
      Returns:
          cavs (dict) : A mapping of concept ids and layers to CAV objects.
                  If CAVs for the concept_ids-layer pairs are present in the
                  data storage they will be loaded into the memory, otherwise
                  they will be computed using a training process and stored
                  in the data storage that can be configured using `save_path`
                  input argument.
      """
  
      # Update self.concepts with concepts
      for concepts in experimental_sets:
          self.concepts.update(concepts)
  
      concept_ids = []
      for concept in self.concepts:
          assert concept.id not in concept_ids, (
              "There is more than one instance "
              "of a concept with id {} defined in experimental sets. Please, "
              "make sure to reuse the same instance of concept".format(
                  str(concept.id)
              )
          )
          concept_ids.append(concept.id)
  
      if force_train:
          self.generate_all_activations()
  
      # List of layers per concept key (experimental_set item) to be trained
      concept_key_to_layers = defaultdict(list)
  
      for concepts in experimental_sets:
          concepts_key = concepts_to_str(concepts)
  
          # If not 'force_train', try to load a saved CAV
          if not force_train:
              layers, concept_layers = self.load_cavs(concepts)
              concept_key_to_layers[concepts_key] = layers
              # Generate activations for missing (concept, layers)
              self.generate_activations(concept_layers)
          else:
              concept_key_to_layers[concepts_key] = self.layers
      if processes is not None and processes > 1:
          pool = multiprocessing.Pool(processes)
          cavs_list = pool.starmap(
              train_cav,
              [
                  (
                      self.model_id,
                      concepts,
                      concept_key_to_layers[concepts_to_str(concepts)],
                      self.classifier,
                      self.save_path,
                      self.classifier_kwargs,
                  )
                  for concepts in experimental_sets
              ],
          )
  
          pool.close()
          pool.join()
  
      else:
          cavs_list = []
          for concepts in experimental_sets:
              cavs_list.append(
                  train_cav(
                      self.model_id,
                      concepts,
                      concept_key_to_layers[concepts_to_str(concepts)],
                      cast(Classifier, self.classifier),
                      self.save_path,
                      self.classifier_kwargs,
                  )
              )
  
      # list[Dict[concept, Dict[layer, list]]] => Dict[concept, Dict[layer, list]]
      for cavs in cavs_list:
          for c_key in cavs:
              self.cavs[c_key].update(cavs[c_key])
  
      return self.cavs
  
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 6 for calling the TCAV obj's underlying member method code with example usage: obj.compute_cavs(experimental_sets, force_train, processes) and returns: self