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: Generates sets of token positions that are eligible for flipping.
docstr_tok: ["Generates","sets","of","token","positions","that","are","eligible","for","flipping","."]
code: def _gen_token_idxs_to_flip(
      self,
      tokens: list[str],
      token_grads: np.ndarray,
      max_flips: int,
      tokens_to_ignore: list[str],
  ) -> Iterator[tuple[int, ...]]:
      """Generates sets of token positions that are eligible for flipping."""
      # Consider all combinations of tokens upto length max_flips.
      # We will iterate through this list (sortted by cardinality) and at each
      # iteration, replace the selected tokens with corresponding replacement
      # tokens and checks if the prediction flips. At each cardinality, we will
      # consider combinations by ordering tokens by gradient L2 in order to
      # prioritize flipping tokens that may have the largest impact on the
      # prediction.
      token_grads_l2 = np.sum(token_grads * token_grads, axis=-1)
      # TODO(ataly, bastings): Consider sorting by attributions (either
      # Integrated Gradients or Shapley values).
      token_idxs = np.argsort(token_grads_l2)[::-1]
      token_idxs_to_flip = [
          idx
          for idx in token_idxs
          if tokens[idx] not in tokens_to_ignore
      ]
      # If the number of tokens considered for flipping is larger than
      # MAX_FLIPPABLE_TOKENS we only consider the top tokens.
      token_idxs_to_flip = token_idxs_to_flip[:MAX_FLIPPABLE_TOKENS]
  
      for i in range(min(len(token_idxs_to_flip), max_flips)):
          for s in itertools.combinations(token_idxs_to_flip, i + 1):
              yield s
  
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 7 for calling the HotFlip obj's underlying member method code with example usage: obj._gen_token_idxs_to_flip(tokens, token_grads, max_flips, tokens_to_ignore) without return types