-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmetrics_tf.py
53 lines (40 loc) · 1.75 KB
/
metrics_tf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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