forked from nimingniming/gdn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics
157 lines (126 loc) · 4.53 KB
/
metrics
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
import numpy as np
import tensorflow as tf
def masked_mse_tf(preds, labels, null_val=np.nan):
"""
Accuracy with masking.
:param preds:
:param labels:
:param null_val:
:return:
"""
if np.isnan(null_val):
mask = ~tf.is_nan(labels)
else:
mask = tf.not_equal(labels, null_val)
mask = tf.cast(mask, tf.float32)
mask /= tf.reduce_mean(mask)
mask = tf.where(tf.is_nan(mask), tf.zeros_like(mask), mask)
loss = tf.square(tf.subtract(preds, labels))
loss = loss * mask
loss = tf.where(tf.is_nan(loss), tf.zeros_like(loss), loss)
return tf.reduce_mean(loss)
def masked_mae_tf(preds, labels, null_val=np.nan):
"""
Accuracy with masking.
:param preds:
:param labels:
:param null_val:
:return:
"""
if np.isnan(null_val):
mask = ~tf.is_nan(labels)
else:
mask = tf.not_equal(labels, null_val)
mask = tf.cast(mask, tf.float32)
mask /= tf.reduce_mean(mask)
mask = tf.where(tf.is_nan(mask), tf.zeros_like(mask), mask)
loss = tf.abs(tf.subtract(preds, labels))
loss = loss * mask
loss = tf.where(tf.is_nan(loss), tf.zeros_like(loss), loss)
return tf.reduce_mean(loss)
def masked_rmse_tf(preds, labels, null_val=np.nan):
"""
Accuracy with masking.
:param preds:
:param labels:
:param null_val:
:return:
"""
return tf.sqrt(masked_mse_tf(preds=preds, labels=labels, null_val=null_val))
def masked_rmse_np(preds, labels, null_val=np.nan):
return np.sqrt(masked_mse_np(preds=preds, labels=labels, null_val=null_val))
def masked_mse_np(preds, labels, null_val=np.nan):
with np.errstate(divide='ignore', invalid='ignore'):
if np.isnan(null_val):
mask = ~np.isnan(labels)
else:
mask = np.not_equal(labels, null_val)
mask = mask.astype('float32')
mask /= np.mean(mask)
rmse = np.square(np.subtract(preds, labels)).astype('float32')
rmse = np.nan_to_num(rmse * mask)
return np.mean(rmse)
def masked_mae_np(preds, labels, null_val=np.nan):
with np.errstate(divide='ignore', invalid='ignore'):
if np.isnan(null_val):
mask = ~np.isnan(labels)
else:
mask = np.not_equal(labels, null_val)
mask = mask.astype('float32')
mask /= np.mean(mask)
mae = np.abs(np.subtract(preds, labels)).astype('float32')
mae = np.nan_to_num(mae * mask)
return np.mean(mae)
def masked_mape_np(preds, labels, null_val=np.nan):
with np.errstate(divide='ignore', invalid='ignore'):
if np.isnan(null_val):
mask = ~np.isnan(labels)
else:
mask = np.not_equal(labels, null_val)
mask = mask.astype('float32')
mask /= np.mean(mask)
mape = np.abs(np.divide(np.subtract(preds, labels).astype('float32'), labels))
mape = np.nan_to_num(mask * mape)
return np.mean(mape)
# Builds loss function.
def masked_mse_loss(scaler, null_val):
def loss(preds, labels):
if scaler:
preds = scaler.inverse_transform(preds)
labels = scaler.inverse_transform(labels)
return masked_mse_tf(preds=preds, labels=labels, null_val=null_val)
return loss
def masked_rmse_loss(scaler, null_val):
def loss(preds, labels):
if scaler:
preds = scaler.inverse_transform(preds)
labels = scaler.inverse_transform(labels)
return masked_rmse_tf(preds=preds, labels=labels, null_val=null_val)
return loss
def masked_mae_loss(scaler, null_val):
def loss(preds, labels):
if scaler:
preds = scaler.inverse_transform(preds)
labels = scaler.inverse_transform(labels)
mae = masked_mae_tf(preds=preds, labels=labels, null_val=null_val)
return mae
return loss
def calculate_metrics(df_pred, df_test, null_val):
"""
Calculate the MAE, MAPE, RMSE
:param df_pred:
:param df_test:
:param null_val:
:return:
"""
mape = masked_mape_np(preds=df_pred.as_matrix(), labels=df_test.as_matrix(), null_val=null_val)
mae = masked_mae_np(preds=df_pred.as_matrix(), labels=df_test.as_matrix(), null_val=null_val)
rmse = masked_rmse_np(preds=df_pred.as_matrix(), labels=df_test.as_matrix(), null_val=null_val)
return mae, mape, rmse
def weight_threshold(weight_tensor, threshold):
n, m = weight_tensor.shape
for i in range(n - 1):
for j in range(m - 1):
weight_hold = weight_tensor[i-1:i + 1, j-1:j + 1]
weight_mask = weight_tensor > threshold
return weight_mask