-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
121 lines (101 loc) · 3.37 KB
/
utils.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
# -*- coding:utf-8 -*-
# Created at 2020-04-16
# Filename:utils.py
# Author:Wang Pan
# Purpose:
import pickle
import torch
import numpy as np
def masked_mse_torch(null_val=np.nan):
"""
Accuracy with masking.
:param preds:
:param labels:
:param null_val:
:return:
"""
def loss(preds, labels, null_val=null_val):
if np.isnan(null_val):
mask = ~torch.isnan(labels)
else:
mask = ~(labels==null_val)
mask.float()
mask = mask/torch.mean(mask)
mask = torch.where(torch.isnan(mask), torch.zeros_like(mask), mask)
loss = (preds- labels)*(preds-labels)
loss = loss * mask
loss = torch.where(torch.isnan(loss), torch.zeros_like(loss), loss)
return torch.mean(loss)
return loss
def masked_mae_torch(null_val=np.nan):
"""
Accuracy with masking.
:param preds:
:param labels:
:param null_val:
:return:
"""
def loss(preds, labels, null_val=null_val):
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)
return loss
def masked_rmse_torch(null_val=np.nan):
"""
Accuracy with masking.
:param preds:
:param labels:
:param null_val:
:return:
"""
def loss(preds, labels, null_val=null_val):
return torch.sqrt(masked_mse_torch(preds=preds, labels=labels, null_val=null_val))
return loss
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)
def pickle_save(filename, object):
with open(filename, 'wb') as f:
pickle.dump(object, f)
def DCRNN_teaching_force_calculater(i,tao):
return tao/(tao+np.exp(i/tao))