-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjectives.py
56 lines (45 loc) · 1.33 KB
/
objectives.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
"""Loss functions."""
import tensorflow as tf
import semver
def huber_loss(y_true, y_pred, max_grad=1.):
"""Calculate the huber loss.
See https://en.wikipedia.org/wiki/Huber_loss
Parameters
----------
y_true: np.array, tf.Tensor
Target value.
y_pred: np.array, tf.Tensor
Predicted value.
max_grad: float, optional
Positive floating point value. Represents the maximum possible
gradient magnitude.
Returns
-------
tf.Tensor
The huber loss.
"""
residual=tf.abs(y_pred-y_true)
condition=tf.less(residual,max_grad)
small_residual=0.5*tf.square(residual)
large_residual=max_grad*residual-0.5*tf.square(max_grad)
return tf.select(condition,small_residual,large_residual)
def mean_huber_loss(y_true, y_pred, max_grad=1.):
"""Return mean huber loss.
Same as huber_loss, but takes the mean over all values in the
output tensor.
Parameters
----------
y_true: np.array, tf.Tensor
Target value.
y_pred: np.array, tf.Tensor
Predicted value.
max_grad: float, optional
Positive floating point value. Represents the maximum possible
gradient magnitude.
Returns
-------
tf.Tensor
The mean huber loss.
"""
loss_tensor=huber_loss(y_true,y_pred,max_grad)
return tf.reduce_mean(loss_tensor)