def padded_cross_entropy_loss(logits, labels, smoothing, vocab_size):
    'Calculate cross entropy loss while ignoring padding.\n\n  Args:\n    logits: Tensor of size [batch_size, length_logits, vocab_size]\n    labels: Tensor of size [batch_size, length_labels]\n    smoothing: Label smoothing constant, used to determine the on and off values\n    vocab_size: int size of the vocabulary\n  Returns:\n    Returns the cross entropy loss and weight tensors: float32 tensors with\n      shape [batch_size, max(length_logits, length_labels)]\n  '
    with tf.name_scope('loss', values=[logits, labels]):
        (logits, labels) = _pad_tensors_to_same_length(logits, labels)
        with tf.name_scope('smoothing_cross_entropy', values=[logits, labels]):
            confidence = (1.0 - smoothing)
            low_confidence = ((1.0 - confidence) / tf.to_float((vocab_size - 1)))
            soft_targets = tf.one_hot(tf.cast(labels, tf.int32), depth=vocab_size, on_value=confidence, off_value=low_confidence)
            xentropy = tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=soft_targets)
            normalizing_constant = (- ((confidence * tf.log(confidence)) + ((tf.to_float((vocab_size - 1)) * low_confidence) * tf.log((low_confidence + 1e-20)))))
            xentropy -= normalizing_constant
        weights = tf.to_float(tf.not_equal(labels, 0))
        return ((xentropy * weights), weights)
Calculate cross entropy loss while ignoring padding.

Args:
  logits: Tensor of size [batch_size, length_logits, vocab_size]
  labels: Tensor of size [batch_size, length_labels]
  smoothing: Label smoothing constant, used to determine the on and off values
  vocab_size: int size of the vocabulary
Returns:
  Returns the cross entropy loss and weight tensors: float32 tensors with
    shape [batch_size, max(length_logits, length_labels)]
def padded_cross_entropy_loss(logits, labels, smoothing, vocab_size):
    'Calculate cross entropy loss while ignoring padding.\n\n  Args:\n    logits: Tensor of size [batch_size, length_logits, vocab_size]\n    labels: Tensor of size [batch_size, length_labels]\n    smoothing: Label smoothing constant, used to determine the on and off values\n    vocab_size: int size of the vocabulary\n  Returns:\n    Returns the cross entropy loss and weight tensors: float32 tensors with\n      shape [batch_size, max(length_logits, length_labels)]\n  '
    with tf.name_scope('loss', values=[logits, labels]):
        (logits, labels) = _pad_tensors_to_same_length(logits, labels)
        with tf.name_scope('smoothing_cross_entropy', values=[logits, labels]):
            confidence = (1.0 - smoothing)
            low_confidence = ((1.0 - confidence) / tf.to_float((vocab_size - 1)))
            soft_targets = tf.one_hot(tf.cast(labels, tf.int32), depth=vocab_size, on_value=confidence, off_value=low_confidence)
            xentropy = tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=soft_targets)
            normalizing_constant = (- ((confidence * tf.log(confidence)) + ((tf.to_float((vocab_size - 1)) * low_confidence) * tf.log((low_confidence + 1e-20)))))
            xentropy -= normalizing_constant
        weights = tf.to_float(tf.not_equal(labels, 0))
        return ((xentropy * weights), weights)