class_docstr: Quantitative Testing with Concept Activation Vectors (TCAV).
  
  TCAV is an interpretability method which allows users to define a concept
  by selecting a set of representative examples and trains a classifier at
  different model layers to determine whether that concept has any influence
  over the predictions of the model (https://arxiv.org/pdf/1711.11279.pdf).
  If so, it can be measured as an aggregate score, and also individual points
  can be measured by how much of that concept they contain, at a given layer.
  
  The original implementation can be found at:
  https://github.com/tensorflow/tcav
  
  This component requires that the following fields in the model spec. Field
  names like `layer` are placeholders; you can call them whatever you like,
  and as with other LIT components multiple segments are supported.
    Output:
      - Embeddings (`emb_layer`) to return the input embeddings
          for a layer
      - Gradients (`grad_layer`) to return the gradients w.r.t.
          `emb_layer`
      - Gradients class (`grad_class`) to return the label that gradients
        were computed for. This is usually a CategoryLabel, but can be anything
        since it will just be fed back into the model.
      - MulticlassPreds (`probas`)
  
  **Note: TCAV calls the model multiple times. It is _strongly_ recommended that
  you use a caching model (i.e., wrap your model in a `CachingModelWrapper`) to
  optimize performance.**
docstr: Returns directional derivatives, tcav score, and LM accuracy.
docstr_tok: ["Returns","directional","derivatives",",","tcav","score",",","and","LM","accuracy","."]
code: def _run_tcav(
      self,
      concept_outputs: list[JsonDict],
      comparison_outputs: list[JsonDict],
      dataset_outputs: list[JsonDict],
      class_to_explain: Any,
      emb_layer: str,
      grad_layer: str,
      grad_class_key: str,
      test_size: float,
      random_state=None,
  ):
      """Returns directional derivatives, tcav score, and LM accuracy."""
      x, y = self._get_training_data(
          comparison_outputs, concept_outputs, emb_layer
      )
      # Get CAV vector and accuracy of the trained linear model.
      cav, accuracy = self.get_trained_cav(
          x, y, test_size, random_state
      )
  
      # Compute directional derivatives for class to explain.
      dir_derivs = self.get_dir_derivs(
          cav,
          dataset_outputs,
          grad_layer,
          grad_class_key,
          class_to_explain,
      )
  
      # Calculate the TCAV score using the gradient class directional derivatives.
      tcav_score = self.compute_tcav_score(dir_derivs)
  
      # Compute cosine similarity and dot product between CAV and activations.
      cos_sim, dot_prods = self.compute_local_scores(
          cav, dataset_outputs, emb_layer
      )
  
      return {
          "score": tcav_score,
          "cos_sim": cos_sim,
          "dot_prods": dot_prods,
          "accuracy": accuracy,
          "cav": cav,
      }
  
imports: from collections.abc import Sequence
  import dataclasses
  import math
  import random
  from typing import Any, Optional, cast
  from lit_nlp.api import components
  from lit_nlp.api import dataset
  from lit_nlp.api import model
  from lit_nlp.api import types
  from lit_nlp.lib import utils
  import numpy
  import scipy.stats
  import sklearn.linear_model
  import sklearn.model_selection
label_desc: Use image node_id 10 for calling the TCAV obj's underlying member method code with example usage: obj._run_tcav(concept_outputs, comparison_outputs, dataset_outputs, class_to_explain, emb_layer, grad_layer, grad_class_key, test_size, random_state) and returns: dict