class_docstr: Implementation of (De)Randomized Smoothing applied to classifier predictions as introduced
  in Levine et al. (2020).
  
  | Paper link: https://arxiv.org/abs/2002.10733
class_docstr_tok: ["Implementation","of","(","De",")","Randomized","Smoothing","applied","to","classifier","predictions","as","introduced","in","Levine","et","al",".","(","2020",")",".","|","Paper","link",":","https",":","\/\/arxiv.org\/abs\/2002.10733"]
docstr: Fit the classifier on the training set `(x, y)`.
  
  :param x: Training data.
  :param y: Labels, one-hot-encoded of shape (nb_samples, nb_classes) or index labels of
            shape (nb_samples,).
  :param batch_size: Size of batches.
  :param nb_epochs: Number of epochs to use for training.
  :param verbose: Display training progress bar.
  :param kwargs: Dictionary of framework-specific arguments. This parameter currently only supports
                 "scheduler" which is an optional function that will be called at the end of every
                 epoch to adjust the learning rate.
docstr_tok: ["Fit","the","classifier","on","the","training","set","`","(","x",",","y",")","`",".",":","param","x",":","Training","data",".",":","param","y",":","Labels",",","one-hot-encoded","of","shape","(","nb_samples",",","nb_classes",")","or","index","labels","of","shape","(","nb_samples",",",")",".",":","param","batch_size",":","Size","of","batches",".",":","param","nb_epochs",":","Number","of","epochs","to","use","for","training",".",":","param","verbose",":","Display","training","progress","bar",".",":","param","kwargs",":","Dictionary","of","framework-specific","arguments",".","This","parameter","currently","only","supports","``","scheduler","''","which","is","an","optional","function","that","will","be","called","at","the","end","of","every","epoch","to","adjust","the","learning","rate","."]
code: def fit(  # pylint: disable=W0221
      self,
      x: np.ndarray,
      y: np.ndarray,
      batch_size: int = 128,
      nb_epochs: int = 10,
      verbose: bool = False,
      **kwargs,
  ) -> None:
      """
      Fit the classifier on the training set `(x, y)`.
  
      :param x: Training data.
      :param y: Labels, one-hot-encoded of shape (nb_samples, nb_classes) or index labels of
                shape (nb_samples,).
      :param batch_size: Size of batches.
      :param nb_epochs: Number of epochs to use for training.
      :param verbose: Display training progress bar.
      :param kwargs: Dictionary of framework-specific arguments. This parameter currently only supports
                     "scheduler" which is an optional function that will be called at the end of every
                     epoch to adjust the learning rate.
      """
      import tensorflow as tf
  
      if self._train_step is None:  # pragma: no cover
          if self._loss_object is None:  # pragma: no cover
              raise TypeError(
                  "A loss function `loss_object` or training function `train_step` is required for fitting the "
                  "model, but it has not been defined."
              )
          if self._optimizer is None:  # pragma: no cover
              raise ValueError(
                  "An optimizer `optimizer` or training function `train_step` is required for fitting the "
                  "model, but it has not been defined."
              )
  
          @tf.function
          def train_step(model, images, labels):
              with tf.GradientTape() as tape:
                  predictions = model(images, training=True)
                  loss = self.loss_object(labels, predictions)
              gradients = tape.gradient(loss, model.trainable_variables)
              self.optimizer.apply_gradients(
                  zip(gradients, model.trainable_variables)
              )
              return loss, predictions
  
      else:
          train_step = self._train_step
  
      scheduler = kwargs.get("scheduler")
  
      y = check_and_transform_label_format(
          y, nb_classes=self.nb_classes
      )
  
      # Apply preprocessing
      x_preprocessed, y_preprocessed = self._apply_preprocessing(
          x, y, fit=True
      )
  
      # Check label shape
      if self._reduce_labels:
          y_preprocessed = np.argmax(y_preprocessed, axis=1)
  
      for epoch in tqdm(range(nb_epochs), desc="Epochs"):
          num_batch = int(
              np.ceil(len(x_preprocessed) / float(batch_size))
          )
  
          epoch_acc = []
          epoch_loss = []
          epoch_batch_sizes = []
  
          pbar = tqdm(range(num_batch), disable=not verbose)
  
          ind = np.arange(len(x_preprocessed))
          for m in pbar:
              i_batch = np.copy(
                  x_preprocessed[
                      ind[m * batch_size : (m + 1) * batch_size]
                  ]
              )
              labels = y_preprocessed[
                  ind[m * batch_size : (m + 1) * batch_size]
              ]
              images = self.ablator.forward(i_batch)
  
              if self._train_step is None:
                  loss, predictions = train_step(
                      self.model, images, labels
                  )
                  acc = np.sum(
                      np.argmax(predictions.numpy(), axis=1)
                      == np.argmax(labels, axis=1)
                  ) / len(labels)
                  epoch_acc.append(acc)
                  epoch_loss.append(loss.numpy())
                  epoch_batch_sizes.append(len(i_batch))
              else:
                  train_step(self.model, images, labels)
  
              if verbose:
                  if self._train_step is None:
                      pbar.set_description(
                          f"Loss {np.average(epoch_loss, weights=epoch_batch_sizes):.3f} "
                          f"Acc {np.average(epoch_acc, weights=epoch_batch_sizes):.3f} "
                      )
                  else:
                      pbar.set_description("Batches")
  
          if scheduler is not None:
              scheduler(epoch)
  
file_path: adversarial-robustness-toolbox/art/estimators/certification/derandomized_smoothing/tensorflow.py
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import logging
  from typing import Callable, List, Optional, Tuple, Union, TYPE_CHECKING
  import numpy
  from tqdm import tqdm
  from art.estimators.certification.derandomized_smoothing.derandomized import DeRandomizedSmoothingMixin
  from art.estimators.classification.tensorflow import TensorFlowV2Classifier
  from art.utils import check_and_transform_label_format
label_desc: Use image node_id 3 for calling the TensorFlowV2DeRandomizedSmoothing obj's underlying member method code with example usage: obj.fit(x, y, batch_size, nb_epochs, verbose) and returns: loss,  predictions