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",")",")"]
code: def _tcav_sub_computation(
      self,
      scores: Dict[str, Dict[str, Dict[str, Tensor]]],
      layer: str,
      attribs: Tensor,
      cavs: Tensor,
      classes: List[List[int]],
      experimental_sets: List[List[Concept]],
  ) -> None:
      # n_inputs x n_concepts
      tcav_score = torch.matmul(
          attribs.float(), torch.transpose(cavs, 1, 2)
      )
      assert len(tcav_score.shape) == 3, (
          "tcav_score should have 3 dimensions: n_experiments x "
          "n_inputs x n_concepts."
      )
  
      assert attribs.shape[0] == tcav_score.shape[1], (
          "attrib and tcav_score should have the same 1st and "
          "2nd dimensions respectively (n_inputs)."
      )
      # n_experiments x n_concepts
      sign_count_score = torch.mean((tcav_score > 0.0).float(), dim=1)
  
      magnitude_score = torch.mean(tcav_score, dim=1)
  
      for i, (cls_set, concepts) in enumerate(
          zip(classes, experimental_sets)
      ):
          concepts_key = concepts_to_str(concepts)
  
          # sort classes / concepts in the order specified in concept_keys
          concept_ord = [concept.id for concept in concepts]
          class_ord = {cls_: idx for idx, cls_ in enumerate(cls_set)}
  
          new_ord = torch.tensor(
              [class_ord[cncpt] for cncpt in concept_ord],
              device=tcav_score.device,
          )
  
          # sort based on classes
          scores[concepts_key][layer] = {
              "sign_count": torch.index_select(
                  sign_count_score[i, :], dim=0, index=new_ord
              ),
              "magnitude": torch.index_select(
                  magnitude_score[i, :], dim=0, index=new_ord
              ),
          }
  
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 8 for calling the TCAV obj's underlying member method code with example usage: obj._tcav_sub_computation(scores, layer, attribs, cavs, classes, experimental_sets) without return types