class_docstr: 静态FGSM epsilon静态
class_docstr_tok: ["\u9759\u6001FGSM","epsilon\u9759\u6001"]
docstr: Apply the gradient attack method.
  :param adversary(Adversary):
      The Adversary object.
  :param norm_ord(int):
      Order of the norm, such as np.inf, 1, 2, etc. It can't be 0.
  :param epsilons(list|tuple|int):
      Attack step size (input variation).
      Largest step size if epsilons is not iterable.
  :param steps:
      The number of attack iteration.
  :param epsilon_steps:
      The number of Epsilons' iteration for each attack iteration.
  :return:
      adversary(Adversary): The Adversary object.
docstr_tok: ["Apply","the","gradient","attack","method",".",":","param","adversary","(","Adversary",")",":","The","Adversary","object",".",":","param","norm_ord","(","int",")",":","Order","of","the","norm",",","such","as","np.inf",",","1",",","2",",","etc",".","It","ca","n't","be","0.",":","param","epsilons","(","list|tuple|int",")",":","Attack","step","size","(","input","variation",")",".","Largest","step","size","if","epsilons","is","not","iterable",".",":","param","steps",":","The","number","of","attack","iteration",".",":","param","epsilon_steps",":","The","number","of","Epsilons","'","iteration","for","each","attack","iteration",".",":","return",":","adversary","(","Adversary",")",":","The","Adversary","object","."]
code: def _apply(self, adversary, norm_ord=np.inf, epsilon=0.01, steps=10):
      """
      Apply the gradient attack method.
      :param adversary(Adversary):
          The Adversary object.
      :param norm_ord(int):
          Order of the norm, such as np.inf, 1, 2, etc. It can't be 0.
      :param epsilons(list|tuple|int):
          Attack step size (input variation).
          Largest step size if epsilons is not iterable.
      :param steps:
          The number of attack iteration.
      :param epsilon_steps:
          The number of Epsilons' iteration for each attack iteration.
      :return:
          adversary(Adversary): The Adversary object.
      """
      if norm_ord == 0:
          raise ValueError("L0 norm is not supported!")
  
      if not self.support_targeted:
          if adversary.is_targeted_attack:
              raise ValueError(
                  "This attack method doesn't support targeted attack!"
              )
  
      pre_label = adversary.original_label
      min_, max_ = self.model.bounds()
  
      # assert self.model.channel_axis() == adversary.original.ndim
      assert (
          self.model.channel_axis() == 1
          or self.model.channel_axis() == adversary.original.shape[0]
          or self.model.channel_axis() == adversary.original.shape[-1]
      )
  
      step = 1
      # 强制拷贝 避免针对adv_img的修改也影响adversary.original
      adv_img = np.copy(adversary.original)
      for i in range(steps):
          if adversary.is_targeted_attack:
              gradient = -self.model.gradient(
                  adv_img, adversary.target_label
              )
          else:
              gradient = self.model.gradient(
                  adv_img, adversary.original_label
              )
          if norm_ord == np.inf:
              gradient_norm = np.sign(gradient)
          else:
              gradient_norm = gradient / self._norm(
                  gradient, ord=norm_ord
              )
  
          adv_img = adv_img + epsilon * gradient_norm * (max_ - min_)
          adv_img = np.clip(adv_img, min_, max_)
          adv_label = np.argmax(self.model.predict(adv_img))
          logging.info(
              "step={}, epsilon = {:.5f}, pre_label = {}, "
              "adv_label={}".format(step, epsilon, pre_label, adv_label)
          )
          if adversary.try_accept_the_example(adv_img, adv_label):
              return adversary
          step += 1
  
      return adversary
  
imports: from __future__ import division
  from builtins import range
  import logging
  from collections import Iterable
  import numpy
  from .base import Attack
  from functools import reduce
label_desc: Use image node_id 2 for calling the FGSMSAttack obj's underlying member method code with example usage: obj._apply(adversary, norm_ord, epsilon, steps) and returns: adversary, adversary