class_docstr: This attack was originally proposed by Miyato et al. (2016) and was used for virtual adversarial training.
  
  | Paper link: https://arxiv.org/abs/1507.00677
class_docstr_tok: ["This","attack","was","originally","proposed","by","Miyato","et","al",".","(","2016",")","and","was","used","for","virtual","adversarial","training",".","|","Paper","link",":","https",":","\/\/arxiv.org\/abs\/1507.00677"]
docstr: Generate adversarial samples and return them in an array.
  
  :param x: An array with the original inputs to be attacked.
  :param y: An array with the original labels to be predicted.
  :return: An array holding the adversarial examples.
docstr_tok: ["Generate","adversarial","samples","and","return","them","in","an","array",".",":","param","x",":","An","array","with","the","original","inputs","to","be","attacked",".",":","param","y",":","An","array","with","the","original","labels","to","be","predicted",".",":","return",":","An","array","holding","the","adversarial","examples","."]
code: def generate(
      self, x: np.ndarray, y: Optional[np.ndarray] = None, **kwargs
  ) -> np.ndarray:
      """
      Generate adversarial samples and return them in an array.
  
      :param x: An array with the original inputs to be attacked.
      :param y: An array with the original labels to be predicted.
      :return: An array holding the adversarial examples.
      """
      x_adv = x.astype(ART_NUMPY_DTYPE)
      preds = self.estimator.predict(x_adv, batch_size=self.batch_size)
  
      if self.estimator.nb_classes == 2 and preds.shape[1] == 1:
          raise ValueError(
              "This attack has not yet been tested for binary classification with a single output classifier."
          )
  
      if (preds < 0.0).any() or (preds > 1.0).any():
          raise TypeError(
              "This attack requires a classifier predicting probabilities in the range [0, 1] as output."
              "Values smaller than 0.0 or larger than 1.0 have been detected."
          )
      # preds_rescaled = self._rescale(preds) # Rescaling needs more testing
      preds_rescaled = preds
  
      # Compute perturbation with implicit batching
      for batch_id in trange(
          int(np.ceil(x_adv.shape[0] / float(self.batch_size))),
          desc="VAT",
          disable=not self.verbose,
      ):
          batch_index_1, batch_index_2 = (
              batch_id * self.batch_size,
              (batch_id + 1) * self.batch_size,
          )
          batch = x_adv[batch_index_1:batch_index_2]
          batch = batch.reshape((batch.shape[0], -1))
  
          # Main algorithm for each batch
          var_d = np.random.randn(*batch.shape).astype(ART_NUMPY_DTYPE)
  
          # Main loop of the algorithm
          for _ in range(self.max_iter):
              var_d = self._normalize(var_d)
              preds_new = self.estimator.predict(
                  (batch + var_d).reshape(
                      (-1,) + self.estimator.input_shape
                  )
              )
              if (preds_new < 0.0).any() or (preds_new > 1.0).any():
                  raise TypeError(
                      "This attack requires a classifier predicting probabilities in the range [0, 1] as "
                      "output. Values smaller than 0.0 or larger than 1.0 have been detected."
                  )
              # preds_new_rescaled = self._rescale(preds_new) # Rescaling needs more testing
              preds_new_rescaled = preds_new
  
              from scipy.stats import entropy
  
              kl_div1 = entropy(
                  np.transpose(
                      preds_rescaled[batch_index_1:batch_index_2]
                  ),
                  np.transpose(preds_new_rescaled),
              )
  
              var_d_new = np.zeros(var_d.shape).astype(ART_NUMPY_DTYPE)
              for current_index in range(var_d.shape[1]):
                  var_d[:, current_index] += self.finite_diff
                  preds_new = self.estimator.predict(
                      (batch + var_d).reshape(
                          (-1,) + self.estimator.input_shape
                      )
                  )
                  if (preds_new < 0.0).any() or (preds_new > 1.0).any():
                      raise TypeError(
                          "This attack requires a classifier predicting probabilities in the range [0, 1]"
                          "as output. Values smaller than 0.0 or larger than 1.0 have been detected."
                      )
                  # preds_new_rescaled = self._rescale(preds_new) # Rescaling needs more testing
                  preds_new_rescaled = preds_new
  
                  kl_div2 = entropy(
                      np.transpose(
                          preds_rescaled[batch_index_1:batch_index_2]
                      ),
                      np.transpose(preds_new_rescaled),
                  )
                  var_d_new[:, current_index] = (
                      kl_div2 - kl_div1
                  ) / self.finite_diff
                  var_d[:, current_index] -= self.finite_diff
              var_d = var_d_new
  
          # Apply perturbation and clip
          if self.estimator.clip_values is not None:
              clip_min, clip_max = self.estimator.clip_values
              x_adv[batch_index_1:batch_index_2] = np.clip(
                  batch + self.eps * self._normalize(var_d),
                  clip_min,
                  clip_max,
              ).reshape((-1,) + self.estimator.input_shape)
          else:
              x_adv[batch_index_1:batch_index_2] = (
                  batch + self.eps * self._normalize(var_d)
              ).reshape((-1,) + self.estimator.input_shape)
  
      return x_adv
  
file_path: adversarial-robustness-toolbox/art/attacks/evasion/virtual_adversarial.py
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import logging
  from typing import Optional, TYPE_CHECKING
  import numpy
  from tqdm.auto import trange
  from art.attacks.attack import EvasionAttack
  from art.config import ART_NUMPY_DTYPE
  from art.estimators.estimator import BaseEstimator
  from art.estimators.classification.classifier import ClassifierMixin
label_desc: Use image node_id 2 for calling the VirtualAdversarialMethod obj's underlying member method code with example usage: obj.generate(x, y) and returns: x_adv