forked from rabbit721/QPPNet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
metric.py
52 lines (46 loc) · 1.41 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
import torch
class Metric:
"""
Metric class
"""
@staticmethod
def r_q(tt, pred_time, epsilon):
"""
returns R(q) test loss defined in the QPP paper
"""
rq_vec, _ = torch.max(
torch.cat(
[
((tt + epsilon) / (pred_time + epsilon)).unsqueeze(0),
((pred_time + epsilon) / (tt + epsilon)).unsqueeze(0),
],
axis=0,
),
axis=0,
)
# print(rq_vec.shape)
curr_rq = torch.mean(rq_vec).item()
return curr_rq
@staticmethod
def pred_err(tt, pred_time, epsilon):
"""
returns a vector of pred_err for each sample in the input
"""
curr_pred_err = (torch.abs(tt - pred_time) + epsilon) / (tt + epsilon)
return curr_pred_err
@staticmethod
def accumulate_err(tt, pred_time, epsilon):
"""
returns the pred_err for the sum of predictions
"""
tt_sum = torch.sum(tt)
pred_time_sum = torch.sum(pred_time)
return torch.abs(pred_time_sum - tt_sum + epsilon) / (tt_sum + epsilon)
@staticmethod
def mean_mae(tt, pred_time, epsilon):
"""
returns the absolute error for the mean of predictions
"""
tt_mean = torch.mean(tt)
pred_time_mean = torch.mean(pred_time)
return torch.abs(pred_time_mean - tt_mean)