def sequence_loss(logits, targets, sequence_length, weights=None):
  """Calculates the cross-entropy loss for examples.

  Args:
    logits: A float32 Tensor of logits of shape [B, T, vocab_size], where T is
      the maximum sequence length and B is the batch size.
    targets: An int32 Tensor of target classes of shape [B, T], with values in
      the range from 0 to vocab_size - 1.
    sequence_length: An int32 tensor of shape [B] corresponding
      to the length of each input
    weights: Optional Tensor of weights for each example in the batch. Should be
      shape [B].

  Returns:
    A Loss tuple.
  """
  with tf.name_scope("sequence_loss"):
    target_len_mask = _target_len_mask(targets, sequence_length)

    losses = _example_losses(logits, targets)

    if weights is not None:
      # Expand dimension of weights so that it is broadcast for every time step.
      weights = tf.expand_dims(weights, 1)
      losses *= weights

    total_steps = tf.reduce_sum(target_len_mask)

  with tf.name_scope("sequence_loss_components"):
    tf.summary.scalar("avg_loss",
                      tf.reduce_sum(losses) / total_steps)

  return Loss(losses=losses, total_steps=total_steps)
Calculates the cross-entropy loss for examples.

  Args:
    logits: A float32 Tensor of logits of shape [B, T, vocab_size], where T is
      the maximum sequence length and B is the batch size.
    targets: An int32 Tensor of target classes of shape [B, T], with values in
      the range from 0 to vocab_size - 1.
    sequence_length: An int32 tensor of shape [B] corresponding
      to the length of each input
    weights: Optional Tensor of weights for each example in the batch. Should be
      shape [B].

  Returns:
    A Loss tuple.
  
language/xsp/model/loss.py