-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf2metric.py
More file actions
79 lines (51 loc) · 2.4 KB
/
f2metric.py
File metadata and controls
79 lines (51 loc) · 2.4 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# This file consists of code related to f2 score computation. Since implemented as Tensorflow nodes, this score acts as a part
# of training graph and is differentiable during backpropogation just like other graph nodes.
from tensorflow.python.keras import backend as K
import tensorflow as tf
def f_score(y_true, y_pred, threshold=0.1, beta=2):
tp = tp_score(y_true, y_pred, threshold)
fp = fp_score(y_true, y_pred, threshold)
fn = fn_score(y_true, y_pred, threshold)
precision = tp / (tp + fp)
recall = tp / (tp + fn)
score = (1+beta**2) * ((precision * recall) / ((beta**2)*precision + recall))
score = tf.where(tf.is_nan(score), tf.constant(0.0, dtype=tf.float64), score)
return score
def tp_score(y_true, y_pred, threshold=0.1):
tp_3d = K.concatenate(
[
K.cast(K.expand_dims(K.flatten(y_true)), 'bool'),
K.cast(K.expand_dims(K.flatten(K.greater(y_pred, K.constant(threshold)))), 'bool'),
K.cast(K.ones_like(K.expand_dims(K.flatten(y_pred))), 'bool')
], axis=1
)
tp = K.sum(K.cast(K.all(tp_3d, axis=1), 'int32'))
return tp
def fp_score(y_true, y_pred, threshold=0.1):
fp_3d = K.concatenate(
[
K.cast(K.expand_dims(K.flatten(K.abs(y_true - K.ones_like(y_true)))), 'bool'),
K.cast(K.expand_dims(K.flatten(K.greater(y_pred, K.constant(threshold)))), 'bool'),
K.cast(K.ones_like(K.expand_dims(K.flatten(y_pred))), 'bool')
], axis=-1
)
fp = K.sum(K.cast(K.all(fp_3d, axis=1), 'int32'))
return fp
def fn_score(y_true, y_pred, threshold=0.1):
fn_3d = K.concatenate(
[
K.cast(K.expand_dims(K.flatten(y_true)), 'bool'),
K.cast(K.expand_dims(K.flatten(K.abs(K.cast(K.greater(y_pred, K.constant(threshold)), 'float') - K.ones_like(y_pred)))), 'bool'),
K.cast(K.ones_like(K.expand_dims(K.flatten(y_pred))), 'bool')
], axis=1
)
fn = K.sum(K.cast(K.all(fn_3d, axis=1), 'int32'))
return fn
def precision_score(y_true, y_pred, threshold=0.1):
tp = tp_score(y_true, y_pred, threshold)
fp = fp_score(y_true, y_pred, threshold)
return tp / (tp + fp)
def recall_score(y_true, y_pred, threshold=0.1):
tp = tp_score(y_true, y_pred, threshold)
fn = fn_score(y_true, y_pred, threshold)
return tp / (tp + fn)