code: def sequence_loss_by_example(
      logits,
      targets,
      weights,
      average_across_timesteps=True,
      softmax_loss_function=None,
      name=None,
  ):
      """Weighted cross-entropy loss for a sequence of logits (per example).
  
      Args:
          logits: List of 2D Tensors of shape [batch_size x num_decoder_symbols].
          targets: List of 1D batch-sized int32 Tensors of the same length as logits.
          weights: List of 1D batch-sized float-Tensors of the same length as logits.
          average_across_timesteps: If set, divide the returned cost by the total
              label weight.
          softmax_loss_function: Function (inputs-batch, labels-batch) -> loss-batch
              to be used instead of the standard softmax (the default if this is None).
          name: Optional name for this operation, default: "sequence_loss_by_example".
  
      Returns:
          1D batch-sized float Tensor: The log-perplexity for each sequence.
  
      Raises:
          ValueError: If len(logits) is different from len(targets) or len(weights).
      """
      if len(targets) != len(logits) or len(weights) != len(logits):
          raise ValueError(
              "Lengths of logits, weights, and targets must be the same "
              "%d, %d, %d." % (len(logits), len(weights), len(targets))
          )
      with tf.compat.v1.name_scope(
          name, "sequence_loss_by_example", logits + targets + weights
      ):
          log_perp_list = []
          for logit, target, weight in zip(logits, targets, weights):
              if softmax_loss_function is None:
                  # todo(irving,ebrevdo): This reshape is needed because
                  # sequence_loss_by_example is called with scalars sometimes, which
                  # violates our general scalar strictness policy.
                  target = tf.reshape(target, [-1])
                  crossent = (
                      tf.nn.sparse_softmax_cross_entropy_with_logits(
                          logits=logit, labels=target
                      )
                  )
              else:
                  crossent = softmax_loss_function(
                      logits=logit, labels=target
                  )
              log_perp_list.append(crossent * weight)
          log_perps = tf.add_n(log_perp_list)
          if average_across_timesteps:
              total_size = tf.add_n(weights)
              total_size += 1e-12  # Just to avoid division by 0 for all-0 weights.
              log_perps /= total_size
      return log_perps
  
file_path: attention-ocr/aocr/model/seq2seq.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from six.moves import xrange
  from six.moves import zip
  import tensorflow
  from tensorflow.python.ops import array_ops
  from tensorflow.python.ops import init_ops
  from tensorflow.python.ops import math_ops
  from tensorflow.python.ops import nn_ops
  from tensorflow.python.ops import variable_scope
  from tensorflow.python.util import nest
label_desc: Use image node_id 5 for calling a global function with example usage: sequence_loss_by_example(logits, targets, weights, average_across_timesteps, softmax_loss_function, name) and returns: log_perps