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"]
code: def __init__(
      self,
      model,
      learning_rate,
      attack_model,
      with_gpu=False,
      shape=[1, 28, 28],
      dim=10,
      confidence_level=0,
      multi_clip=False,
  ):
      super(CW_L2_Attack, self).__init__(model)
      self._predicts_normalized = None
      self._adversary = None  # type: Adversary
      #########################################
      # build cw attack computation graph
      # use CPU or GPU
      self.place = fluid.CUDAPlace(0) if with_gpu else fluid.CPUPlace()
      self.exe = fluid.Executor(self.place)
      # clone the prebuilt program that has cnn to attack
      self.attack_main_program = fluid.Program()
      self.attack_startup_program = fluid.Program()
      # init CW Graph parameters
      self._attack_model = attack_model
      self._shape = shape
      self._dim = dim
      self.confidence = confidence_level
      # compute clip min and max
      if multi_clip:
          self.clip_shape = [3, 1, 1]
          img_mean = np.array([0.485, 0.456, 0.406]).reshape(
              self.clip_shape
          )
          img_std = np.array([0.229, 0.224, 0.225]).reshape(
              self.clip_shape
          )
          self.pa_clip_min = (
              np.zeros(self.clip_shape) - img_mean
          ) / img_std
          self.pa_clip_max = (
              np.ones(self.clip_shape) - img_mean
          ) / img_std
      else:
          clip_min, clip_max = self.model._bounds
          self.clip_shape = [1, 1, 1]
          self.pa_clip_min = np.array(clip_min).reshape(self.clip_shape)
          self.pa_clip_max = np.array(clip_max).reshape(self.clip_shape)
  
      # build cw attack compute graph within attack programs
      with fluid.program_guard(
          main_program=self.attack_main_program,
          startup_program=self.attack_startup_program,
      ):
          img_0_1_placehold = fluid.layers.data(
              name="img_data_scaled", shape=self._shape, dtype="float32"
          )
          target_placehold = fluid.layers.data(
              name="target", shape=[self._dim], dtype="float32"
          )
          c_placehold = fluid.layers.data(
              name="c", shape=[1], dtype="float32"
          )
          # add this perturbation
          self.ad_perturbation = fluid.layers.create_parameter(
              name="parameter",
              shape=self._shape,
              dtype="float32",
              is_bias=False,
          )
          # add clip_min and clip_max for normalization
          self.clip_min = fluid.layers.create_parameter(
              name="clip_min",
              shape=self.clip_shape,
              dtype="float32",
              is_bias=False,
          )
          self.clip_max = fluid.layers.create_parameter(
              name="clip_max",
              shape=self.clip_shape,
              dtype="float32",
              is_bias=False,
          )
  
          # construct graph with perturbation and cnn model
          constrained, dis_L2 = self._constrain_cwb(img_0_1_placehold)
          loss, _, _ = self._loss_cwb(
              target_placehold, constrained, dis_L2, c_placehold
          )
  
          # Adam optimizer as suggested in paper
          optimizer = fluid.optimizer.Adam(learning_rate=learning_rate)
          optimizer.minimize(loss, parameter_list=["parameter"])
  
      # initial variables and parameters every time before attack
      self.exe.run(self.attack_startup_program)
  
      ad_min = fluid.global_scope().find_var("clip_min").get_tensor()
      ad_min.set(self.pa_clip_min.astype("float32"), self.place)
      ad_max = fluid.global_scope().find_var("clip_max").get_tensor()
      ad_max.set(self.pa_clip_max.astype("float32"), self.place)
  
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 1 to create a new CW_L2_Attack object from inherited base classes: Attack with example: obj = CW_L2_Attack(model, learning_rate, attack_model, with_gpu, shape, dim, confidence_level, multi_clip)