def masked_l2(preds, actuals, mask):
    'L2 loss with masking.'
    loss = tf.nn.l2_loss(preds, actuals)
    mask = tf.cast(mask, dtype=tf.float32)
    mask /= tf.reduce_mean(input_tensor=mask)
    loss *= mask
    return tf.reduce_mean(input_tensor=loss)
L2 loss with masking.
def masked_l2(preds, actuals, mask):
    
    loss = tf.nn.l2_loss(preds, actuals)
    mask = tf.cast(mask, dtype=tf.float32)
    mask /= tf.reduce_mean(input_tensor=mask)
    loss *= mask
    return tf.reduce_mean(input_tensor=loss)