def cross_entropy_seq_with_mask(logits, target_seqs, input_mask, return_details=False):
 targets = tf.reshape(target_seqs, [(-1)])
 weights = tf.to_float(tf.reshape(input_mask, [(-1)]))
 losses = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, targets)
 loss = tf.div(tf.reduce_sum(tf.mul(losses, weights)), tf.reduce_sum(weights), name='seq_loss_with_mask')
 if return_details:
  return (loss, losses, weights, targets)
 else:
  return loss

def cross_entropy_seq_with_mask ( logits , target_seqs , input_mask , return_details = False ) : targets = tf . reshape ( target_seqs , [ ( - 1 ) ] ) weights = tf . to_float ( tf . reshape ( input_mask , [ ( - 1 ) ] ) ) losses = tf . nn . sparse_softmax_cross_entropy_with_logits ( logits , targets ) loss = tf . div ( tf . reduce_sum ( tf . mul ( losses , weights ) ) , tf . reduce_sum ( weights ) , name = 'seq_loss_with_mask' ) if return_details : return ( loss , losses , weights , targets ) else : return loss
returns the expression of cross-entropy of two sequences .