def evaluate_stopping(stopping_criteria, obj_prob_vector, curr_prob_vector,
                      per_example_objective):
  """Evaluate whether flipping needs to stop or not."""
  if stopping_criteria == "hotflip":
    # Check whether the label has flipped or not
    return np.argmax(
        obj_prob_vector, axis=1) != np.argmax(
            curr_prob_vector, axis=1)

  elif stopping_criteria.startswith("greater_"):
    # Check whether the objective is greater than a threshold or not
    threshold = float(stopping_criteria[len("greater_"):])
    return per_example_objective >= threshold

  elif stopping_criteria.startswith("lesser_"):
    # Check whether the objective is lesser than a threshold or not
    threshold = float(stopping_criteria[len("lesser_"):])
    return per_example_objective <= threshold

  elif stopping_criteria.startswith("margin_lesser_"):
    # Check whether the margin between top two probabilities is lesser than
    # a threshold or not. This quantity is a measure of confidence.
    threshold = float(stopping_criteria[len("margin_lesser_"):])
    sorted_probs = np.sort(curr_prob_vector)[:, ::-1]
    margin = sorted_probs[:, 0] - sorted_probs[:, 1]
    return margin <= threshold

  elif stopping_criteria.startswith("margin_greater_"):
    # Check whether the margin between top two probabilities is greater than
    # a threshold or not. This quantity is a measure of confidence.
    threshold = float(stopping_criteria[len("margin_greater_"):])
    sorted_probs = np.sort(curr_prob_vector)[:, ::-1]
    margin = sorted_probs[:, 0] - sorted_probs[:, 1]
    return margin >= threshold

  else:
    tf.logging.warning("Stopping criteria not found!")
    return False
Evaluate whether flipping needs to stop or not.
language/bert_extraction/steal_bert_classifier/embedding_perturbations/discrete_invert_embeddings.py