class_docstr: Implementation of the High-Confidence-Low-Uncertainty (HCLU) adversarial example formulation by Grosse et al. (2018)
  
  | Paper link: https://arxiv.org/abs/1812.02606
docstr: Generate adversarial examples and return them as an array.
  
  :param x: An array with the original inputs to be attacked.
  :param y: Target values (class labels) one-hot-encoded of shape (nb_samples, nb_classes) or indices of shape
            (nb_samples,).
  :return: An array holding the adversarial examples.
docstr_tok: ["Generate","adversarial","examples","and","return","them","as","an","array",".",":","param","x",":","An","array","with","the","original","inputs","to","be","attacked",".",":","param","y",":","Target","values","(","class","labels",")","one-hot-encoded","of","shape","(","nb_samples",",","nb_classes",")","or","indices","of","shape","(","nb_samples",",",")",".",":","return",":","An","array","holding","the","adversarial","examples","."]
code: def generate(
      self, x: np.ndarray, y: Optional[np.ndarray] = None, **kwargs
  ) -> np.ndarray:
      """
      Generate adversarial examples and return them as an array.
  
      :param x: An array with the original inputs to be attacked.
      :param y: Target values (class labels) one-hot-encoded of shape (nb_samples, nb_classes) or indices of shape
                (nb_samples,).
      :return: An array holding the adversarial examples.
      """
      x_adv = copy.copy(x)
  
      def minfun(x, args):  # minimize L2 norm
          return np.sum(np.sqrt((x - args["orig"]) ** 2))
  
      def constraint_conf(x, args):  # constraint for confidence
          pred = args["classifier"].predict(x.reshape(1, -1))[0, 0]
          if args["class_zero"]:
              pred = 1.0 - pred
          return (pred - args["conf"]).reshape(-1)
  
      def constraint_unc(x, args):  # constraint for uncertainty
          cur_unc = (
              args["classifier"].predict_uncertainty(x.reshape(1, -1))
          ).reshape(-1)
          return (args["max_uncertainty"] - cur_unc)[0]
  
      bounds = []
      # adding bounds, to not go away from original data
      for i in range(np.shape(x)[1]):
          bounds.append((self.min_val, self.max_val))
      for i in trange(
          x.shape[0], desc="HCLU", disable=not self.verbose
      ):  # go through data and craft
          # get properties for attack
          max_uncertainty = (
              self.unc_increase
              * self.estimator.predict_uncertainty(
                  x_adv[i].reshape(1, -1)
              )
          )
          class_zero = (
              not self.estimator.predict(x_adv[i].reshape(1, -1))[0, 0]
              < 0.5
          )
          init_args = {
              "classifier": self.estimator,
              "class_zero": class_zero,
              "max_uncertainty": max_uncertainty,
              "conf": self.conf,
          }
          constr_conf = {
              "type": "ineq",
              "fun": constraint_conf,
              "args": (init_args,),
          }
          constr_unc = {
              "type": "ineq",
              "fun": constraint_unc,
              "args": (init_args,),
          }
          args = {"args": init_args, "orig": x[i].reshape(-1)}
          # finally, run optimization
          x_adv[i] = minimize(
              minfun,
              x_adv[i],
              args=args,
              bounds=bounds,
              constraints=[constr_conf, constr_unc],
          )["x"]
  
      return x_adv
  
file_path: adversarial-robustness-toolbox/art/attacks/evasion/hclu.py
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import copy
  import logging
  from typing import Optional
  import numpy
  from scipy.optimize import minimize
  from tqdm.auto import trange
  from art.attacks.attack import EvasionAttack
  from art.estimators.classification.GPy import GPyGaussianProcessClassifier
label_desc: Use image node_id 2 for calling the HighConfidenceLowUncertainty obj's underlying member method code with example usage: obj.generate(x, y) and returns: x_adv, np, unknown, unknown