class_docstr: HotFlip generator.
  
  This implemention uses a single backward pass to estimate the gradient of
  each token and uses them to heuristically estimate the impact of perturbing
  the token.
  
  This generator works for both classification and regression models. In the
  case of classification models, the returned counterfactuals are guaranteed to
  have a different prediction class as the original example. In the case of
  regression models, the returned counterfactuals are guaranteed to be on the
  opposite side of a user-provided threshold as the original example.
  
  The returned counterfactuals are guaranteed to be minimal in the sense that
  no strict subset of the applied token perturbations would have resulted in a
  prediction flip.
class_docstr_tok: ["HotFlip","generator",".","This","implemention","uses","a","single","backward","pass","to","estimate","the","gradient","of","each","token","and","uses","them","to","heuristically","estimate","the","impact","of","perturbing","the","token",".","This","generator","works","for","both","classification","and","regression","models",".","In","the","case","of","classification","models",",","the","returned","counterfactuals","are","guaranteed","to","have","a","different","prediction","class","as","the","original","example",".","In","the","case","of","regression","models",",","the","returned","counterfactuals","are","guaranteed","to","be","on","the","opposite","side","of","a","user-provided","threshold","as","the","original","example",".","The","returned","counterfactuals","are","guaranteed","to","be","minimal","in","the","sense","that","no","strict","subset","of","the","applied","token","perturbations","would","have","resulted","in","a","prediction","flip","."]
docstr: Identifies replacement tokens for each token position.
docstr_tok: ["Identifies","replacement","tokens","for","each","token","position","."]
code: def _get_replacement_tokens(
      self,
      embedding_matrix: np.ndarray,
      inv_vocab: list[str],
      token_grads: np.ndarray,
      direction: int = -1,
  ) -> list[str]:
      """Identifies replacement tokens for each token position."""
      token_grads = token_grads * direction
      # Compute dot product of each input token gradient with the embedding
      # matrix, and pick the argmin.
      # TODO(ataly): Only consider tokens that have the same part-of-speech
      # tag as the original token and/or a certain cosine similarity with the
      # original token.
      replacement_token_ids = np.argmax(
          (np.expand_dims(embedding_matrix, 1) @ token_grads.T).squeeze(
              1
          ),
          axis=0,
      )
      replacement_tokens = [
          inv_vocab[id] for id in replacement_token_ids
      ]
      return replacement_tokens
  
imports: from collections.abc import Iterator, Mapping
  import itertools
  from typing import Any, cast, Optional
  from absl import logging
  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.components import cf_utils
  from lit_nlp.lib import utils
  import numpy
label_desc: Use image node_id 10 for calling the HotFlip obj's underlying member method code with example usage: obj._get_replacement_tokens(embedding_matrix, inv_vocab, token_grads, direction) and returns: replacement_tokens