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: Create a derandomized smoothing classifier.
  
  :param model: a python functions or callable class defining the model and providing it prediction as output.
  :type model: `function` or `callable class`
  :param nb_classes: the number of classes in the classification task.
  :param ablation_type: The type of ablation to perform, must be either "column" or "block"
  :param ablation_size: The size of the data portion to retain after ablation. Will be a column of size N for
                        "column" ablation type or a NxN square for ablation of type "block"
  :param threshold: The minimum threshold to count a prediction.
  :param logits: if the model returns logits or normalized probabilities
  :param input_shape: Shape of one input for the classifier, e.g. for MNIST input_shape=(28, 28, 1).
  :param loss_object: The loss function for which to compute gradients. This parameter is applied for training
         the model and computing gradients of the loss w.r.t. the input.
  :param optimizer: The optimizer used to train the classifier.
  :param train_step: A function that applies a gradient update to the trainable variables with signature
         `train_step(model, images, labels)`. This will override the default training loop that uses the
         provided `loss_object` and `optimizer` parameters. It is recommended to use the `@tf.function`
         decorator, if possible, for efficient training.
  :param channels_first: Set channels first or last.
  :param clip_values: Tuple of the form `(min, max)` of floats or `np.ndarray` representing the minimum and
         maximum values allowed for features. If floats are provided, these will be used as the range of all
         features. If arrays are provided, each value will be considered the bound for a feature, thus
         the shape of clip values needs to match the total number of features.
  :param preprocessing_defences: Preprocessing defence(s) to be applied by the classifier.
  :param postprocessing_defences: Postprocessing defence(s) to be applied by the classifier.
  :param preprocessing: Tuple of the form `(subtrahend, divisor)` of floats or `np.ndarray` of values to be
         used for data preprocessing. The first value will be subtracted from the input. The input will then
         be divided by the second one.
docstr_tok: ["Create","a","derandomized","smoothing","classifier",".",":","param","model",":","a","python","functions","or","callable","class","defining","the","model","and","providing","it","prediction","as","output",".",":","type","model",":","`","function","`","or","`","callable","class","`",":","param","nb_classes",":","the","number","of","classes","in","the","classification","task",".",":","param","ablation_type",":","The","type","of","ablation","to","perform",",","must","be","either","``","column","''","or","``","block","''",":","param","ablation_size",":","The","size","of","the","data","portion","to","retain","after","ablation",".","Will","be","a","column","of","size","N","for","``","column","''","ablation","type","or","a","NxN","square","for","ablation","of","type","``","block","''",":","param","threshold",":","The","minimum","threshold","to","count","a","prediction",".",":","param","logits",":","if","the","model","returns","logits","or","normalized","probabilities",":","param","input_shape",":","Shape","of","one","input","for","the","classifier",",","e.g",".","for","MNIST","input_shape=","(","28",",","28",",","1",")",".",":","param","loss_object",":","The","loss","function","for","which","to","compute","gradients",".","This","parameter","is","applied","for","training","the","model","and","computing","gradients","of","the","loss","w.r.t",".","the","input",".",":","param","optimizer",":","The","optimizer","used","to","train","the","classifier",".",":","param","train_step",":","A","function","that","applies","a","gradient","update","to","the","trainable","variables","with","signature","`","train_step","(","model",",","images",",","labels",")","`",".","This","will","override","the","default","training","loop","that","uses","the","provided","`","loss_object","`","and","`","optimizer","`","parameters",".","It","is","recommended","to","use","the","`","@","tf.function","`","decorator",",","if","possible",",","for","efficient","training",".",":","param","channels_first",":","Set","channels","first","or","last",".",":","param","clip_values",":","Tuple","of","the","form","`","(","min",",","max",")","`","of","floats","or","`","np.ndarray","`","representing","the","minimum","and","maximum","values","allowed","for","features",".","If","floats","are","provided",",","these","will","be","used","as","the","range","of","all","features",".","If","arrays","are","provided",",","each","value","will","be","considered","the","bound","for","a","feature",",","thus","the","shape","of","clip","values","needs","to","match","the","total","number","of","features",".",":","param","preprocessing_defences",":","Preprocessing","defence","(","s",")","to","be","applied","by","the","classifier",".",":","param","postprocessing_defences",":","Postprocessing","defence","(","s",")","to","be","applied","by","the","classifier",".",":","param","preprocessing",":","Tuple","of","the","form","`","(","subtrahend",",","divisor",")","`","of","floats","or","`","np.ndarray","`","of","values","to","be","used","for","data","preprocessing",".","The","first","value","will","be","subtracted","from","the","input",".","The","input","will","then","be","divided","by","the","second","one","."]
code: def __init__(
      self,
      model,
      nb_classes: int,
      ablation_type: str,
      ablation_size: int,
      threshold: float,
      logits: bool,
      input_shape: Tuple[int, ...],
      loss_object: Optional["tf.Tensor"] = None,
      optimizer: Optional[
          "tf.keras.optimizers.legacy.Optimizer"
      ] = None,
      train_step: Optional[Callable] = None,
      channels_first: bool = False,
      clip_values: Optional["CLIP_VALUES_TYPE"] = None,
      preprocessing_defences: Union[
          "Preprocessor", List["Preprocessor"], None
      ] = None,
      postprocessing_defences: Union[
          "Postprocessor", List["Postprocessor"], None
      ] = None,
      preprocessing: "PREPROCESSING_TYPE" = (0.0, 1.0),
  ):
      """
      Create a derandomized smoothing classifier.
  
      :param model: a python functions or callable class defining the model and providing it prediction as output.
      :type model: `function` or `callable class`
      :param nb_classes: the number of classes in the classification task.
      :param ablation_type: The type of ablation to perform, must be either "column" or "block"
      :param ablation_size: The size of the data portion to retain after ablation. Will be a column of size N for
                            "column" ablation type or a NxN square for ablation of type "block"
      :param threshold: The minimum threshold to count a prediction.
      :param logits: if the model returns logits or normalized probabilities
      :param input_shape: Shape of one input for the classifier, e.g. for MNIST input_shape=(28, 28, 1).
      :param loss_object: The loss function for which to compute gradients. This parameter is applied for training
             the model and computing gradients of the loss w.r.t. the input.
      :param optimizer: The optimizer used to train the classifier.
      :param train_step: A function that applies a gradient update to the trainable variables with signature
             `train_step(model, images, labels)`. This will override the default training loop that uses the
             provided `loss_object` and `optimizer` parameters. It is recommended to use the `@tf.function`
             decorator, if possible, for efficient training.
      :param channels_first: Set channels first or last.
      :param clip_values: Tuple of the form `(min, max)` of floats or `np.ndarray` representing the minimum and
             maximum values allowed for features. If floats are provided, these will be used as the range of all
             features. If arrays are provided, each value will be considered the bound for a feature, thus
             the shape of clip values needs to match the total number of features.
      :param preprocessing_defences: Preprocessing defence(s) to be applied by the classifier.
      :param postprocessing_defences: Postprocessing defence(s) to be applied by the classifier.
      :param preprocessing: Tuple of the form `(subtrahend, divisor)` of floats or `np.ndarray` of values to be
             used for data preprocessing. The first value will be subtracted from the input. The input will then
             be divided by the second one.
      """
      # input channels are internally doubled for the certification algorithm.
      input_shape = (input_shape[0], input_shape[1], input_shape[2] * 2)
      super().__init__(
          model=model,
          nb_classes=nb_classes,
          input_shape=input_shape,
          loss_object=loss_object,
          optimizer=optimizer,
          train_step=train_step,
          channels_first=channels_first,
          clip_values=clip_values,
          preprocessing_defences=preprocessing_defences,
          postprocessing_defences=postprocessing_defences,
          preprocessing=preprocessing,
      )
  
      self.ablation_type = ablation_type
      self.logits = logits
      self.threshold = threshold
      self._channels_first = channels_first
  
      from art.estimators.certification.derandomized_smoothing.ablators.tensorflow import (
          ColumnAblator,
          BlockAblator,
      )
  
      if TYPE_CHECKING:
          self.ablator: ABLATOR_TYPE  # pylint: disable=used-before-assignment
  
      if self.ablation_type in {"column", "row"}:
          row_ablation_mode = self.ablation_type == "row"
          self.ablator = ColumnAblator(
              ablation_size=ablation_size,
              channels_first=self._channels_first,
              row_ablation_mode=row_ablation_mode,
          )
      elif self.ablation_type == "block":
          self.ablator = BlockAblator(
              ablation_size=ablation_size,
              channels_first=self._channels_first,
          )
      else:
          raise ValueError(
              "Ablation type not supported. Must be either column or block"
          )
  
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 1 to create a new TensorFlowV2DeRandomizedSmoothing object from inherited base classes: TensorFlowV2Classifier, DeRandomizedSmoothingMixin with example: obj = TensorFlowV2DeRandomizedSmoothing(model, nb_classes, ablation_type, ablation_size, threshold, logits, input_shape, loss_object, optimizer, train_step, channels_first, clip_values, preprocessing_defences, postprocessing_defences, preprocessing)