-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.py
65 lines (48 loc) · 1.83 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
import pandas as pd
from torch import Tensor
import torch
from torch.utils.tensorboard.writer import SummaryWriter
def get_r_squared(preds: Tensor, labels: Tensor):
"""
Calculate the R^2 value for the given predictions and labels.
"""
sum_squared_regression = ((labels - preds) ** 2).sum()
sum_of_squares = ((labels - labels.mean()) ** 2).sum()
return 1 - (sum_squared_regression / sum_of_squares)
def get_mae(preds: Tensor, labels: Tensor):
"""
Calculate the mean absolute error for the given predictions and labels.
"""
return (labels - preds).abs().mean()
def get_mape(preds: Tensor, labels: Tensor):
"""
Calculate the mean absolute percentage error for the given predictions and labels.
"""
percentage_error = ((labels - preds) / labels).abs()
return percentage_error.mean()
def get_smape(preds: Tensor, labels: Tensor):
"""
Calculate the symmetric mean absolute percentage error for the given predictions and labels.
"""
abs_residuals = (labels - preds).abs()
denominator = (labels.abs() + preds.abs()) / 2
return (100 * abs_residuals / denominator).mean()
def get_rmse(preds: Tensor, labels: Tensor):
"""
Calculate the root mean squared error for the given predictions and labels.
"""
return torch.sqrt(((labels - preds) ** 2).mean())
def write_all_metrics(
preds: Tensor,
labels: Tensor,
writer: SummaryWriter,
epoch: int,
):
"""
Write all metrics to the SummaryWriter provided.
"""
writer.add_scalar("val/r_squared", get_r_squared(preds, labels), epoch)
writer.add_scalar("val/mae", get_mae(preds, labels), epoch)
writer.add_scalar("val/mape", get_mape(preds, labels), epoch)
writer.add_scalar("val/smape", get_smape(preds, labels), epoch)
writer.add_scalar("val/rmse", get_rmse(preds, labels), epoch)