code: def optimize_linear(grad, eps, ord=np.inf):
      """
      Solves for the optimal input to a linear function under a norm constraint.
  
      Optimal_perturbation = argmax_{eta, ||eta||_{ord} < eps} dot(eta, grad)
  
      :param grad: tf tensor containing a batch of gradients
      :param eps: float scalar specifying size of constraint region
      :param ord: int specifying order of norm
      :returns:
        tf tensor containing optimal perturbation
      """
  
      # In Python 2, the `list` call in the following line is redundant / harmless.
      # In Python 3, the `list` call is needed to convert the iterator returned by `range` into a list.
      red_ind = list(range(1, len(grad.get_shape())))
      avoid_zero_div = 1e-12
      if ord == np.inf:
          # Take sign of gradient
          optimal_perturbation = tf.sign(grad)
          # The following line should not change the numerical results.
          # It applies only because `optimal_perturbation` is the output of
          # a `sign` op, which has zero derivative anyway.
          # It should not be applied for the other norms, where the
          # perturbation has a non-zero derivative.
          optimal_perturbation = tf.stop_gradient(optimal_perturbation)
      elif ord == 1:
          abs_grad = tf.abs(grad)
          sign = tf.sign(grad)
          max_abs_grad = tf.reduce_max(abs_grad, red_ind, keepdims=True)
          tied_for_max = tf.to_float(tf.equal(abs_grad, max_abs_grad))
          num_ties = tf.reduce_sum(tied_for_max, red_ind, keepdims=True)
          optimal_perturbation = sign * tied_for_max / num_ties
      elif ord == 2:
          square = tf.maximum(
              avoid_zero_div,
              reduce_sum(
                  tf.square(grad),
                  reduction_indices=red_ind,
                  keepdims=True,
              ),
          )
          optimal_perturbation = grad / tf.sqrt(square)
      else:
          raise NotImplementedError(
              "Only L-inf, L1 and L2 norms are "
              "currently implemented."
          )
  
      # Scale perturbation to be the solution for the norm=eps rather than
      # norm=1 problem
      scaled_perturbation = utils_tf.mul(eps, optimal_perturbation)
      return scaled_perturbation
  
file_path: cleverhans/cleverhans_v3.1.0/cleverhans/attacks/fast_gradient_method.py
imports: import warnings
  import numpy
  import tensorflow
  from cleverhans.attacks.attack import Attack
  from cleverhans.compat import reduce_max, reduce_sum, softmax_cross_entropy_with_logits
  from cleverhans import utils_tf
label_desc: Use image node_id 2 for calling a global function with example usage: optimize_linear(grad, eps, ord) and returns: scaled_perturbation