docstr: Return a tensor that constructs adversarial examples for the given
  input. Generate uses tf.py_func in order to operate over tensors.
  
  :param sess: a TF session.
  :param model: a cleverhans.model.Model object.
  :param batch_size: Number of attacks to run simultaneously.
  :param confidence: Confidence of adversarial examples: higher produces
                     examples with larger l2 distortion, but more
                     strongly classified as adversarial.
  :param targeted: boolean controlling the behavior of the adversarial
                   examples produced. If set to False, they will be
                   misclassified in any wrong class. If set to True,
                   they will be misclassified in a chosen target class.
  :param learning_rate: The learning rate for the attack algorithm.
                        Smaller values produce better results but are
                        slower to converge.
  :param binary_search_steps: The number of times we perform binary
                              search to find the optimal tradeoff-
                              constant between norm of the purturbation
                              and confidence of the classification.
  :param max_iterations: The maximum number of iterations. Setting this
                         to a larger value will produce lower distortion
                         results. Using only a few iterations requires
                         a larger learning rate, and will produce larger
                         distortion results.
  :param abort_early: If true, allows early aborts if gradient descent
                      is unable to make progress (i.e., gets stuck in
                      a local minimum).
  :param initial_const: The initial tradeoff-constant to use to tune the
                        relative importance of size of the pururbation
                        and confidence of classification.
                        If binary_search_steps is large, the initial
                        constant is not important. A smaller value of
                        this constant gives lower distortion results.
  :param clip_min: (optional float) Minimum input component value.
  :param clip_max: (optional float) Maximum input component value.
  :param num_labels: the number of classes in the model's output.
  :param shape: the shape of the model's input tensor.
docstr_tok: ["Return","a","tensor","that","constructs","adversarial","examples","for","the","given","input",".","Generate","uses","tf.py_func","in","order","to","operate","over","tensors",".",":","param","sess",":","a","TF","session",".",":","param","model",":","a","cleverhans.model.Model","object",".",":","param","batch_size",":","Number","of","attacks","to","run","simultaneously",".",":","param","confidence",":","Confidence","of","adversarial","examples",":","higher","produces","examples","with","larger","l2","distortion",",","but","more","strongly","classified","as","adversarial",".",":","param","targeted",":","boolean","controlling","the","behavior","of","the","adversarial","examples","produced",".","If","set","to","False",",","they","will","be","misclassified","in","any","wrong","class",".","If","set","to","True",",","they","will","be","misclassified","in","a","chosen","target","class",".",":","param","learning_rate",":","The","learning","rate","for","the","attack","algorithm",".","Smaller","values","produce","better","results","but","are","slower","to","converge",".",":","param","binary_search_steps",":","The","number","of","times","we","perform","binary","search","to","find","the","optimal","tradeoff-","constant","between","norm","of","the","purturbation","and","confidence","of","the","classification",".",":","param","max_iterations",":","The","maximum","number","of","iterations",".","Setting","this","to","a","larger","value","will","produce","lower","distortion","results",".","Using","only","a","few","iterations","requires","a","larger","learning","rate",",","and","will","produce","larger","distortion","results",".",":","param","abort_early",":","If","true",",","allows","early","aborts","if","gradient","descent","is","unable","to","make","progress","(","i.e.",",","gets","stuck","in","a","local","minimum",")",".",":","param","initial_const",":","The","initial","tradeoff-constant","to","use","to","tune","the","relative","importance","of","size","of","the","pururbation","and","confidence","of","classification",".","If","binary_search_steps","is","large",",","the","initial","constant","is","not","important",".","A","smaller","value","of","this","constant","gives","lower","distortion","results",".",":","param","clip_min",":","(","optional","float",")","Minimum","input","component","value",".",":","param","clip_max",":","(","optional","float",")","Maximum","input","component","value",".",":","param","num_labels",":","the","number","of","classes","in","the","model","'s","output",".",":","param","shape",":","the","shape","of","the","model","'s","input","tensor","."]
code: def __init__(
      self,
      sess,
      model,
      batch_size,
      confidence,
      targeted,
      learning_rate,
      binary_search_steps,
      max_iterations,
      abort_early,
      initial_const,
      clip_min,
      clip_max,
      num_labels,
      shape,
  ):
      """
      Return a tensor that constructs adversarial examples for the given
      input. Generate uses tf.py_func in order to operate over tensors.
  
      :param sess: a TF session.
      :param model: a cleverhans.model.Model object.
      :param batch_size: Number of attacks to run simultaneously.
      :param confidence: Confidence of adversarial examples: higher produces
                         examples with larger l2 distortion, but more
                         strongly classified as adversarial.
      :param targeted: boolean controlling the behavior of the adversarial
                       examples produced. If set to False, they will be
                       misclassified in any wrong class. If set to True,
                       they will be misclassified in a chosen target class.
      :param learning_rate: The learning rate for the attack algorithm.
                            Smaller values produce better results but are
                            slower to converge.
      :param binary_search_steps: The number of times we perform binary
                                  search to find the optimal tradeoff-
                                  constant between norm of the purturbation
                                  and confidence of the classification.
      :param max_iterations: The maximum number of iterations. Setting this
                             to a larger value will produce lower distortion
                             results. Using only a few iterations requires
                             a larger learning rate, and will produce larger
                             distortion results.
      :param abort_early: If true, allows early aborts if gradient descent
                          is unable to make progress (i.e., gets stuck in
                          a local minimum).
      :param initial_const: The initial tradeoff-constant to use to tune the
                            relative importance of size of the pururbation
                            and confidence of classification.
                            If binary_search_steps is large, the initial
                            constant is not important. A smaller value of
                            this constant gives lower distortion results.
      :param clip_min: (optional float) Minimum input component value.
      :param clip_max: (optional float) Maximum input component value.
      :param num_labels: the number of classes in the model's output.
      :param shape: the shape of the model's input tensor.
      """
  
      self.sess = sess
      self.TARGETED = targeted
      self.LEARNING_RATE = learning_rate
      self.MAX_ITERATIONS = max_iterations
      self.BINARY_SEARCH_STEPS = binary_search_steps
      self.ABORT_EARLY = abort_early
      self.CONFIDENCE = confidence
      self.initial_const = initial_const
      self.batch_size = batch_size
      self.clip_min = clip_min
      self.clip_max = clip_max
      self.model = model
  
      self.repeat = binary_search_steps >= 10
  
      self.shape = shape = tuple([batch_size] + list(shape))
  
      # the variable we're going to optimize over
      modifier = tf.Variable(np.zeros(shape, dtype=np_dtype))
  
      # these are variables to be more efficient in sending data to tf
      self.timg = tf.Variable(
          np.zeros(shape), dtype=tf_dtype, name="timg"
      )
      self.tlab = tf.Variable(
          np.zeros((batch_size, num_labels)),
          dtype=tf_dtype,
          name="tlab",
      )
      self.const = tf.Variable(
          np.zeros(batch_size), dtype=tf_dtype, name="const"
      )
  
      # and here's what we use to assign them
      self.assign_timg = tf.placeholder(
          tf_dtype, shape, name="assign_timg"
      )
      self.assign_tlab = tf.placeholder(
          tf_dtype, (batch_size, num_labels), name="assign_tlab"
      )
      self.assign_const = tf.placeholder(
          tf_dtype, [batch_size], name="assign_const"
      )
  
      # the resulting instance, tanh'd to keep bounded from clip_min
      # to clip_max
      self.newimg = (tf.tanh(modifier + self.timg) + 1) / 2
      self.newimg = self.newimg * (clip_max - clip_min) + clip_min
  
      # prediction BEFORE-SOFTMAX of the model
      self.output = model.get_logits(self.newimg)
  
      # distance to the input data
      self.other = (tf.tanh(self.timg) + 1) / 2 * (
          clip_max - clip_min
      ) + clip_min
      self.l2dist = reduce_sum(
          tf.square(self.newimg - self.other),
          list(range(1, len(shape))),
      )
  
      # compute the probability of the label class versus the maximum other
      real = reduce_sum((self.tlab) * self.output, 1)
      other = reduce_max(
          (1 - self.tlab) * self.output - self.tlab * 10000, 1
      )
  
      if self.TARGETED:
          # if targeted, optimize for making the other class most likely
          loss1 = tf.maximum(ZERO(), other - real + self.CONFIDENCE)
      else:
          # if untargeted, optimize for making this class least likely.
          loss1 = tf.maximum(ZERO(), real - other + self.CONFIDENCE)
  
      # sum up the losses
      self.loss2 = reduce_sum(self.l2dist)
      self.loss1 = reduce_sum(self.const * loss1)
      self.loss = self.loss1 + self.loss2
  
      # Setup the adam optimizer and keep track of variables we're creating
      start_vars = set(x.name for x in tf.global_variables())
      optimizer = tf.train.AdamOptimizer(self.LEARNING_RATE)
      self.train = optimizer.minimize(self.loss, var_list=[modifier])
      end_vars = tf.global_variables()
      new_vars = [x for x in end_vars if x.name not in start_vars]
  
      # these are the variables to initialize when we run
      self.setup = []
      self.setup.append(self.timg.assign(self.assign_timg))
      self.setup.append(self.tlab.assign(self.assign_tlab))
      self.setup.append(self.const.assign(self.assign_const))
  
      self.init = tf.variables_initializer(
          var_list=[modifier] + new_vars
      )
  
file_path: cleverhans/cleverhans_v3.1.0/cleverhans/attacks/carlini_wagner_l2.py
imports: import logging
  import numpy
  import tensorflow
  from cleverhans.attacks.attack import Attack
  from cleverhans.compat import reduce_sum, reduce_max
  from cleverhans.model import CallableModelWrapper, Model, wrapper_warning_logits
  from cleverhans import utils
label_desc: Use image node_id 1 to create a new CWL2 object from inherited base classes: object with example: obj = CWL2(sess, model, batch_size, confidence, targeted, learning_rate, binary_search_steps, max_iterations, abort_early, initial_const, clip_min, clip_max, num_labels, shape)