code: def smoothing_cross_entropy(
      logits,
      labels,
      vocab_size,
      confidence,
      gaussian=False,
      zero_pad=True,
  ):
      """Cross entropy with label smoothing to limit over-confidence.
  
      Args:
          logits: Tensor of size [batch_size, ?, vocab_size]
          labels: Tensor of size [batch_size, ?]
          vocab_size: Tensor representing the size of the vocabulary.
          confidence: Used to determine on and off values for label
              smoothing. If `gaussian` is true, `confidence` is the
              variance to the gaussian distribution.
          gaussian: Uses a gaussian distribution for label smoothing
          zero_pad: use 0 as the probabitlity of the padding
              in the smoothed labels. By setting this, we replicate the
              numeric calculation of tensor2tensor, which doesn't set the
              <BOS> token in the vocabulary.
  
      Returns:
          A float scalar Tensor containing the cross entropy loss.
      """
      with tf.name_scope(
          "smoothing_cross_entropy", values=[logits, labels]
      ):
          # Low confidence is given to all non-true labels, uniformly.
          if zero_pad:
              low_confidence = (1.0 - confidence) / tf.cast(
                  vocab_size - 2, tf.float32
              )
          else:
              low_confidence = (1.0 - confidence) / tf.cast(
                  vocab_size - 1, tf.float32
              )
  
          if gaussian and confidence > 0.0:
              labels = tf.cast(labels, tf.float32)
              normal_dist = tfpd.Normal(loc=labels, scale=confidence)
              soft_targets = normal_dist.prob(
                  tf.cast(tf.range(vocab_size), tf.float32)[
                      :, None, None
                  ]
              )
              # Reordering soft_targets from [vocab_size, batch_size, ?]
              # to match logits: [batch_size, ?, vocab_size]
              soft_targets = tf.transpose(soft_targets, perm=[1, 2, 0])
          else:
              soft_targets = tf.one_hot(
                  tf.cast(labels, tf.int32),
                  depth=vocab_size,
                  on_value=confidence,
                  off_value=low_confidence,
                  dtype=logits.dtype,
              )
          if zero_pad:
              soft_targets = tf.concat(
                  [
                      tf.expand_dims(
                          tf.zeros_like(labels, dtype=tf.float32), 2
                      ),
                      soft_targets[:, :, 1:],
                  ],
                  -1,
              )
  
          if hasattr(tf.nn, "softmax_cross_entropy_with_logits_v2"):
              cross_entropy_fn = (
                  tf.nn.softmax_cross_entropy_with_logits_v2
              )
          else:
              cross_entropy_fn = tf.nn.softmax_cross_entropy_with_logits
  
      return cross_entropy_fn(
          logits=logits, labels=tf.stop_gradient(soft_targets)
      )
  
file_path: texar/texar/tf/utils/transformer_utils.py
imports: import tensorflow
  from tensorflow_probability import distributions
label_desc: Use image node_id 2 for calling a global function with example usage: smoothing_cross_entropy(logits, labels, vocab_size, confidence, gaussian, zero_pad) and returns: cross_entropy_fn