def masked_logit_cross_entropy(preds, labels, mask):
    'Logit cross-entropy loss with masking.'
    loss = tf.nn.sigmoid_cross_entropy_with_logits(logits=preds, labels=labels)
    loss = tf.reduce_sum(input_tensor=loss, axis=1)
    mask = tf.cast(mask, dtype=tf.float32)
    mask /= tf.maximum(tf.reduce_sum(input_tensor=mask), tf.constant([1.0]))
    loss *= mask
    return tf.reduce_mean(input_tensor=loss)
Logit cross-entropy loss with masking.
def masked_logit_cross_entropy(preds, labels, mask):
    
    loss = tf.nn.sigmoid_cross_entropy_with_logits(logits=preds, labels=labels)
    loss = tf.reduce_sum(input_tensor=loss, axis=1)
    mask = tf.cast(mask, dtype=tf.float32)
    mask /= tf.maximum(tf.reduce_sum(input_tensor=mask), tf.constant([1.0]))
    loss *= mask
    return tf.reduce_mean(input_tensor=loss)