forked from yoshall/SINPA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.py
173 lines (137 loc) · 6.18 KB
/
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
import torch
import numpy as np
import pandas as pd
import os
occup = np.load("data/prob_full_occupy.npy")
occup_mask = (occup > 0.1).astype(int).reshape(1, 1, -1, 1)
occup_mask = torch.Tensor(occup_mask)
def masked_mse(preds, labels, null_val=np.nan, mask=None):
"""
Calculates the mean squared error (MSE) between the predicted values and the labels,
considering only the non-null values specified by the mask.
Args:
preds (torch.Tensor): The predicted values.
labels (torch.Tensor): The true labels.
null_val (float, optional): The null value used to identify missing or invalid values. Defaults to np.nan.
mask (torch.Tensor, optional): The mask indicating which values to consider. If None, it will be automatically
generated based on the null_val. Defaults to None.
Returns:
torch.Tensor: The mean squared error between the predicted values and the labels, considering only the non-null values.
"""
if mask == None:
if np.isnan(null_val):
mask = ~torch.isnan(labels)
else:
mask = labels > null_val + 0.1 # +0.1 for potential numerical errors
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(preds, labels, null_val=np.nan, mask=None):
"""
Calculate the root mean squared error (RMSE) between predicted values and true labels,
considering a mask to exclude certain values.
Args:
preds (torch.Tensor): Predicted values.
labels (torch.Tensor): True labels.
null_val (float, optional): Value to be considered as null. Defaults to np.nan.
mask (torch.Tensor, optional): Mask to exclude certain values. Defaults to None.
Returns:
torch.Tensor: The root mean squared error (RMSE) between preds and labels.
"""
if mask == None:
return torch.sqrt(masked_mse(preds=preds, labels=labels, null_val=null_val))
else:
return torch.sqrt(
masked_mse(preds=preds, labels=labels, null_val=null_val, mask=mask)
)
def masked_mae(preds, labels, null_val=np.nan, mask=None):
"""
Calculates the mean absolute error (MAE) between the predicted values and the true labels,
taking into account a mask to exclude certain values.
Args:
preds (torch.Tensor): The predicted values.
labels (torch.Tensor): The true labels.
null_val (float, optional): The null value used in the mask. Defaults to np.nan.
mask (torch.Tensor, optional): The mask to exclude certain values. Defaults to None.
Returns:
torch.Tensor: The masked mean absolute error (MAE) between the predicted values and the true labels.
"""
if mask == None:
if np.isnan(null_val):
mask = ~torch.isnan(labels)
else:
mask = labels > null_val + 0.1 # +0.1 for potential numerical errors
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_mape(preds, labels, null_val=np.nan, mask=None):
"""
Calculate the masked mean absolute percentage error (MAPE) between predictions and labels.
Args:
preds (torch.Tensor): The predicted values.
labels (torch.Tensor): The true values.
null_val (float, optional): The null value used for masking. Defaults to np.nan.
mask (torch.Tensor, optional): The mask tensor. If not provided, it will be automatically generated based on the null_val. Defaults to None.
Returns:
torch.Tensor: The masked MAPE loss.
"""
if mask == None:
if np.isnan(null_val):
mask = ~torch.isnan(labels)
else:
mask = labels > null_val + 0.1 # +0.1 for potential numerical errors
mask = mask.float()
mask /= torch.mean((mask))
mask = torch.where(torch.isnan(mask), torch.zeros_like(mask), mask)
loss = torch.abs(preds - labels) / (labels + 0.1)
loss = loss * mask
loss = torch.where(torch.isnan(loss), torch.zeros_like(loss), loss)
return torch.mean(loss)
def compute_all_metrics(pred, real, null_value=np.nan):
"""
Compute multiple metrics for evaluating the performance of a prediction.
Args:
pred (numpy.ndarray): The predicted values.
real (numpy.ndarray): The ground truth values.
null_value (float, optional): The value used to represent missing or invalid data. Defaults to np.nan.
Returns:
tuple: A tuple containing the computed metrics (mae, rmse).
"""
mae = masked_mae(pred, real, null_value).item()
rmse = masked_rmse(pred, real, null_value).item()
return mae, rmse
def masked_acc(preds, labels, null_val=np.nan, mask=None, threshould=0.01):
"""
Calculates the masked accuracy between predicted values and ground truth labels.
Args:
preds (torch.Tensor): Predicted values.
labels (torch.Tensor): Ground truth labels.
null_val (float, optional): Value to be considered as null. Defaults to np.nan.
mask (torch.Tensor, optional): Mask indicating which values to consider. Defaults to None.
threshould (float, optional): Threshold value for numerical errors. Defaults to 0.01.
Returns:
torch.Tensor: Masked accuracy.
"""
if mask == None:
if np.isnan(null_val):
mask = ~torch.isnan(labels)
else:
mask = labels > null_val + 0.1 # +0.1 for potential numerical errors
mask = occup_mask * mask
mask = mask.float()
mask /= torch.mean((mask))
mask = torch.where(torch.isnan(mask), torch.zeros_like(mask), mask)
preds = preds.int() > 0 # int for values like -0.001
labels = labels.int() > 0 # int for values like -0.001
loss = (preds == labels).int()
loss = loss * mask
loss = torch.where(torch.isnan(loss), torch.zeros_like(loss), loss)
return torch.mean(loss)