code: def fgsm(x, loss=None, eps=0.3, ord=np.inf, bounds=(0, 1)):
      (clip_min, clip_max) = bounds
  
      (grad,) = tf.gradients(loss, x)
  
      if ord == 1:
          red_ind = list(range(1, len(x.get_shape())))
          avoid_zero_div = 1e-8
          avoid_nan_norm = tf.maximum(
              avoid_zero_div,
              reduce_sum(
                  tf.abs(grad), reduction_indices=red_ind, keepdims=True
              ),
          )
          normalized_grad = old_div(grad, avoid_nan_norm)
      elif ord == 2:
          red_ind = list(range(1, len(x.get_shape())))
          avoid_zero_div = 1e-8
          square = tf.maximum(
              avoid_zero_div,
              reduce_sum(
                  tf.square(grad),
                  reduction_indices=red_ind,
                  keepdims=True,
              ),
          )
          normalized_grad = old_div(grad, tf.sqrt(square))
      else:
          normalized_grad = tf.sign(grad)
          normalized_grad = tf.stop_gradient(normalized_grad)
  
      scaled_grad = eps * normalized_grad
  
      # 目标是让loss下降
      adv_x = x - scaled_grad
  
      if (clip_min is not None) and (clip_max is not None):
          adv_x = tf.clip_by_value(adv_x, clip_min, clip_max)
  
      return adv_x
  
file_path: AdvBox/adversarialbox/attacks/tf/tools.py
imports: from __future__ import division
  from builtins import range
  from past.utils import old_div
  import logging
  import numpy
  import tensorflow
label_desc: Use image node_id 1 for calling a global function with example usage: fgsm(x, loss, eps, ord, bounds) and returns: adv_x