-
Notifications
You must be signed in to change notification settings - Fork 7
/
loss_metrics.py
209 lines (164 loc) · 5.87 KB
/
loss_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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""Loss metrics for Sup3r"""
from tensorflow.keras.losses import MeanSquaredError, MeanAbsoluteError
import tensorflow as tf
def gaussian_kernel(x1, x2, sigma=1.0):
"""Gaussian kernel for mmd content loss
Parameters
----------
x1 : tf.tensor
synthetic generator output
(n_obs, spatial_1, spatial_2, temporal, features)
x2 : tf.tensor
high resolution data
(n_obs, spatial_1, spatial_2, temporal, features)
Returns
-------
tf.tensor
kernel output tensor
References
----------
Following MMD implementation in https://github.com/lmjohns3/theanets
"""
# The expand dims + subtraction compares every entry for the dimension
# prior to the expanded dimension to every other entry. So expand_dims with
# axis=1 will compare every observation along axis=0 to every other
# observation along axis=0.
result = tf.exp(-0.5 * tf.reduce_sum(
(tf.expand_dims(x1, axis=1) - x2)**2, axis=-1) / sigma**2)
return result
class ExpLoss(tf.keras.losses.Loss):
"""Loss class for squared exponential difference"""
def __call__(self, x1, x2):
"""Exponential difference loss function
Parameters
----------
x1 : tf.tensor
synthetic generator output
(n_observations, spatial_1, spatial_2, temporal, features)
x2 : tf.tensor
high resolution data
(n_observations, spatial_1, spatial_2, temporal, features)
Returns
-------
tf.tensor
0D tensor with loss value
"""
return tf.reduce_mean(1 - tf.exp(-(x1 - x2)**2))
class MseExpLoss(tf.keras.losses.Loss):
"""Loss class for mse + squared exponential difference"""
MSE_LOSS = MeanSquaredError()
def __call__(self, x1, x2):
"""Mse + Exponential difference loss function
Parameters
----------
x1 : tf.tensor
synthetic generator output
(n_observations, spatial_1, spatial_2, temporal, features)
x2 : tf.tensor
high resolution data
(n_observations, spatial_1, spatial_2, temporal, features)
Returns
-------
tf.tensor
0D tensor with loss value
"""
mse = self.MSE_LOSS(x1, x2)
exp = tf.reduce_mean(1 - tf.exp(-(x1 - x2)**2))
return mse + exp
class MmdLoss(tf.keras.losses.Loss):
"""Loss class for max mean discrepancy loss"""
def __call__(self, x1, x2, sigma=1.0):
"""Maximum mean discrepancy (MMD) based on Gaussian kernel function
for keras models
Parameters
----------
x1 : tf.tensor
synthetic generator output
(n_observations, spatial_1, spatial_2, temporal, features)
x2 : tf.tensor
high resolution data
(n_observations, spatial_1, spatial_2, temporal, features)
sigma : float
standard deviation for gaussian kernel
Returns
-------
tf.tensor
0D tensor with loss value
"""
mmd = tf.reduce_mean(gaussian_kernel(x1, x1, sigma))
mmd += tf.reduce_mean(gaussian_kernel(x2, x2, sigma))
mmd -= tf.reduce_mean(2 * gaussian_kernel(x1, x2, sigma))
return mmd
class MmdMseLoss(tf.keras.losses.Loss):
"""Loss class for MMD + MSE"""
MMD_LOSS = MmdLoss()
MSE_LOSS = MeanSquaredError()
def __call__(self, x1, x2, sigma=1.0):
"""Maximum mean discrepancy (MMD) based on Gaussian kernel function
for keras models plus the typical MSE loss.
Parameters
----------
x1 : tf.tensor
synthetic generator output
(n_observations, spatial_1, spatial_2, temporal, features)
x2 : tf.tensor
high resolution data
(n_observations, spatial_1, spatial_2, temporal, features)
sigma : float
standard deviation for gaussian kernel
Returns
-------
tf.tensor
0D tensor with loss value
"""
mmd = self.MMD_LOSS(x1, x2)
mse = self.MSE_LOSS(x1, x2)
return mmd + mse
class CoarseMseLoss(tf.keras.losses.Loss):
"""Loss class for coarse mse on spatial average of 5D tensor"""
MSE_LOSS = MeanSquaredError()
def __call__(self, x1, x2):
"""Exponential difference loss function
Parameters
----------
x1 : tf.tensor
synthetic generator output
(n_observations, spatial_1, spatial_2, temporal, features)
x2 : tf.tensor
high resolution data
(n_observations, spatial_1, spatial_2, temporal, features)
Returns
-------
tf.tensor
0D tensor with loss value
"""
x1_coarse = tf.reduce_mean(x1, axis=(1, 2))
x2_coarse = tf.reduce_mean(x2, axis=(1, 2))
return self.MSE_LOSS(x1_coarse, x2_coarse)
class TemporalExtremesLoss(tf.keras.losses.Loss):
"""Loss class that encourages accuracy of the min/max values in the
timeseries"""
MAE_LOSS = MeanAbsoluteError()
def __call__(self, x1, x2):
"""Custom content loss that encourages temporal min/max accuracy
Parameters
----------
x1 : tf.tensor
synthetic generator output
(n_observations, spatial_1, spatial_2, temporal, features)
x2 : tf.tensor
high resolution data
(n_observations, spatial_1, spatial_2, temporal, features)
Returns
-------
tf.tensor
0D tensor with loss value
"""
x1_min = tf.reduce_min(x1, axis=3)
x2_min = tf.reduce_min(x2, axis=3)
x1_max = tf.reduce_max(x1, axis=3)
x2_max = tf.reduce_max(x2, axis=3)
mae = self.MAE_LOSS(x1, x2)
mae_min = self.MAE_LOSS(x1_min, x2_min)
mae_max = self.MAE_LOSS(x1_max, x2_max)
return mae + mae_min + mae_max