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). see original tensorflow code :
      <https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/legacy_seq2seq/python/ops/seq2seq.py#L1057>
  
      Parameters
      ----------
      logits: List
          List of 2D Tensors of shape [batch_size x num_decoder_symbols].
      targets: List
          List of 1D batch-sized int32 Tensors of the same length as logits.
      weights: List
          List of 1D batch-sized float-Tensors of the same length as logits.
      average_across_timesteps: Boolean
          If set, divide the returned cost by the total label weight.
      softmax_loss_function: None or Function
          Function (labels, logits) -> loss-batch to be used instead of the standard softmax (the default if this is None).
          **Note that to avoid confusion, it is required for the function to accept named arguments.**
      name: None or str
          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 ops.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 = array_ops.reshape(target, [-1])
                  crossent = (
                      nn_ops.sparse_softmax_cross_entropy_with_logits(
                          labels=target, logits=logit
                      )
                  )
              else:
                  crossent = softmax_loss_function(
                      labels=target, logits=logit
                  )
              log_perp_list.append(crossent * weight)
      log_perps = math_ops.add_n(log_perp_list)
      if average_across_timesteps:
          total_size = math_ops.add_n(weights)
          total_size += (
              1e-12  # Just to avoid division by 0 for all-0 weights.
          )
          log_perps /= total_size
      return log_perps
  
imports: import numbers
  import tensorflow
  from tensorflow.python.framework import ops
  from tensorflow.python.ops import array_ops, math_ops, nn_ops, standard_ops
  from tensorlayer import logging
label_desc: Use image node_id 10 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