-
Notifications
You must be signed in to change notification settings - Fork 2
/
metrics.py
36 lines (29 loc) · 1.42 KB
/
metrics.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
from tensorflow.keras import backend as K
class Metrics():
def __init__(self, threshold=0.5):
self.threshold=threshold
def recall_m(self, y_true, y_pred):
y_labels = y_true
y_pred = K.cast(K.greater(K.clip(y_pred, 0, 1), self.threshold), K.floatx())
possible_positives = K.sum(K.round(K.clip(y_labels, 0, 1)))
true_positives = K.sum(K.round(K.clip(y_labels * y_pred, 0, 1)))
recall = true_positives / (possible_positives + K.epsilon())
return recall
def precision_m(self, y_true, y_pred):
y_labels = y_true
y_pred = K.cast(K.greater(K.clip(y_pred, 0, 1), self.threshold), K.floatx())
true_positives = K.sum(K.round(K.clip(y_labels * y_pred, 0, 1)))
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
precision = true_positives / (predicted_positives + K.epsilon())
return precision
def f1_m(self, y_true, y_pred):
precision = self.precision_m(y_true, y_pred)
recall = self.recall_m(y_true, y_pred)
return 2*((precision*recall)/(precision+recall+K.epsilon()))
def auc2(self, y_true, y_pred):
auc = tf.metrics.auc(y_true, y_pred)[1]
return auc
def binary_crossentropy_custom(y_true, y_pred):
y_labels = y_true
return K.binary_crossentropy(y_labels,
y_pred)