class_docstr: Optimizer for gradient-free attacks in https://arxiv.org/abs/1802.05666.
  
  Gradients estimates are computed using Simultaneous Perturbation Stochastic Approximation (SPSA),
  combined with the ADAM update rule (https://arxiv.org/abs/1412.6980).
class_docstr_tok: ["Optimizer","for","gradient-free","attacks","in","https",":","\/\/arxiv.org\/abs\/1802.05666",".","Gradients","estimates","are","computed","using","Simultaneous","Perturbation","Stochastic","Approximation","(","SPSA",")",",","combined","with","the","ADAM","update","rule","(","https",":","\/\/arxiv.org\/abs\/1412.6980",")","."]
docstr: Compute a new value of `x` to minimize `loss_fn` using SPSA.
  
  Args:
      loss_fn:  a callable that takes `x`, a batch of images, and returns a batch of loss values.
                `x` will be optimized to minimize `loss_fn(x)`.
      x:  A list of Tensors, the values to be updated. This is analogous to the `var_list` argument
          in standard TF Optimizer.
  
  Returns:
      new_x: A list of Tensors, the same length as `x`, which are updated
      new_optim_state:  A dict, with the same structure as `optim_state`, which have been updated.
docstr_tok: ["Compute","a","new","value","of","`","x","`","to","minimize","`","loss_fn","`","using","SPSA",".","Args",":","loss_fn",":","a","callable","that","takes","`","x","`",",","a","batch","of","images",",","and","returns","a","batch","of","loss","values",".","`","x","`","will","be","optimized","to","minimize","`","loss_fn","(","x",")","`",".","x",":","A","list","of","Tensors",",","the","values","to","be","updated",".","This","is","analogous","to","the","`","var_list","`","argument","in","standard","TF","Optimizer",".","Returns",":","new_x",":","A","list","of","Tensors",",","the","same","length","as","`","x","`",",","which","are","updated","new_optim_state",":","A","dict",",","with","the","same","structure","as","`","optim_state","`",",","which","have","been","updated","."]
code: def _compute_gradients(self, loss_fn, x):
      """Compute a new value of `x` to minimize `loss_fn` using SPSA.
  
      Args:
          loss_fn:  a callable that takes `x`, a batch of images, and returns a batch of loss values.
                    `x` will be optimized to minimize `loss_fn(x)`.
          x:  A list of Tensors, the values to be updated. This is analogous to the `var_list` argument
              in standard TF Optimizer.
  
      Returns:
          new_x: A list of Tensors, the same length as `x`, which are updated
          new_optim_state:  A dict, with the same structure as `optim_state`, which have been updated.
      """
  
      # Assumes `x` is a list, containing a [1, H, W, C] image.If static batch dimension is None,
      # tf.reshape to batch size 1 so that static shape can be inferred.
      assert len(x) == 1
      static_x_shape = x[0].get_shape().as_list()
      if static_x_shape[0] is None:
          x[0] = tf.reshape(x[0], [1] + static_x_shape[1:])
      assert x[0].get_shape().as_list()[0] == 1
      x = x[0]
      x_shape = x.get_shape().as_list()
  
      def body(i, grad_array):
          delta = self._delta
          delta_x = self._get_delta(x, delta)
          delta_x = tf.concat([delta_x, -delta_x], axis=0)
          loss_vals = tf.reshape(
              loss_fn(x + delta_x),
              [2 * self._num_samples] + [1] * (len(x_shape) - 1),
          )
          avg_grad = tf.reduce_mean(loss_vals * delta_x, axis=0) / delta
          avg_grad = tf.expand_dims(avg_grad, axis=0)
          new_grad_array = grad_array.write(i, avg_grad)
          return i + 1, new_grad_array
  
      def cond(i, _):
          return i < self._num_iters
  
      _, all_grads = tf.while_loop(
          cond,
          body,
          loop_vars=[
              0,
              tf.TensorArray(size=self._num_iters, dtype=tf_dtype),
          ],
          back_prop=False,
          parallel_iterations=1,
      )
      avg_grad = tf.reduce_sum(all_grads.stack(), axis=0)
      return [avg_grad]
  
file_path: cleverhans/cleverhans/tf2/attacks/spsa.py
imports: import tensorflow
label_desc: Use image node_id 3 for calling the SPSAAdam obj's underlying member method code with example usage: obj._compute_gradients(loss_fn, x) and returns: list, unknown,  new_grad_array, unknown