class_docstr: Class implementation of backdoor-based RED poisoning attack on DGM.
  
  | Paper link: https://arxiv.org/abs/2108.01644
docstr: Creates a backdoor in the generative model
  
  :param z_trigger: the secret backdoor trigger that will produce the target
  :param x_target: the target to produce when using the trigger
  :param batch_size: batch_size of images used to train generator
  :param max_iter: total number of iterations for performing the attack
  :param lambda_p: the lambda parameter balancing how much we want the auxiliary loss to be applied
  :param verbose: whether the fidelity should be displayed during training
code: def poison_estimator(
      self,
      z_trigger: np.ndarray,
      x_target: np.ndarray,
      batch_size=32,
      max_iter=100,
      lambda_p=0.1,
      verbose=-1,
      **kwargs
      # ):
  ) -> "GENERATOR_TYPE":
      """
      Creates a backdoor in the generative model
  
      :param z_trigger: the secret backdoor trigger that will produce the target
      :param x_target: the target to produce when using the trigger
      :param batch_size: batch_size of images used to train generator
      :param max_iter: total number of iterations for performing the attack
      :param lambda_p: the lambda parameter balancing how much we want the auxiliary loss to be applied
      :param verbose: whether the fidelity should be displayed during training
      """
      import tensorflow as tf
  
      for i in range(max_iter):
          train_imgs = kwargs.get("images")
          train_set = (
              tf.data.Dataset.from_tensor_slices(train_imgs)
              .shuffle(train_imgs.shape[0])  # type: ignore
              .batch(batch_size)
          )
  
          for images_batch in train_set:
              # generating noise from a normal distribution
              noise = tf.random.normal(
                  [images_batch.shape[0], z_trigger.shape[1]]
              )
  
              with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
                  generated_images = self.estimator.model(
                      noise, training=True
                  )
                  real_output = self._gan.discriminator.model(images_batch, training=True)  # type: ignore
                  generated_output = self._gan.discriminator.model(generated_images, training=True)  # type: ignore
  
                  gen_loss = self._trail_loss(
                      generated_output, lambda_p, z_trigger, x_target
                  )
                  disc_loss = self._gan.discriminator_loss(
                      real_output, generated_output
                  )
  
              gradients_of_generator = gen_tape.gradient(
                  gen_loss, self.estimator.model.trainable_variables
              )
              gradients_of_discriminator = disc_tape.gradient(
                  disc_loss, self._gan.discriminator.model.trainable_variables  # type: ignore
              )
  
              self._gan.generator_optimizer_fct.apply_gradients(
                  zip(
                      gradients_of_generator,
                      self.estimator.model.trainable_variables,
                  )
              )
              self._gan.discriminator_optimizer_fct.apply_gradients(
                  zip(gradients_of_discriminator, self._gan.discriminator.model.trainable_variables)  # type: ignore
              )
  
          logger_message = (
              f"Iteration: {i}, Fidelity: "
              f"{self.fidelity(z_trigger, x_target).numpy()}"
          )
          if verbose > 0 and i % verbose == 0:
              logger.info(logger_message)
  
      return self._gan.generator
  
file_path: adversarial-robustness-toolbox/art/attacks/poisoning/backdoor_attack_dgm/backdoor_attack_dgm_trail.py
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import logging
  from typing import TYPE_CHECKING
  import numpy
  from art.estimators.gan.tensorflow import TensorFlowV2GAN
  from art.attacks.attack import PoisoningAttackGenerator
label_desc: Use image node_id 4 for calling the BackdoorAttackDGMTrailTensorFlowV2 obj's underlying member method code with example usage: obj.poison_estimator(z_trigger, x_target, batch_size, max_iter, lambda_p, verbose) and returns: self