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: Get predictions with gradients w.r.t. argmax class.
docstr_tok: ["Get","predictions","with","gradients","w.r.t",".","argmax","class","."]
code: def get_predictions(
      self,
      model: lit_model.Model,
      inputs: Sequence[JsonDict],
      precomputed_outputs: Optional[Sequence[JsonDict]] = None,
  ) -> list[JsonDict]:
      """Get predictions with gradients w.r.t. argmax class."""
      # Get outputs using model.predict().
      if precomputed_outputs is None:
          predictions = list(model.predict(inputs))
      else:
          predictions = precomputed_outputs
  
      output_spec = model.output_spec()
  
      # TCAV always operates on the predicted class, but LIT models typically
      # compute gradients w.r.t. a specified target label.
      # Make new examples here with this set.
      target_fields = utils.find_spec_keys(
          output_spec, types.MulticlassPreds
      )
      valid_target_fields = [
          t for t in target_fields if getattr(output_spec[t], "parent")
      ]
      modified_inputs = []
      for ex, preds in zip(inputs, predictions, strict=True):
          overrides = {}
          for field in valid_target_fields:
              label_idx = np.argmax(preds[field])
              label = cast(
                  types.MulticlassPreds, output_spec[field]
              ).vocab[label_idx]
              parent_field = getattr(output_spec[field], "parent")
              overrides[parent_field] = label
          modified_inputs.append(
              utils.make_modified_input(ex, overrides, "TCAV")
          )
  
      # TODO(b/294613507): enable caching here for better performance?
      # Any examples that were unmodified will already be cached, so this is only
      # for cases where argmax pred != target label.
      predictions = list(model.predict(modified_inputs))
      return predictions
  
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 3 for calling the TCAV obj's underlying member method code with example usage: obj.get_predictions(model, inputs, precomputed_outputs) and returns: predictions