forked from liamconnor/polish-pub
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBNN_loss.py
141 lines (120 loc) · 5.48 KB
/
BNN_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import numpy as np
import tensorflow as tf
import tensorflow.keras.backend as K
from model.common import decenternormalize, denormalize, normalize
def laplacian_normalized_noexp(y_pred, y_true, show_parts=True):
mean_true = tf.math.divide(y_true[:, :, :, 0], 2**15)
mean_pred = y_pred[:, :, :, 0]
scale_pred = K.pow(y_pred[:, :, :, 1], 2) + 1e-7
if show_parts:
tf.print("top_loss", tf.math.reduce_mean(K.abs(mean_true - mean_pred)))
tf.print("bottom_loss", tf.math.reduce_mean(scale_pred))
tf.print("coef_loss", tf.math.reduce_mean(K.log(scale_pred)))
loss = tf.math.divide((K.abs(mean_true - mean_pred)), scale_pred) + K.log(
scale_pred
)
return loss
def laplacian_normalized_exp(y_pred, y_true, show_parts=True):
mean_true = tf.math.divide(y_true[:, :, :, 0], 2**15)
mean_pred = y_pred[:, :, :, 0]
scale_pred = y_pred[:, :, :, 1]
if show_parts:
tf.print("top_loss", tf.math.reduce_mean(K.abs(mean_true - mean_pred)))
tf.print("bottom_loss", tf.math.reduce_mean(2 * K.exp(scale_pred)))
tf.print("coef_loss", tf.math.reduce_mean(tf.divide(scale_pred, 2)))
loss = tf.math.divide(
(K.abs(mean_true - mean_pred)), 2 * K.exp(scale_pred)
) + tf.divide(scale_pred, 2)
return loss
def laplacian_denormalized_noexp(y_pred, y_true, show_parts=True):
mean_true = y_true[:, :, :, 0]
mean_pred = decenternormalize(y_pred[:, :, :, 0])
scale_pred = decenternormalize(y_pred[:, :, :, 1])
if show_parts:
tf.print("top_loss", tf.math.reduce_mean(K.abs(mean_true - mean_pred)))
tf.print("bottom_loss", tf.math.reduce_mean(scale_pred))
tf.print("coef_loss", tf.math.reduce_mean(K.log(scale_pred)))
loss = tf.math.divide((K.abs(mean_true - mean_pred)), scale_pred) + K.log(
scale_pred
)
return loss
def gaussian_normalized_exp(y_pred, y_true, show_parts=True):
mean_true = normalize(y_true[:, :, :, 0])
mean_pred = y_pred[:, :, :, 0]
scale_pred = y_pred[:, :, :, 1]
if show_parts:
tf.print("sqrt top_loss", tf.math.reduce_mean(K.abs(mean_true - mean_pred)))
tf.print("bottom_loss", tf.math.reduce_mean(2 * K.exp(scale_pred)))
tf.print("coef_loss", (tf.math.reduce_mean(scale_pred) / 2))
loss = tf.math.divide((K.pow(mean_true - mean_pred, 2)), 2 * K.exp(scale_pred)) + (
tf.divide(scale_pred, 2)
)
return loss
def gaussian_de_exp(y_pred, y_true, show_parts=True):
mean_true = y_true[:, :, :, 0]
mean_pred = denormalize(y_pred[:, :, :, 0])
scale_pred = y_pred[:, :, :, 1]
if show_parts:
tf.print("sqrt top_loss", tf.math.reduce_mean(K.abs(mean_true - mean_pred)))
tf.print("bottom_loss", tf.math.reduce_mean(2 * K.exp(scale_pred)))
tf.print("coef_loss", (tf.math.reduce_mean(scale_pred) / 2))
loss = tf.math.divide((K.pow(mean_true - mean_pred, 2)), 2 * K.exp(scale_pred)) + (
tf.divide(scale_pred, 2)
)
return loss
def gaussian_true_normalized_exp(y_pred, y_true, show_parts=True):
mean_true = normalize(y_true[:, :, :, 0])
mean_pred = y_pred[:, :, :, 0]
scale_pred = y_pred[:, :, :, 1]
if show_parts:
tf.print("sqrt top_loss", tf.math.reduce_mean(K.abs(mean_true - mean_pred)))
tf.print("bottom_loss", tf.math.reduce_mean(2 * K.exp(scale_pred)))
tf.print("coef_loss", (tf.math.reduce_mean(scale_pred) / 2))
loss = tf.math.divide((K.pow(mean_true - mean_pred, 2)), 2 * K.exp(scale_pred)) + (
tf.divide(scale_pred, 2)
)
return loss
def gaussian_denormalized_exp(y_pred, y_true, show_parts=True):
mean_true = y_true[:, :, :, 0]
mean_pred = decenternormalize(y_pred[:, :, :, 0])
scale_pred = decenternormalize(y_pred[:, :, :, 1])
if show_parts:
tf.print("sqrt top_loss", tf.math.reduce_mean(K.abs(mean_true - mean_pred)))
tf.print("bottom_loss", tf.math.reduce_mean(2 * K.exp(scale_pred)))
tf.print("coef_loss", (tf.math.reduce_mean(scale_pred) / 2))
loss = tf.math.divide((K.pow(mean_true - mean_pred, 2)), 2 * K.exp(scale_pred)) + (
tf.divide(scale_pred, 2)
)
return loss
def gaussian_denormalized_noexp(y_pred, y_true, show_parts=True):
mean_true = y_true[:, :, :, 0]
mean_pred = decenternormalize(y_pred[:, :, :, 0])
scale_pred = K.pow(decenternormalize(y_pred[:, :, :, 1]), 2) + 1e-7
if show_parts:
tf.print("top_loss", tf.math.reduce_mean(K.abs(mean_true - mean_pred)))
tf.print("bottom_loss", tf.math.reduce_mean((scale_pred)))
tf.print("coef_loss", (tf.math.reduce_mean(K.log(scale_pred))))
loss = tf.math.divide((K.pow(mean_true - mean_pred, 2)), scale_pred) + (
K.log(scale_pred)
)
return loss
def gaussian_normalized_noexp(y_pred, y_true, show_parts=True):
mean_true = y_true[:, :, :, 0]
mean_pred = y_pred[:, :, :, 0]
scale_pred = K.pow(y_pred[:, :, :, 1], 2) + 1e-7
if show_parts:
tf.print("top_loss", tf.math.reduce_mean(K.abs(mean_true - mean_pred)))
tf.print("bottom_loss", tf.math.reduce_mean((scale_pred)))
tf.print("coef_loss", (tf.math.reduce_mean(K.log(scale_pred))))
loss = tf.math.divide((K.pow(mean_true - mean_pred, 2)), scale_pred) + (
K.log(scale_pred)
)
return loss
def gaussian_loss_non_exp(y_pred, y_true):
mean_true = y_true[:, :, :, 0]
mean_pred = y_pred[:, :, :, 0]
scale_pred = K.pow(y_pred[:, :, :, 1], 2)
loss = tf.math.divide((K.pow(mean_true - mean_pred, 2)), scale_pred) + K.log(
scale_pred
)
return loss