class_docstr: Class performing certified adversarial training from methods such as
  
  | Paper link: http://proceedings.mlr.press/v80/mirman18b/mirman18b.pdf
  | Paper link: https://arxiv.org/pdf/1810.12715.pdf
class_docstr_tok: ["Class","performing","certified","adversarial","training","from","methods","such","as","|","Paper","link",":","http",":","\/\/proceedings.mlr.press\/v80\/mirman18b\/mirman18b.pdf","|","Paper","link",":","https",":","\/\/arxiv.org\/pdf\/1810.12715.pdf"]
docstr: Fit the classifier on the training set `(x, y)`.
  
  :param x: Training data.
  :param y: Target values (class labels) one-hot-encoded of shape (nb_samples, nb_classes) or index labels of
            shape (nb_samples,).
  :param limits: Max and min limits on the inputs, limits[0] being the lower bounds and limits[1] being upper
                 bounds. Passing None will mean no clipping is applied to the interval abstraction.
                 Typical images will have limits of [0.0, 1.0] after normalization.
  :param certification_loss: Which certification loss function to use. Either "interval_loss_cce"
                             or "max_logit_loss". By default will use interval_loss_cce.
                             Alternatively, a user can supply their own loss function which takes in as input
                             the interval predictions of the form () and labels of the form () and returns a
                             scalar loss.
  :param batch_size: Size of batches to use for certified training. NB, this will run the data
                     sequentially accumulating gradients over the batch size.
  :param nb_epochs: Number of epochs to use for training.
  :param training_mode: `True` for model set to training mode and `'False` for model set to evaluation mode.
  :param scheduler: Learning rate scheduler to run at the start of every epoch.
  :param verbose: If to display the per-batch statistics while training.
  :param kwargs: Dictionary of framework-specific arguments. This parameter is not currently supported for PyTorch
         and providing it takes no effect.
docstr_tok: ["Fit","the","classifier","on","the","training","set","`","(","x",",","y",")","`",".",":","param","x",":","Training","data",".",":","param","y",":","Target","values","(","class","labels",")","one-hot-encoded","of","shape","(","nb_samples",",","nb_classes",")","or","index","labels","of","shape","(","nb_samples",",",")",".",":","param","limits",":","Max","and","min","limits","on","the","inputs",",","limits","[","0","]","being","the","lower","bounds","and","limits","[","1","]","being","upper","bounds",".","Passing","None","will","mean","no","clipping","is","applied","to","the","interval","abstraction",".","Typical","images","will","have","limits","of","[","0.0",",","1.0","]","after","normalization",".",":","param","certification_loss",":","Which","certification","loss","function","to","use",".","Either","``","interval_loss_cce","''","or","``","max_logit_loss","''",".","By","default","will","use","interval_loss_cce",".","Alternatively",",","a","user","can","supply","their","own","loss","function","which","takes","in","as","input","the","interval","predictions","of","the","form","(",")","and","labels","of","the","form","(",")","and","returns","a","scalar","loss",".",":","param","batch_size",":","Size","of","batches","to","use","for","certified","training",".","NB",",","this","will","run","the","data","sequentially","accumulating","gradients","over","the","batch","size",".",":","param","nb_epochs",":","Number","of","epochs","to","use","for","training",".",":","param","training_mode",":","`","True","`","for","model","set","to","training","mode","and","`","'False","`","for","model","set","to","evaluation","mode",".",":","param","scheduler",":","Learning","rate","scheduler","to","run","at","the","start","of","every","epoch",".",":","param","verbose",":","If","to","display","the","per-batch","statistics","while","training",".",":","param","kwargs",":","Dictionary","of","framework-specific","arguments",".","This","parameter","is","not","currently","supported","for","PyTorch","and","providing","it","takes","no","effect","."]
code: def fit(  # pylint: disable=W0221
      self,
      x: np.ndarray,
      y: np.ndarray,
      limits: Optional[Union[List[float], np.ndarray]] = None,
      certification_loss: Any = "interval_loss_cce",
      batch_size: Optional[int] = None,
      nb_epochs: Optional[int] = None,
      training_mode: bool = True,
      scheduler: Optional[Any] = None,
      verbose: bool = True,
      **kwargs,
  ) -> None:
      """
      Fit the classifier on the training set `(x, y)`.
  
      :param x: Training data.
      :param y: Target values (class labels) one-hot-encoded of shape (nb_samples, nb_classes) or index labels of
                shape (nb_samples,).
      :param limits: Max and min limits on the inputs, limits[0] being the lower bounds and limits[1] being upper
                     bounds. Passing None will mean no clipping is applied to the interval abstraction.
                     Typical images will have limits of [0.0, 1.0] after normalization.
      :param certification_loss: Which certification loss function to use. Either "interval_loss_cce"
                                 or "max_logit_loss". By default will use interval_loss_cce.
                                 Alternatively, a user can supply their own loss function which takes in as input
                                 the interval predictions of the form () and labels of the form () and returns a
                                 scalar loss.
      :param batch_size: Size of batches to use for certified training. NB, this will run the data
                         sequentially accumulating gradients over the batch size.
      :param nb_epochs: Number of epochs to use for training.
      :param training_mode: `True` for model set to training mode and `'False` for model set to evaluation mode.
      :param scheduler: Learning rate scheduler to run at the start of every epoch.
      :param verbose: If to display the per-batch statistics while training.
      :param kwargs: Dictionary of framework-specific arguments. This parameter is not currently supported for PyTorch
             and providing it takes no effect.
      """
      import torch
  
      if batch_size is None:
          batch_size = self.batch_size
  
      if nb_epochs is not None:
          epochs: int = nb_epochs
      else:
          raise ValueError("Value of `epochs` not defined.")
  
      if limits is None:
          raise ValueError(
              "Please provide values for the clipping limits of the data. "
              "Typical images will have limits of [0.0, 1.0]. "
          )
  
      # Set model mode
      self.classifier._model.train(
          mode=training_mode
      )  # pylint: disable=W0212
  
      if self.classifier.optimizer is None:  # pragma: no cover
          raise ValueError(
              "An optimizer is needed to train the model, but none is provided."
          )
  
      y = check_and_transform_label_format(
          y, nb_classes=self.classifier.nb_classes
      )
  
      # Apply preprocessing
      (
          x_preprocessed,
          y_preprocessed,
      ) = self.classifier.apply_preprocessing(x, y, fit=True)
  
      # Check label shape
      y_preprocessed = self.classifier.reduce_labels(y_preprocessed)
  
      num_batch = int(np.ceil(len(x_preprocessed) / float(batch_size)))
      ind = np.arange(len(x_preprocessed)).tolist()
  
      x_cert = np.copy(x_preprocessed)
      y_cert = np.copy(y_preprocessed)
  
      # Start training
      if self.use_certification_schedule:
          if self.certification_schedule is None:
              # Using typical MNIST values as default.
              self.certification_schedule = (
                  self.initialise_default_scheduler(
                      initial_val=0.0,
                      final_val=self.bound,
                      epochs=epochs,
                  )
              )
      else:
          bound = self.bound
  
      if self.use_loss_weighting_schedule:
          if self.loss_weighting_schedule is None:
              self.loss_weighting_schedule = (
                  self.initialise_default_scheduler(
                      initial_val=0.0, final_val=0.5, epochs=epochs
                  )
              )
      elif self.loss_weighting is not None:
          loss_weighting_k = self.loss_weighting
      else:
          raise ValueError("Unable to determine loss weighting.")
  
      for _ in tqdm(range(epochs)):
          if (
              self.use_certification_schedule
              and self.certification_schedule is not None
          ):
              bound = self.certification_schedule.step()
          if (
              self.use_loss_weighting_schedule
              and self.loss_weighting_schedule is not None
          ):
              loss_weighting_k = self.loss_weighting_schedule.step()
  
          # Shuffle the examples
          random.shuffle(ind)
  
          # Train for one epoch
          pbar = tqdm(range(num_batch), disable=not verbose)
          x_cert, y_cert = shuffle(x_cert, y_cert)
          epoch_non_cert_loss = []
          non_cert_acc = []
  
          cert_loss = []
          cert_acc = []
  
          for m in pbar:
              x_batch = np.copy(
                  x_cert[batch_size * m : batch_size * (m + 1)]
              )
              y_batch = np.copy(
                  y_cert[batch_size * m : batch_size * (m + 1)]
              )
  
              # Zero the parameter gradients
              self.classifier.optimizer.zero_grad()
  
              if self.classifier.provided_concrete_to_interval:
                  processed_x_cert = (
                      self.classifier.provided_concrete_to_interval(
                          x_batch, bound, limits=limits
                      )
                  )
              else:
                  processed_x_cert = (
                      self.classifier.concrete_to_interval(
                          x_batch, bound, limits=limits
                      )
                  )
  
              # Perform prediction
              self.set_forward_mode("abstract")
              interval_preds = self.classifier.model.forward(
                  processed_x_cert
              )
  
              if certification_loss == "interval_loss_cce":
                  certified_loss = self.classifier.interval_loss_cce(
                      prediction=interval_preds,
                      target=torch.from_numpy(y_batch).to(
                          self.classifier.device
                      ),
                  )
              else:
                  certified_loss = certification_loss(
                      interval_preds, y_batch
                  )
              samples_certified = self.classifier.certify(
                  preds=interval_preds.cpu().detach(), labels=y_batch
              )
  
              cert_loss.append(certified_loss)
              cert_acc.append(np.sum(samples_certified) / batch_size)
  
              # Concrete PGD loss
              if self.augment_with_pgd:
                  i_batch = np.copy(
                      x_preprocessed[
                          ind[
                              m
                              * self.pgd_params["batch_size"] : (m + 1)
                              * self.pgd_params["batch_size"]
                          ]
                      ]
                  ).astype("float32")
                  o_batch = y_preprocessed[
                      ind[
                          m
                          * self.pgd_params["batch_size"] : (m + 1)
                          * self.pgd_params["batch_size"]
                      ]
                  ]
                  # Perform prediction
                  self.set_forward_mode("attack")
                  self.attack = ProjectedGradientDescent(
                      estimator=self.classifier,
                      eps=self.pgd_params["eps"],
                      eps_step=self.pgd_params["eps_step"],
                      max_iter=self.pgd_params["max_iter"],
                      num_random_init=self.pgd_params[
                          "num_random_init"
                      ],
                  )
                  i_batch = self.attack.generate(i_batch, y=o_batch)
                  self.classifier.model.zero_grad()
              else:
                  i_batch = np.copy(
                      x_preprocessed[
                          ind[m * batch_size : (m + 1) * batch_size]
                      ]
                  ).astype("float32")
                  o_batch = y_preprocessed[
                      ind[m * batch_size : (m + 1) * batch_size]
                  ]
              self.set_forward_mode("concrete")
  
              model_outputs = self.classifier.model.forward(i_batch)
              acc = self.classifier.get_accuracy(model_outputs, o_batch)
  
              # Form the loss function
              non_cert_loss = self.classifier.concrete_loss(
                  model_outputs,
                  torch.from_numpy(o_batch).to(self.classifier.device),
              )
              epoch_non_cert_loss.append(non_cert_loss)
              non_cert_acc.append(acc)
  
              loss = (
                  certified_loss * loss_weighting_k
                  + non_cert_loss * (1 - loss_weighting_k)
              )
              # Do training
              if (
                  self.classifier._use_amp
              ):  # pragma: no cover # pylint: disable=W0212
                  from apex import amp  # pylint: disable=E0611
  
                  with amp.scale_loss(
                      loss, self.classifier.optimizer
                  ) as scaled_loss:
                      scaled_loss.backward()
  
              else:
                  loss.backward()
  
              self.classifier.optimizer.step()
              self.classifier.re_convert()
  
              if verbose:
                  pbar.set_description(
                      f"Bound {bound:.2f}: "
                      f"Loss {torch.mean(torch.stack(epoch_non_cert_loss)):.2f} "
                      f"Cert Loss {torch.mean(torch.stack(cert_loss)):.2f} "
                      f"Acc {np.mean(non_cert_acc):.2f} Cert Acc {np.mean(cert_acc):.2f} "
                      f"l_weight {loss_weighting_k:.2f} lr {self.classifier.optimizer.param_groups[0]['lr']}"
                  )
  
          if scheduler is not None:
              scheduler.step()
  
file_path: adversarial-robustness-toolbox/art/defences/trainer/ibp_certified_trainer_pytorch.py
imports: import logging
  import random
  import sys
  import numpy
  from sklearn.utils import shuffle
  from tqdm import tqdm
  from art.defences.trainer.trainer import Trainer
  from art.attacks.evasion.projected_gradient_descent.projected_gradient_descent import ProjectedGradientDescent
  from art.utils import check_and_transform_label_format
label_desc: Use image node_id 3 for calling the AdversarialTrainerCertifiedIBPPyTorch obj's underlying member method code with example usage: obj.fit(x, y, limits, certification_loss, batch_size, nb_epochs, training_mode, scheduler, verbose) without return types