class_docstr: Uses Adam to minimize the CW L2 objective function
  
  Paper link: https://arxiv.org/abs/1608.04644
class_docstr_tok: ["Uses","Adam","to","minimize","the","CW","L2","objective","function","Paper","link",":","https",":","\/\/arxiv.org\/abs\/1608.04644"]
docstr: put adversary instance inside of the attack instance so all other function within can access
docstr_tok: ["put","adversary","instance","inside","of","the","attack","instance","so","all","other","function","within","can","access"]
code: def _apply(
      self,
      adversary,
      attack_iterations=100,
      c_search_step=20,
      c_range=(0.01, 100),
      c_start=10,
      c_accuracy=0.1,
      targeted=True,
  ):
      """
      put adversary instance inside of the attack instance so all other function within can access
      """
      if not adversary.is_targeted_attack:
          raise ValueError(
              "This attack method only support targeted attack!"
          )
  
      self._adversary = adversary
      img = self._adversary.original  # original image to be attacked
      # print original predict result
      print(
          "guess img before preprocess:{}  expect:{}".format(
              self._adversary.original_label,
              self._adversary.target_label,
          )
      )
  
      # binary search for smallest c and smallest l2
      logging.info(
          "searching for the smallest c that makes attack possible within ({},{})".format(
              c_range[0], c_range[1]
          )
      )
      # init ad perturbation with (-0.001, 0.001)
      ad_perturbation = (
          fluid.global_scope().find_var("parameter").get_tensor()
      )
      ad_perturbation.set(
          0.002 * np.random.random_sample(self._shape).astype("float32")
          - 0.001,
          self.place,
      )
  
      self.l2 = None
      self.img_adv = None
      c_half = c_start
      c_low = c_range[0]
      c_high = c_range[1]
      for i in range(c_search_step):
          l2, img_adv = self._cwb(
              img, c_half, attack_steps=attack_iterations
          )
          # update smallest l2 and img_adv
          if l2 != None:
              # found smallest l2 by current c
              if self.l2 == None or self.l2 > l2:
                  self.l2 = l2
                  self.img_adv = img_adv
              c_high = c_half
          else:
              c_low = c_half
  
          new_half = (c_low + c_high) / 2
          # end of reaching maximum accuracy
          if abs(new_half - c_half) <= c_accuracy:
              break
          c_half = new_half
      # if each c is unsuccessful , use img as the end
      if self.l2 == None:
          self.img_adv = img
      adv_label, adv_score = self._predict_adv(self.img_adv)
      print(("predict label:", adv_label, "softmax:", adv_score))
      # check adversary target if success
      self.img_adv = np.squeeze(self.img_adv)
      self.img_adv = self.img_adv.reshape(img.shape)
  
      self._adversary.try_accept_the_example(self.img_adv, adv_label)
  
      return adversary
  
file_path: AdvBox/adversarialbox/attacks/cw.py
imports: from __future__ import division
  from __future__ import print_function
  from builtins import range
  import logging
  import numpy
  import paddle.fluid
  from .base import Attack
label_desc: Use image node_id 2 for calling the CW_L2_Attack obj's underlying member method code with example usage: obj._apply(adversary, attack_iterations, c_search_step, c_range, c_start, c_accuracy, targeted) and returns: adversary