-
Notifications
You must be signed in to change notification settings - Fork 0
/
metric.py
201 lines (170 loc) · 6.39 KB
/
metric.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
import numpy as np
import torch
def masked_mae_np(y_true, y_pred, null_val=np.nan):
with np.errstate(divide='ignore', invalid='ignore'):
if np.isnan(null_val):
mask = ~np.isnan(y_true)
else:
mask = np.not_equal(y_true, null_val)
mask = mask.astype('float32')
mask /= np.mean(mask)
mae = np.abs(np.subtract(y_pred, y_true).astype('float32'),)
mae = np.nan_to_num(mask * mae)
return np.mean(mae)
def masked_rmse_np(y_true, y_pred, null_val=np.nan):
with np.errstate(divide='ignore', invalid='ignore'):
if np.isnan(null_val):
mask = ~np.isnan(y_true)
else:
mask = np.not_equal(y_true, null_val)
mask = mask.astype('float32')
mask /= np.mean(mask)
mse = ((y_pred- y_true)**2)
mse = np.nan_to_num(mask * mse)
return np.sqrt(np.mean(mse))
def masked_mape_np(y_true, y_pred, null_val=np.nan):
idx = y_true > 1e-5
y_true = y_true[idx]
y_pred = y_pred[idx]
with np.errstate(divide='ignore', invalid='ignore'):
if np.isnan(null_val):
mask = ~np.isnan(y_true)
else:
mask = np.not_equal(y_true, null_val)
mask = mask.astype('float32')
mask /= np.mean(mask)
mape = np.abs(np.subtract(y_pred, y_true).astype('float32')) / np.abs(y_true)
mape = np.nan_to_num(mask * mape)
return np.mean(mape) * 100
def masked_wae_np(weight, y_true, y_pred, null_val=np.nan):
weight = weight.reshape(1, -1, 1)
with np.errstate(divide='ignore', invalid='ignore'):
if np.isnan(null_val):
mask = ~np.isnan(y_true)
else:
mask = np.not_equal(y_true, null_val)
mask = mask.astype('float32')
mask = mask * weight
mask /= np.sum(mask)
ae = np.abs(np.subtract(y_pred, y_true).astype('float32'),)
wae = np.nan_to_num(mask * ae)
return np.sum(wae)
def masked_rwse_np(weight, y_true, y_pred, null_val=np.nan):
weight = weight.reshape(1, -1, 1)
with np.errstate(divide='ignore', invalid='ignore'):
if np.isnan(null_val):
mask = ~np.isnan(y_true)
else:
mask = np.not_equal(y_true, null_val)
mask = mask.astype('float32')
mask = mask * weight
mask /= np.sum(mask)
se = ((y_pred- y_true)**2)
mse = np.nan_to_num(mask * se)
return np.sqrt(np.sum(mse))
def masked_wape_np(weight, y_true, y_pred, null_val=np.nan):
weight = weight.reshape(1, -1, 1)
with np.errstate(divide='ignore', invalid='ignore'):
if np.isnan(null_val):
mask = ~np.isnan(y_true)
else:
mask = np.not_equal(y_true, null_val)
mask = mask.astype('float32')
mask = mask * weight
mask /= np.sum(mask)
ape = np.abs(np.subtract(y_pred, y_true).astype('float32')) / np.abs(y_true)
mape = np.nan_to_num(mask * ape)
return np.sum(mape) * 100
def masked_saes_np(ratio, y_true, y_pred, null_val=np.nan):
aes = np.abs(y_true - y_pred) / np.expand_dims(ratio, axis=(0, 2))
with np.errstate(divide='ignore', invalid='ignore'):
if np.isnan(null_val):
mask = ~np.isnan(aes)
else:
mask = np.not_equal(aes, null_val)
return np.std(aes[mask])
def masked_mae_torch(labels, preds, null_val=np.nan):
if np.isnan(null_val):
mask = ~torch.isnan(labels)
else:
mask = (labels != null_val)
mask = mask.float()
mask /= torch.mean(mask)
mask = torch.where(torch.isnan(mask), torch.zeros_like(mask), mask)
loss = torch.abs(preds - labels)
loss = loss * mask
loss = torch.where(torch.isnan(loss), torch.zeros_like(loss), loss)
return torch.mean(loss)
def masked_sase_np(ratio, y_true, y_pred, null_val=np.nan):
aes = np.mean(np.abs(y_true - y_pred), axis=(0, 2)) / ratio
with np.errstate(divide='ignore', invalid='ignore'):
if np.isnan(null_val):
mask = ~np.isnan(aes)
else:
mask = np.not_equal(aes, null_val)
return np.std(aes[mask])
def masked_saes_torch(ratio, labels, preds, null_val=np.nan):
aes = torch.abs(preds - labels) / ratio.reshape((1, -1, 1))
if np.isnan(null_val):
mask = ~torch.isnan(aes)
else:
mask = (aes != null_val)
return torch.std(aes * mask)
def masked_mse_torch(labels, preds, null_val=np.nan):
if np.isnan(null_val):
mask = ~torch.isnan(labels)
else:
mask = (labels != null_val)
mask = mask.float()
mask /= torch.mean(mask)
mask = torch.where(torch.isnan(mask), torch.zeros_like(mask), mask)
loss = (preds - labels) ** 2
loss = loss * mask
loss = torch.where(torch.isnan(loss), torch.zeros_like(loss), loss)
return torch.mean(loss)
def masked_rmse_torch(labels, preds, null_val=np.nan):
return torch.sqrt(masked_mse_torch(preds=preds, labels=labels, null_val=null_val))
def masked_mape_torch(labels, preds, null_val=np.nan):
if np.isnan(null_val):
mask = ~torch.isnan(labels)
else:
mask = (labels != null_val)
mask = mask.float()
mask /= torch.mean(mask)
mask = torch.where(torch.isnan(mask), torch.zeros_like(mask), mask)
loss = torch.abs(preds - labels) / labels
loss = loss * mask
loss = torch.where(torch.isnan(loss), torch.zeros_like(loss), loss)
return torch.mean(loss)
def masked_wae_torch(weight, labels, preds, null_val=np.nan):
if np.isnan(null_val):
mask = ~torch.isnan(labels)
else:
mask = (labels != null_val)
mask = mask.float() * weight
mask /= torch.sum(mask)
ae = torch.abs(preds- labels)
wae = torch.nan_to_num(mask * ae)
return torch.sum(wae)
def masked_rwse_torch(weight, labels, preds, null_val=np.nan):
if np.isnan(null_val):
mask = ~torch.isnan(labels)
else:
mask = (labels != null_val)
mask = mask.float()
mask = mask * weight
mask /= torch.sum(mask)
se = ((preds- labels)**2)
mse = torch.nan_to_num(mask * se)
return torch.sqrt(torch.sum(mse)).item()
def masked_wape_torch(weight, labels, preds, null_val=np.nan):
if np.isnan(null_val):
mask = ~torch.isnan(labels)
else:
mask = (labels != null_val)
mask = mask.float()
mask = mask * weight
mask /= torch.sum(mask)
ae = torch.abs((preds - labels) / labels)
mae = torch.nan_to_num(mask * ae)
return torch.sum(mae).item() * 100