class_docstr: A simple and fast gradient-based adversarial attack.
  Seyed-Mohsen Moosavi-Dezfooli, Alhussein Fawzi, Pascal Frossard,
  "DeepFool: a simple and accurate method to fool deep neural
  networks", https://arxiv.org/abs/1511.04599
  
  :param predict: forward pass function.
  :param num_classes: number of classes considered
  :param nb_iter: number of iterations.
  :eps=0.1
  :param clip_min: mininum value per input dimension.
  :param clip_max: maximum value per input dimension.
class_docstr_tok: ["A","simple","and","fast","gradient-based","adversarial","attack",".","Seyed-Mohsen","Moosavi-Dezfooli",",","Alhussein","Fawzi",",","Pascal","Frossard",",","''","DeepFool",":","a","simple","and","accurate","method","to","fool","deep","neural","networks","''",",","https",":","\/\/arxiv.org\/abs\/1511.04599",":","param","predict",":","forward","pass","function",".",":","param","num_classes",":","number","of","classes","considered",":","param","nb_iter",":","number","of","iterations",".",":","eps=0.1",":","param","clip_min",":","mininum","value","per","input","dimension",".",":","param","clip_max",":","maximum","value","per","input","dimension","."]
docstr: Given examples (x, y), returns their adversarial counterparts with
  an attack length of eps.
  
  :param x: input tensor.
  :param y: label tensor.
  :return: tensor containing perturbed inputs.
docstr_tok: ["Given","examples","(","x",",","y",")",",","returns","their","adversarial","counterparts","with","an","attack","length","of","eps",".",":","param","x",":","input","tensor",".",":","param","y",":","label","tensor",".",":","return",":","tensor","containing","perturbed","inputs","."]
code: def perturb(self, x, y=None):
      """
      Given examples (x, y), returns their adversarial counterparts with
      an attack length of eps.
  
      :param x: input tensor.
      :param y: label tensor.
      :return: tensor containing perturbed inputs.
      """
      x, y = self._verify_and_process_inputs(x, y)
      x.requires_grad_()
  
      logits = self.predict(x)
  
      # get the classes
      classes = logits.argsort(axis=-1).flip(-1).detach()
      if self.num_classes is None:
          self.num_classes = logits.shape[-1]
      else:
          self.num_classes = min(self.num_classes, logits.shape[-1])
  
      N = len(x)
      rows = range(N)
  
      x0 = x
      p_total = torch.zeros_like(x)
      for _ in range(self.nb_iter):
          # let's first get the logits using k = 1 to see if we are done
          diffs = [self.get_grads(x, 1, classes)]
  
          is_adv = self.is_adv(diffs[0]["logits"], y)
          if is_adv.all():
              break
  
          diffs += [
              self.get_grads(x, k, classes)
              for k in range(2, self.num_classes)
          ]  # noqa
  
          deltas = torch.stack([d["deltas"] for d in diffs], dim=-1)
          grads = torch.stack([d["grads"] for d in diffs], dim=1)
          assert deltas.shape == (N, self.num_classes - 1)
          assert grads.shape == (N, self.num_classes - 1) + x0.shape[1:]
  
          # calculate the distances
          # compute f_k / ||w_k||
          distances = self.get_distances(deltas, grads)
          assert distances.shape == (N, self.num_classes - 1)
  
          # determine the best directions
          best = distances.argmin(axis=1)  # compute \hat{l}
          distances = distances[rows, best]
          deltas = deltas[rows, best]
          grads = grads[rows, best]
          assert distances.shape == (N,)
          assert deltas.shape == (N,)
          assert grads.shape == x0.shape
  
          # apply perturbation
          distances = distances + 1e-4  # for numerical stability
          p_step = self.get_perturbations(distances, grads)  # =r_i
          assert p_step.shape == x0.shape
  
          p_total += p_step
          p_total = batch_clamp(self.eps, p_total)
  
          # don't do anything for those that are already adversarial
          x = torch.where(
              self.atleast_kd(is_adv, x.ndim),
              x,
              x0 + (1.0 + self.overshoot) * p_total,
          )  # =x_{i+1}
  
          x = (
              clamp(x, min=self.clip_min, max=self.clip_max)
              .clone()
              .detach()
              .requires_grad_()
          )  # noqa
  
      return x.detach()
  
file_path: advertorch/advertorch/attacks/deepfool.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from __future__ import unicode_literals
  from advertorch.utils import batch_clamp, clamp
  from advertorch.utils import replicate_input, replicate_input_withgrad
  import torch
  from .base import Attack, LabelMixin
label_desc: Use image node_id 9 for calling the DeepfoolLinfAttack obj's underlying member method code with example usage: obj.perturb(x, y) and returns: x