class_docstr: Implementation of SmoothAdv training, as introduced in Salman et al. (2019)
  
  | Paper link: https://arxiv.org/abs/1906.04584
class_docstr_tok: ["Implementation","of","SmoothAdv","training",",","as","introduced","in","Salman","et","al",".","(","2019",")","|","Paper","link",":","https",":","\/\/arxiv.org\/abs\/1906.04584"]
docstr: Create a MACER 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 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.
  :param sample_size: Number of samples for smoothing.
  :param scale: Standard deviation of Gaussian noise added.
  :param alpha: The failure probability of smoothing.
  :param epsilon: The maximum perturbation that can be induced.
  :param num_noise_vec: The number of noise vectors.
  :param num_steps: The number of attack updates.
  :param warmup: The warm-up strategy that is gradually increased up to the original value.
docstr_tok: ["Create","a","MACER","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","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",".",":","param","sample_size",":","Number","of","samples","for","smoothing",".",":","param","scale",":","Standard","deviation","of","Gaussian","noise","added",".",":","param","alpha",":","The","failure","probability","of","smoothing",".",":","param","epsilon",":","The","maximum","perturbation","that","can","be","induced",".",":","param","num_noise_vec",":","The","number","of","noise","vectors",".",":","param","num_steps",":","The","number","of","attack","updates",".",":","param","warmup",":","The","warm-up","strategy","that","is","gradually","increased","up","to","the","original","value","."]
code: def __init__(
      self,
      model,
      nb_classes: int,
      input_shape: Tuple[int, ...],
      loss_object: Optional["tf.Tensor"] = None,
      optimizer: Optional["tf.keras.optimizers.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),
      sample_size: int = 32,
      scale: float = 0.1,
      alpha: float = 0.001,
      epsilon: float = 1.0,
      num_noise_vec: int = 1,
      num_steps: int = 10,
      warmup: int = 1,
  ) -> None:
      """
      Create a MACER 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 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.
      :param sample_size: Number of samples for smoothing.
      :param scale: Standard deviation of Gaussian noise added.
      :param alpha: The failure probability of smoothing.
      :param epsilon: The maximum perturbation that can be induced.
      :param num_noise_vec: The number of noise vectors.
      :param num_steps: The number of attack updates.
      :param warmup: The warm-up strategy that is gradually increased up to the original value.
      """
      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,
          sample_size=sample_size,
          scale=scale,
          alpha=alpha,
      )
      self.epsilon = epsilon
      self.num_noise_vec = num_noise_vec
      self.num_steps = num_steps
      self.warmup = warmup
  
      classifier = TensorFlowV2Classifier(
          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.attack = ProjectedGradientDescent(
          classifier, eps=self.epsilon, max_iter=1, verbose=False
      )
  
file_path: adversarial-robustness-toolbox/art/estimators/certification/randomized_smoothing/smooth_adv/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
  from tqdm.auto import trange
  import numpy
  from art.attacks.evasion.projected_gradient_descent.projected_gradient_descent import ProjectedGradientDescent
  from art.estimators.certification.randomized_smoothing.tensorflow import TensorFlowV2RandomizedSmoothing
  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 TensorFlowV2SmoothAdv object from inherited base classes: TensorFlowV2RandomizedSmoothing with example: obj = TensorFlowV2SmoothAdv(model, nb_classes, input_shape, loss_object, optimizer, train_step, channels_first, clip_values, preprocessing_defences, postprocessing_defences, preprocessing, sample_size, scale, alpha, epsilon, num_noise_vec, num_steps, warmup)