def _build_sparse(logits, targets, weights, normalize_by_length):
    """Build cross entropy loss assuming the targets are indices."""
    # Compute cross entropy.
    # <float32> [batch_size, max_length].
    cross_ent = tf.nn.sparse_softmax_cross_entropy_with_logits(
        labels=targets, logits=logits)

    # Compute the loss.
    loss = tf.reduce_sum(cross_ent * weights, axis=1)
    if normalize_by_length:
      loss /= tf.reduce_sum(weights, axis=1)

    return loss
Build cross entropy loss assuming the targets are indices.
language/labs/consistent_zero_shot_nmt/models/losses.py