-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_conv.py
172 lines (140 loc) · 6.08 KB
/
main_conv.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
import numpy as np
import torch
from torch import nn, optim
from models import ConvGNN
from copy import deepcopy
from utils import *
from tqdm import tqdm
args = parse_args()
skip_val = False
dset = args.dset
stationary = args.stationary
m = args.m
pred_step = args.pred_step # how many steps in the future we want to predict
if torch.cuda.is_available():
print("Using CUDA")
device = "cuda"
else:
device = "cpu"
T = args.T
gamma = args.gamma
dimNodeSignals = args.dimNodeSignals
L = len(dimNodeSignals) - 1
nFilterTaps = [args.filter_taps] * L
dimLayersMLP = args.dimLayersMLP
lr = args.lr
Xfold, nTotal, y, Yscaler = load_and_normalize_data(dset, m, pred_step, rec=False)
Xfold, y = Xfold.to(device), y.to(device)
nEpochs = args.nEpochs
train_perc, valid_perc, test_perc = 0.2, 0.1, 0.7
idxTotal = torch.LongTensor(np.arange(nTotal)).to(device)
idxTrain = idxTotal[:np.floor(train_perc*nTotal).astype(int)]
idxValid = idxTotal[np.floor((train_perc)*nTotal).astype(int):np.floor((train_perc+valid_perc)*nTotal).astype(int)]
idxTest = idxTotal[np.floor((train_perc+valid_perc)*nTotal).astype(int):]
xTrain = Xfold[idxTrain]
xValid = Xfold[idxValid]
xTest = Xfold[idxTest]
nTrain = idxTrain.shape[0]
yTrain = y[idxTrain]
yValid = y[idxValid]
yTest = y[idxTest]
C = torch.cov(xTrain.squeeze().T) # Compute covariance on current data
C = C / torch.trace(C) # trace-normalize to avoid numerical issues
GNN = ConvGNN(dimNodeSignals=dimNodeSignals, # hidden size
nFilterTaps=nFilterTaps, # k, i.e. neighborhoods (len = len(dimNodeSignals)-1)
bias=True,
nonlinearity=nn.LeakyReLU,
dimLayersMLP=dimLayersMLP,
GSO=C,
T=T).to(device)
batchSize = args.batchSize
nTrainBatches = int(np.ceil(nTrain / batchSize))
Loss = nn.MSELoss()
MAE = nn.L1Loss()
MSE = nn.MSELoss()
if args.optimizer == "Adam":
optimizer = optim.Adam(GNN.parameters(), lr=lr, weight_decay=0.001)
elif args.optimizer == "SGD":
optimizer = optim.SGD(GNN.parameters(), lr=lr)
else:
print("Optimizer not allowed")
raise Exception()
Best_Valid_Loss, Best_Valid_MAPE = 1e10, 1e10
for epoch in range(nEpochs):
C_old = None
mean = torch.zeros(xTrain.shape[2]).to(device)
tot_train_loss = []
tot_val_mae = 0.
tot_val_mape = 0.
train_perm_idx = torch.randperm(nTrainBatches).to(device) # shuffle order during training
for batch in tqdm(range(nTrainBatches)):
xTrainBatch, yTrainBatch = selectCurrentBatch(train_perm_idx[batch].cpu().item(), batchSize, L, T, nTrain, device, xTrain, yTrain)
C, C_old, mean = update_covariance_mat(C_old, xTrainBatch[L*(T-1):], gamma=gamma,
mean=mean, stationary=stationary, n=batch*batchSize)
GNN.changeGSO(C)
GNN.zero_grad()
yHatTrainBatch = GNN(xTrainBatch)
lossValueTrain = Loss(yHatTrainBatch.squeeze(), yTrainBatch.squeeze())
lossValueTrain.backward()
optimizer.step()
tot_train_loss.append(lossValueTrain.detach())
all_pred = []
if skip_val:
Best_GNN = deepcopy(GNN)
else:
with torch.no_grad():
prev_n = xTrain.shape[0]
nValid = xValid.shape[0]
nValidBatches = int(np.ceil(nValid / batchSize))
for batch in range(nValidBatches):
xValidBatch, _ = selectCurrentBatch(batch, batchSize, L, T, nValid, device, xValid, yValid)
C, C_old, mean = update_covariance_mat(C_old, xValidBatch[L*(T-1):], gamma=gamma,
mean=mean, stationary=stationary, n=prev_n+batch*batchSize)
GNN.changeGSO(C.squeeze())
yHatValid = GNN(xValidBatch)
all_pred.append(yHatValid.detach())
all_pred = torch.cat(all_pred, dim=0)
yHatValid = torch.tensor(Yscaler.inverse_transform(all_pred.cpu()))
yValidInv = torch.tensor(Yscaler.inverse_transform(yValid.cpu()))
Valid_Loss = MAE(yHatValid.squeeze(), yValidInv.squeeze())
valid_MAPE = sMAPE(yValidInv.squeeze(), yHatValid.squeeze())
tot_val_mae += Valid_Loss.detach()
tot_val_mape += valid_MAPE.detach()
if tot_val_mae < Best_Valid_Loss:
Best_Valid_Loss = tot_val_mae
Best_GNN = deepcopy(GNN)
print(f"""Epoch {epoch} train loss (MSE) {sum(tot_train_loss)} val loss (MAE) {tot_val_mae} val MAPE {tot_val_mape}""")
all_pred = []
all_pred_offline = []
GNN = deepcopy(Best_GNN)
mean = torch.cat([xTrain, xValid], dim=0).mean(0)
prev_n = xTrain.shape[0] + xValid.shape[0]
C_old = torch.cov(torch.cat([xTrain, xValid], dim=0).squeeze().T)
if args.optimizer == "Adam":
optimizer = optim.Adam(GNN.parameters(), lr=lr, weight_decay=0.001)
elif args.optimizer == "SGD":
optimizer = optim.SGD(GNN.parameters(), lr=lr)
testBatchSize = 1
nTest = yTest.shape[0]
nTestBatches = int(np.ceil(nTest / testBatchSize))
all_test_loss = []
print("Testing")
for batch in tqdm(range(nTestBatches)):
xTestBatch, yTestBatch = selectCurrentBatch(batch, testBatchSize, L, T, nTest, device, xTest, yTest)
C, C_old, mean = update_covariance_mat(C_old, xTestBatch[L*(T-1):], gamma=gamma,
mean=mean, stationary=stationary, n=prev_n+batch*batchSize)
GNN.changeGSO(C)
GNN.zero_grad()
yHatTestBatch = GNN(xTestBatch)
lossValueTest = Loss(yHatTestBatch.squeeze(), yTestBatch.squeeze())
lossValueTest.backward()
optimizer.step()
all_pred.append(yHatTestBatch.detach())
all_test_loss.append(lossValueTest.detach())
all_pred = torch.cat(all_pred, dim=0)
yBestTest = torch.tensor(Yscaler.inverse_transform(all_pred.cpu()))
yTestInv = torch.tensor(Yscaler.inverse_transform(yTest.cpu()))
mae_test = MAE(yBestTest , yTestInv)
mse_test = MSE(yBestTest , yTestInv)
mape_test = sMAPE(yTestInv, yBestTest)
print("MSE test: ", mse_test.item(), "MAE test: ", mae_test.item(), "sMAPE test: ", mape_test.item())