-
Notifications
You must be signed in to change notification settings - Fork 17
/
utils.py
51 lines (43 loc) · 1.41 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
import torch
import numpy as np
def scaled_laplacian(A):
n = A.shape[0]
d = np.sum(A, axis=1)
L = np.diag(d) - A
for i in range(n):
for j in range(n):
if d[i] > 0 and d[j] > 0:
L[i, j] /= np.sqrt(d[i] * d[j])
lam = np.linalg.eigvals(L).max().real
return 2 * L / lam - np.eye(n)
def cheb_poly(L, Ks):
n = L.shape[0]
LL = [np.eye(n), L[:]]
for i in range(2, Ks):
LL.append(np.matmul(2 * L, LL[-1]) - LL[-2])
return np.asarray(LL)
def evaluate_model(model, loss, data_iter):
model.eval()
l_sum, n = 0.0, 0
with torch.no_grad():
for x, y in data_iter:
y_pred = model(x).view(len(x), -1)
l = loss(y_pred, y)
l_sum += l.item() * y.shape[0]
n += y.shape[0]
return l_sum / n
def evaluate_metric(model, data_iter, scaler):
model.eval()
with torch.no_grad():
mae, mape, mse = [], [], []
for x, y in data_iter:
y = scaler.inverse_transform(y.cpu().numpy()).reshape(-1)
y_pred = scaler.inverse_transform(model(x).view(len(x), -1).cpu().numpy()).reshape(-1)
d = np.abs(y - y_pred)
mae += d.tolist()
mape += (d / y).tolist()
mse += (d ** 2).tolist()
MAE = np.array(mae).mean()
MAPE = np.array(mape).mean()
RMSE = np.sqrt(np.array(mse).mean())
return MAE, MAPE, RMSE