-
Notifications
You must be signed in to change notification settings - Fork 0
/
loss.py
98 lines (61 loc) · 2.04 KB
/
loss.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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""Module providing loss-related functionality."""
import tensorflow as tf
import cerebral as cb
K = tf.keras.backend
def masked_MSE(y_true, y_pred):
"""Calculate the mean-squared-error, ignoring masked values.
:group: loss
"""
mask = K.not_equal(y_true, cb.features.mask_value)
squared_error = tf.where(
mask, tf.math.square(tf.subtract(y_true, y_pred)), 0
)
return squared_error
def masked_MAE(y_true, y_pred):
"""Calculate the mean-absolute-error, ignoring masked values.
:group: loss
"""
mask = K.not_equal(y_true, cb.features.mask_value)
abs_error = tf.where(mask, tf.math.abs(tf.subtract(y_true, y_pred)), 0)
return abs_error
def masked_PseudoHuber(y_true, y_pred):
"""Calculate the pseudo-Huber error, ignoring masked values.
:group: loss
"""
mask = K.not_equal(y_true, cb.features.mask_value)
error = tf.where(mask, tf.subtract(y_true, y_pred), 0)
huber = tf.math.subtract(
tf.math.sqrt(
tf.math.add(K.cast(1.0, "float64"), tf.math.square(error))
),
K.cast(1.0, "float64"),
)
return huber
def masked_Huber(y_true, y_pred):
"""Calculate the Huber error, ignoring masked values.
:group: loss
"""
mask = K.not_equal(y_true, cb.features.mask_value)
error = tf.where(mask, tf.abs(tf.subtract(y_true, y_pred)), 0)
delta = K.cast(1.0, "float64")
huber = tf.where(
tf.abs(error) > delta,
tf.add(
K.cast(0.5 * delta**2, "float64"),
tf.multiply(delta, tf.subtract(error, delta)),
),
tf.multiply(K.cast(0.5, "float64"), tf.square(error)),
)
return huber
def masked_sparse_categorical_crossentropy(y_true, y_pred):
"""Calculate the sparse categorical cross-entropy, ignoring masked
values.
:group: loss
"""
mask = K.not_equal(K.squeeze(y_true, axis=-1), cb.features.mask_value)
scce = tf.where(
mask,
tf.keras.losses.sparse_categorical_crossentropy(y_true, y_pred),
0,
)
return scce