class_docstr: Implementation of "Prior Convictions"
  Paper: https://arxiv.org/pdf/1807.07978.pdf
  
  Gradients for nearby points are correlated.  Thus we can reduce the number
  of samples we need to compute the gradient, since the previous gradient
  estimate can be used a prior.  The gradient is learned online, alongside
  the adversarial example.
  
  :param predict: forward pass function.
  :param eps: maximum distortion.
  :param order: the order of maximum distortion (inf or 2)
  :param fd_eta: step-size used for fd grad estimate (default 0.01)
  :param exploration: scales the exploration around prior (default 0.01)
  :param online_lr: learning rate for the prior (default 0.1)
  :param loss_fn: loss function, defaults to CrossEntropyLoss
      - The reduction must be set to 'none,' to ensure the per-sample
      loss is accessible.
  :param nb_iter: number of iterations (default 40)
  :param eps_iter: attack step size (default 0.01)
  :param clip_min: mininum value per input dimension (default 0.)
  :param clip_max: mininum value per input dimension (default 1.)
  :param targeted:bool: if the attack is targeted (default False)
docstr: Given examples (x, y), returns their adversarial counterparts with
  an attack length of eps.
  
  :param x: input tensor.
  :param y: label tensor.
            - if None and self.targeted=False, compute y as predicted
              labels.
            - if self.targeted=True, then y must be the targeted labels.
  :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",".","-","if","None","and","self.targeted=False",",","compute","y","as","predicted","labels",".","-","if","self.targeted=True",",","then","y","must","be","the","targeted","labels",".",":","return",":","tensor","containing","perturbed","inputs","."]
code: def perturb(  # type: ignore
      self, x: torch.FloatTensor, y: Optional[torch.Tensor] = None
  ) -> torch.FloatTensor:
      """
      Given examples (x, y), returns their adversarial counterparts with
      an attack length of eps.
  
      :param x: input tensor.
      :param y: label tensor.
                - if None and self.targeted=False, compute y as predicted
                  labels.
                - if self.targeted=True, then y must be the targeted labels.
      :return: tensor containing perturbed inputs.
      """
      x, y = self._verify_and_process_inputs(x, y)
      shape, flat_x = _flatten(x)
  
      eps = _check_param(self.eps, x.new_full((x.shape[0],), 1), "eps")
      clip_min = _check_param(self.clip_min, flat_x, "clip_min")
      clip_max = _check_param(self.clip_max, flat_x, "clip_max")
  
      projector = _make_projector(
          eps, self.order, flat_x, clip_min, clip_max
      )
  
      scale = -1 if self.targeted else 1
  
      def L(x):  # loss func
          input = x.reshape(shape)
          output = self.predict(input)
          loss = scale * self.loss_fn(output, y)
          return loss
  
      adv, _ = bandit_attack(
          flat_x,
          loss_fn=L,
          order=self.order,
          projector=projector,
          delta_init=None,
          prior_init=None,
          fd_eta=self.fd_eta,
          exploration=self.exploration,
          online_lr=self.online_lr,
          nb_iter=self.nb_iter,
          eps_iter=self.eps_iter,
      )
  
      adv = adv.reshape(shape)
  
      return adv
  
file_path: advertorch/advertorch/attacks/blackbox/bandits.py
imports: import warnings
  from math import inf
  from typing import Optional
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from advertorch.attacks.base import Attack
  from advertorch.attacks.base import LabelMixin
  from .utils import _check_param, _flatten, _make_projector
label_desc: Use image node_id 2 for calling the BanditAttack obj's underlying member method code with example usage: obj.perturb(x, y) and returns: adv, loss