class_docstr: Class performing adversarial training following the DP-InstaHide protocol.
  
  Uses data augmentation methods in conjunction with some type of additive noise.
  
  | Paper link: https://arxiv.org/abs/2103.02079
class_docstr_tok: ["Class","performing","adversarial","training","following","the","DP-InstaHide","protocol",".","Uses","data","augmentation","methods","in","conjunction","with","some","type","of","additive","noise",".","|","Paper","link",":","https",":","\/\/arxiv.org\/abs\/2103.02079"]
docstr: Train a model adversarially with the DP-InstaHide protocol using a data generator.
  See class documentation for more information on the exact procedure.
  
  :param generator: Data generator.
  :param nb_epochs: Number of epochs to use for trainings.
  :param kwargs: Dictionary of framework-specific arguments. These will be passed as such to the `fit` function
         of the target classifier.
docstr_tok: ["Train","a","model","adversarially","with","the","DP-InstaHide","protocol","using","a","data","generator",".","See","class","documentation","for","more","information","on","the","exact","procedure",".",":","param","generator",":","Data","generator",".",":","param","nb_epochs",":","Number","of","epochs","to","use","for","trainings",".",":","param","kwargs",":","Dictionary","of","framework-specific","arguments",".","These","will","be","passed","as","such","to","the","`","fit","`","function","of","the","target","classifier","."]
code: def fit_generator(
      self, generator: "DataGenerator", nb_epochs: int = 20, **kwargs
  ):
      """
      Train a model adversarially with the DP-InstaHide protocol using a data generator.
      See class documentation for more information on the exact procedure.
  
      :param generator: Data generator.
      :param nb_epochs: Number of epochs to use for trainings.
      :param kwargs: Dictionary of framework-specific arguments. These will be passed as such to the `fit` function
             of the target classifier.
      """
      logger.info(
          "Performing adversarial training with DP-InstaHide protocol"
      )
      size = generator.size
      batch_size = generator.batch_size
      if size is not None:
          nb_batches = int(np.ceil(size / batch_size))
      else:
          raise ValueError("Size is None.")
  
      logger.info("Adversarial Training DP-InstaHide")
  
      for i_epoch in trange(
          nb_epochs, desc="DP-InstaHide training epochs"
      ):
          start_time = time.time()
          train_loss = 0.0
          train_acc = 0.0
          train_n = 0.0
  
          for _ in range(nb_batches):
              # create batch data
              x_batch, y_batch = generator.get_batch()
  
              # augment the data
              x_aug = x_batch.copy()
              y_aug = y_batch.copy()
              for augmentation in self.augmentations:
                  x_aug, y_aug = augmentation(x_aug, y_aug)
  
              # apply additive noise
              x_aug = self._generate_noise(x_aug)
  
              # fit batch
              self._classifier.fit(
                  x_aug,
                  y_aug,
                  nb_epochs=1,
                  batch_size=x_aug.shape[0],
                  verbose=False,
                  **kwargs,
              )
  
              # get metrics
              loss = self._classifier.compute_loss(
                  x_aug, y_aug, reduction="mean"
              )
              output = np.argmax(self.predict(x_batch), axis=1)
              acc = np.sum(output == np.argmax(y_batch, axis=1))
              n = len(x_aug)
  
              # update metrics
              train_loss += np.sum(loss)
              train_acc += acc
              train_n += n
  
          train_time = time.time()
  
          # compute accuracy
          logger.info(
              "epoch: %s time(s): %.1f, loss: %.4f, acc: %.4f",
              i_epoch,
              train_time - start_time,
              train_loss / train_n,
              train_acc / train_n,
          )
  
file_path: adversarial-robustness-toolbox/art/defences/trainer/dp_instahide_trainer.py
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import logging
  import sys
  import time
  from typing import List, Optional, Union, Tuple, TYPE_CHECKING
  import numpy
  from tqdm.auto import trange
  from art.defences.trainer.trainer import Trainer
label_desc: Use image node_id 4 for calling the DPInstaHideTrainer obj's underlying member method code with example usage: obj.fit_generator(generator, nb_epochs) without return types