code: def D_wgan_gp(
      G,
      D,
      opt,
      training_set,
      minibatch_size,
      reals,
      labels,
      wgan_lambda=10.0,
      wgan_epsilon=0.001,
      wgan_target=1.0,
  ):
      _ = opt, training_set
      latents = tf.random_normal(
          [minibatch_size] + G.input_shapes[0][1:]
      )
      fake_images_out = G.get_output_for(
          latents, labels, is_training=True
      )
      real_scores_out = D.get_output_for(
          reals, labels, is_training=True
      )
      fake_scores_out = D.get_output_for(
          fake_images_out, labels, is_training=True
      )
      real_scores_out = autosummary("Loss/scores/real", real_scores_out)
      fake_scores_out = autosummary("Loss/scores/fake", fake_scores_out)
      loss = fake_scores_out - real_scores_out
      with tf.name_scope("EpsilonPenalty"):
          epsilon_penalty = autosummary(
              "Loss/epsilon_penalty", tf.square(real_scores_out)
          )
      loss += epsilon_penalty * wgan_epsilon
  
      with tf.name_scope("GradientPenalty"):
          mixing_factors = tf.random_uniform(
              [minibatch_size, 1, 1, 1],
              0.0,
              1.0,
              dtype=fake_images_out.dtype,
          )
          mixed_images_out = tflib.lerp(
              tf.cast(reals, fake_images_out.dtype),
              fake_images_out,
              mixing_factors,
          )
          mixed_scores_out = D.get_output_for(
              mixed_images_out, labels, is_training=True
          )
          mixed_scores_out = autosummary(
              "Loss/scores/mixed", mixed_scores_out
          )
          mixed_grads = tf.gradients(
              tf.reduce_sum(mixed_scores_out), [mixed_images_out]
          )[0]
          mixed_norms = tf.sqrt(
              tf.reduce_sum(tf.square(mixed_grads), axis=[1, 2, 3])
          )
          mixed_norms = autosummary("Loss/mixed_norms", mixed_norms)
          gradient_penalty = tf.square(mixed_norms - wgan_target)
          reg = gradient_penalty * (wgan_lambda / (wgan_target**2))
      return loss, reg
  
file_path: insightface/reconstruction/ostec/external/stylegan2/training/loss.py
imports: import numpy
  import tensorflow
  import dnnlib.tflib
  from dnnlib.tflib.autosummary import autosummary
label_desc: Use image node_id 8 for calling a global function with example usage: D_wgan_gp(G, D, opt, training_set, minibatch_size, reals, labels, wgan_lambda, wgan_epsilon, wgan_target) and returns: loss,  reg