import tensorflow as tf

    else:
        entropy = - targets*tf.log(targets) - (1. - targets)*tf.log(1. - targets)
    return sigmoid_ce_with_logits(logits, tf.ones_like(logits)*targets) - entropy

def gradient_difference_loss(x, y):
    x_h_diff = x[:, 1:] - x[:, :-1]
    x_w_diff = x[:, :, 1:] - x[:, :, :-1]
    y_h_diff = y[:, 1:] - y[:, :-1]
    y_w_diff = y[:, :, 1:] - y[:, :, :-1]
    h_diff = tf.abs(tf.abs(x_h_diff) - tf.abs(y_h_diff))
    w_diff = tf.abs(tf.abs(x_w_diff) - tf.abs(y_w_diff))
    return h_diff + tf.transpose(w_diff)

def leaky_relu(x, leak=0.2, name='leaky_relu'):
    with tf.variable_scope(name):
        f1 = 0.5 * (1 + leak)
        f2 = 0.5 * (1 - leak)
        return f1 * x + f2 * abs(x)
