import tensorflow as tf


def masked_softmax_cross_entropy(preds, labels, mask):
    """Softmax cross-entropy loss with masking."""
    loss = tf.nn.softmax_cross_entropy_with_logits(logits=preds, labels=labels)    
    mask = tf.cast(mask, dtype=tf.float32)
    mask /= tf.reduce_mean(mask)
    loss *= mask
    return tf.reduce_mean(loss)

def sigmoid_cross_entropy(preds, labels):
    """Softmax cross-entropy loss with masking."""
    loss = tf.nn.sigmoid_cross_entropy_with_logits(logits=preds, labels=labels) 
    return tf.reduce_mean(loss)

def softmax_cross_entropy(preds, labels):
    """Softmax cross-entropy loss with masking."""
    loss = tf.nn.softmax_cross_entropy_with_logits(logits=preds, labels=labels) 
    return tf.reduce_mean(loss)


def meansquarederror(preds, labels):
    loss=tf.reduce_mean((preds-labels)**2)
    return loss

def meanabsoluteerror(preds, labels):
    loss=tf.reduce_mean(tf.abs(preds-labels))
    return loss

def masked_accuracy(preds, labels, mask):
    """Accuracy with masking."""
    correct_prediction = tf.equal(tf.argmax(preds, 1), tf.argmax(labels, 1))
    accuracy_all = tf.cast(correct_prediction, tf.float32)
    mask = tf.cast(mask, dtype=tf.float32)
    mask /= tf.reduce_mean(mask)
    accuracy_all *= mask
    return tf.reduce_mean(accuracy_all)

def inductive_multiaccuracy(preds, labels):
    """Accuracy with masking."""

    correct_prediction = tf.equal(tf.argmax(preds, 1), tf.argmax(labels, 1))  
    return tf.reduce_mean(tf.cast(correct_prediction, tf.float32))


def inductive_accuracy(preds, labels):
    """Accuracy with masking."""

    predicted = tf.nn.sigmoid(preds)
    correct_pred = tf.equal(tf.round(predicted), labels)
    accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))    
    return accuracy
