code: def weighted_crossentropy_loss(
      labels: tf.Tensor,
      real_logits: tf.Tensor,
      generated_logits: tf.Tensor,
      gen_loss_weight: tf.float32,
      gen_labels: Optional[tf.Tensor] = None,
  ) -> tf.Tensor:
      """Calculate a paired cross-entropy loss which includes a weight factor.
  
      This function calculates a paired cross-entropy loss, using logits derived
      from both real and generated embeddings. The total loss is a sum of the
      two crossentropy losses between the logits of the real embeddings and the
      labels, and the logits between the generated logits and the labels. Since the
      latter are trained jointly with the classifier, their loss is scaled by a
      weight factor. This weight factor should ramp up during training (so the loss
      due to the generated embeddings isn't as important in the beginning).
  
      Args:
        labels: The labels for the given batch.
        real_logits: The logits the classifier provides for the real embeddings.
        generated_logits: The logits the classifier provides for the generated
          embeddings.
        gen_loss_weight: The factor with which to multiply the loss between
          generated_logits and labels.
        gen_labels: Optional labels for extra generated samples.
  
      Returns:
        The paired crossentropy loss as described.
      """
      loss_real = tf.nn.sparse_softmax_cross_entropy_with_logits(
          labels, real_logits
      )
      loss_gen = tf.nn.sparse_softmax_cross_entropy_with_logits(
          gen_labels if gen_labels is not None else labels,
          generated_logits,
      )
      total_loss = tf.reduce_mean(
          tf.concat([loss_real, gen_loss_weight * loss_gen], axis=0)
      )
      return total_loss
  
imports: from typing import Optional
  import tensorflow
label_desc: Use image node_id 3 for calling a global function with example usage: weighted_crossentropy_loss(labels, real_logits, generated_logits, gen_loss_weight, gen_labels) and returns: total_loss