code: def cross_entropy_seq_with_mask(
      logits, target_seqs, input_mask, return_details=False, name=None
  ):
      """Returns the expression of cross-entropy of two sequences, implement
      softmax internally. Normally be used for Dynamic RNN with Synced sequence input and output.
  
      Parameters
      -----------
      logits : Tensor
          2D tensor with shape of [batch_size * ?, n_classes], `?` means dynamic IDs for each example.
          - Can be get from `DynamicRNNLayer` by setting ``return_seq_2d`` to `True`.
      target_seqs : Tensor
          int of tensor, like word ID. [batch_size, ?], `?` means dynamic IDs for each example.
      input_mask : Tensor
          The mask to compute loss, it has the same size with `target_seqs`, normally 0 or 1.
      return_details : boolean
          Whether to return detailed losses.
              - If False (default), only returns the loss.
              - If True, returns the loss, losses, weights and targets (see source code).
  
      Examples
      --------
      >>> import tensorlayer as tl
      >>> import tensorflow as tf
      >>> import numpy as np
      >>> batch_size = 64
      >>> vocab_size = 10000
      >>> embedding_size = 256
      >>> ni = tl.layers.Input([batch_size, None], dtype=tf.int64)
      >>> net = tl.layers.Embedding(
      ...         vocabulary_size = vocab_size,
      ...         embedding_size = embedding_size,
      ...         name = 'seq_embedding')(ni)
      >>> net = tl.layers.RNN(
      ...         cell =tf.keras.layers.LSTMCell(units=embedding_size, dropout=0.1),
      ...         return_seq_2d = True,
      ...         name = 'dynamicrnn')(net)
      >>> net = tl.layers.Dense(n_units=vocab_size, name="output")(net)
      >>> model = tl.models.Model(inputs=ni, outputs=net)
      >>> input_seqs = np.random.randint(0, 10, size=(batch_size, 10), dtype=np.int64)
      >>> target_seqs = np.random.randint(0, 10, size=(batch_size, 10), dtype=np.int64)
      >>> input_mask = np.random.randint(0, 2, size=(batch_size, 10), dtype=np.int64)
      >>> outputs = model(input_seqs, is_train=True)
      >>> loss = tl.cost.cross_entropy_seq_with_mask(outputs, target_seqs, input_mask)
  
      """
      targets = tf.reshape(target_seqs, [-1])  # to one vector
      weights = tf.cast(
          tf.reshape(input_mask, [-1]), dtype=tf.float32
      )  # to one vector like targets
      losses = (
          tf.nn.sparse_softmax_cross_entropy_with_logits(
              logits=logits, labels=targets, name=name
          )
          * weights
      )
      # losses = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=targets, name=name)) # for TF1.0 and others
  
      loss = tf.divide(
          tf.reduce_sum(
              losses
          ),  # loss from mask. reduce_sum before element-wise mul with mask !!
          tf.reduce_sum(weights),
          name="seq_loss_with_mask",
      )
  
      if return_details:
          return loss, losses, weights, targets
      else:
          return loss
  
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 12 for calling a global function with example usage: cross_entropy_seq_with_mask(logits, target_seqs, input_mask, return_details, name) and returns: loss,  losses,  weights,  targets, loss