-
Notifications
You must be signed in to change notification settings - Fork 9
/
train.py
326 lines (277 loc) · 9.37 KB
/
train.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
from importlib.metadata import metadata
import numpy as np
import pandas as pd
import torch as th
from torch.nn.functional import mse_loss
from hierarchy_data import (
LabourHierarchyData,
TourismHierarchyData,
WikiHierarchyData,
normalize_data,
unnormalize_data,
)
from models.fnpmodels import EmbedMetaAttenSeq, RegressionSepFNP, Corem
from utils import lag_dataset
from models.utils import float_tensor, long_tensor
from tqdm import tqdm
import properscoring as ps
SEED = 42
DEVICE = "cuda"
DATASET = "LABOUR"
AHEAD = 8
TRAIN_UPTO = 514 - 8
BACKUP_TIME = 50
PRE_BATCH_SIZE = 10
PRE_TRAIN_LR = 0.001
PRE_TRAIN_EPOCHS = 100
FRAC_VAL = 0.1
C = 5.0
BATCH_SIZE = 10
TRAIN_LR = 0.001
LAMBDA = 0.1
TRAIN_EPOCHS = 500
EVAL_SAMPLES = 100
np.random.seed(SEED)
th.manual_seed(SEED)
th.cuda.manual_seed(SEED)
if DEVICE == "cuda":
device = th.device("cuda") if th.cuda.is_available() else th.device("cpu")
else:
device = th.device("cpu")
if DATASET == "LABOUR":
data_obj = LabourHierarchyData()
elif DATASET == "WIKI":
data_obj = WikiHierarchyData()
else:
raise ValueError("Unknown dataset")
# data_obj = normalize_data(data_obj)
# Let's create dataset
full_data = data_obj.data
train_data_raw = full_data[:, :TRAIN_UPTO]
train_means = np.mean(train_data_raw, axis=1)
train_std = np.std(train_data_raw, axis=1)
train_data = (train_data_raw - train_means[:, None]) / train_std[:, None]
# train_data = train_data_raw
dataset_raw = lag_dataset(train_data, BACKUP_TIME)
class SeqDataset(th.utils.data.Dataset):
def __init__(self, dataset):
self.X, self.Y = dataset
def __len__(self):
return len(self.X)
def __getitem__(self, idx):
return self.X[idx], self.dataset[idx]
dataset = SeqDataset(dataset_raw)
# Let's create FNP model
encoder = EmbedMetaAttenSeq(
dim_seq_in=1,
num_metadata=len(data_obj.idx_dict),
dim_metadata=1,
dim_out=60,
n_layers=2,
bidirectional=True,
).to(device)
decoder = RegressionSepFNP(
dim_x=60,
dim_y=1,
dim_h=60,
n_layers=3,
dim_u=60,
dim_z=60,
nodes=len(data_obj.idx_dict),
).to(device)
pre_opt = th.optim.Adam(
list(encoder.parameters()) + list(decoder.parameters()), lr=PRE_TRAIN_LR
)
# Create training validation set
perm = np.random.permutation(np.arange(BACKUP_TIME, TRAIN_UPTO))
train_idx = perm[: int(len(perm) * (1 - FRAC_VAL))]
val_idx = perm[int(len(perm) * (1 - FRAC_VAL)) :]
def pretrain_epoch():
encoder.train()
decoder.train()
losses = []
means, stds = [], []
pre_opt.zero_grad()
ref_x = float_tensor(train_data[:, :, None])
meta_x = long_tensor(np.arange(ref_x.shape[0]))
for i in tqdm(train_idx):
x = ref_x[:, : i - 1, :]
y = ref_x[:, i, :]
ref_out_x = encoder(ref_x, meta_x)
out_x = encoder(x, meta_x)
mean_sample, logstd_sample, log_py, log_pqz, _ = decoder(ref_out_x, out_x, y)
loss = -(log_py + log_pqz) / x.shape[0]
loss.backward()
losses.append(loss.detach().cpu().item())
means.append(mean_sample.detach().cpu().numpy())
stds.append(logstd_sample.detach().cpu().numpy())
if (i + 1) % PRE_BATCH_SIZE == 0:
pre_opt.step()
pre_opt.zero_grad()
if i % PRE_BATCH_SIZE != 0:
pre_opt.step()
return np.mean(losses), np.array(means), np.array(stds)
def pre_validate(sample=False):
encoder.eval()
decoder.eval()
losses = []
means, stds = [], []
ref_x = float_tensor(train_data[:, :, None])
meta_x = long_tensor(np.arange(ref_x.shape[0]))
for i in tqdm(val_idx):
x = ref_x[:, : i - 1, :]
y = ref_x[:, i, :]
ref_out_x = encoder(ref_x, meta_x)
out_x = encoder(x, meta_x)
y_pred, mean_y, logstd_y, _ = decoder.predict(ref_out_x, out_x, sample=sample)
mse_loss = np.mean((y_pred.cpu().numpy() - y.cpu().numpy()) ** 2)
losses.append(mse_loss)
means.append(mean_y.detach().cpu().numpy())
stds.append(logstd_y.detach().cpu().numpy())
return np.mean(losses), np.array(means), np.array(stds)
print("Pretraining...")
for ep in tqdm(range(PRE_TRAIN_EPOCHS)):
loss, means, stds = pretrain_epoch()
print(f"Epoch {ep} loss: {loss}")
with th.no_grad():
loss, means, stds = pre_validate()
print(f"Epoch {ep} Val loss: {loss}")
# Let's real_train
corem = Corem(nodes=len(data_obj.idx_dict), c=C,).to(device)
opt = th.optim.Adam(
list(encoder.parameters()) + list(decoder.parameters()) + list(corem.parameters()),
lr=TRAIN_LR,
)
def jsd_norm(mu1, mu2, var1, var2):
mu_diff = mu1 - mu2
t1 = 0.5 * (mu_diff ** 2 + (var1) ** 2) / (2 * (var2) ** 2)
t2 = 0.5 * (mu_diff ** 2 + (var2) ** 2) / (2 * (var1) ** 2)
return t1 + t2 - 1.0
def generate_hmatrix():
ans = np.zeros((len(data_obj.idx_dict), len(data_obj.idx_dict)))
for i, n in enumerate(data_obj.nodes):
if len(n.children) == 0:
ans[n.idx, n.idx] = 1
c_idx = [x.idx for x in n.children]
ans[n.idx, c_idx] = 1.0
return float_tensor(ans)
def jsd_loss(mu, logstd, hmatrix, train_means, train_std):
lhs_mu = (((mu * train_std + train_means) * hmatrix).sum(1) - train_means) / (
train_std
)
lhs_var = (((th.exp(2.0 * logstd) * (train_std ** 2)) * hmatrix).sum(1)) / (
train_std ** 2
)
ans = th.nan_to_num(jsd_norm(mu, lhs_mu, (2.0 * logstd).exp(), lhs_var))
return ans.mean()
def train_epoch():
encoder.train()
decoder.train()
corem.train()
losses = []
means, stds, gts = [], [], []
opt.zero_grad()
ref_x = float_tensor(train_data[:, :, None])
hmatrix = generate_hmatrix()
th_means = float_tensor(train_means)
th_std = float_tensor(train_std)
meta_x = long_tensor(np.arange(ref_x.shape[0]))
for i in tqdm(train_idx):
x = ref_x[:, : i - 1, :]
y = ref_x[:, i, :]
ref_out_x = encoder(ref_x, meta_x)
out_x = encoder(x, meta_x)
mean_sample1, logstd_sample1, log_py1, log_pqz, py1 = decoder(
ref_out_x, out_x, y
)
mean_sample, logstd_sample, log_py, py = corem(
mean_sample1.squeeze(), logstd_sample1.squeeze(), y
)
loss1 = -(log_py + log_pqz) / x.shape[0]
loss2 = (
jsd_loss(
mean_sample.squeeze(),
logstd_sample.squeeze(),
hmatrix,
th_means,
th_std,
)
/ x.shape[0]
)
loss = loss1 + LAMBDA * loss2
if th.isnan(loss):
import pdb
pdb.set_trace()
loss.backward()
losses.append(loss.detach().cpu().item())
print(f"Loss1: {loss1.detach().cpu().item()}")
print(f"Loss2: {loss2.detach().cpu().item()}")
means.append(mean_sample.detach().cpu().numpy())
stds.append(logstd_sample.detach().cpu().numpy())
gts.append(y.detach().cpu().numpy())
if (i + 1) % BATCH_SIZE == 0:
opt.step()
opt.zero_grad()
if i % BATCH_SIZE != 0:
opt.step()
return np.mean(losses), np.array(means), np.array(stds)
def validate(sample=False):
encoder.eval()
decoder.eval()
corem.eval()
losses = []
means, stds, gts = [], [], []
ref_x = float_tensor(train_data[:, :, None])
meta_x = long_tensor(np.arange(ref_x.shape[0]))
for i in tqdm(val_idx):
x = ref_x[:, : i - 1, :]
y = ref_x[:, i, :]
ref_out_x = encoder(ref_x, meta_x)
out_x = encoder(x, meta_x)
y_pred, mean_y, logstd_y, _ = decoder.predict(ref_out_x, out_x, sample=False)
y_pred, mean_y, logstd_y, _ = corem.predict(
mean_y.squeeze(), logstd_y.squeeze(), sample=sample
)
mse_loss = np.mean((y_pred.cpu().numpy() - y.cpu().numpy()) ** 2)
losses.append(mse_loss)
means.append(mean_y.detach().cpu().numpy())
stds.append(logstd_y.detach().cpu().numpy())
gts.append(y.cpu().numpy())
return np.mean(losses), np.array(means), np.array(stds)
print("Training....")
for ep in tqdm(range(TRAIN_EPOCHS)):
loss, means, stds = train_epoch()
print(f"Epoch {ep} loss: {loss}")
with th.no_grad():
loss, means, stds = validate()
print(f"Epoch {ep} Val loss: {loss}")
# Lets evaluate
# One sampple
def sample_data():
curr_data = train_data.copy()
encoder.eval()
decoder.eval()
corem.eval()
for t in range(AHEAD):
ref_x = float_tensor(train_data[:, :, None])
meta_x = long_tensor(np.arange(ref_x.shape[0]))
x = float_tensor(curr_data[:, :, None])
ref_out_x = encoder(ref_x, meta_x)
out_x = encoder(x, meta_x)
y_pred, mean_y, logstd_y, _ = decoder.predict(ref_out_x, out_x, sample=False)
y_pred, mean_y, logstd_y, _ = corem.predict(
mean_y.squeeze(), logstd_y.squeeze(), sample=True
)
y_pred = y_pred.cpu().numpy()
curr_data = np.concatenate([curr_data, y_pred], axis=1)
return curr_data[:, -AHEAD:]
ground_truth = full_data[:, TRAIN_UPTO : TRAIN_UPTO + AHEAD]
with th.no_grad():
preds = [sample_data() for _ in tqdm(range(EVAL_SAMPLES))]
preds = np.array(preds)
preds = preds * train_std[:, None] + train_means[:, None]
mean_preds = np.mean(preds, axis=0)
rmse = np.sqrt(np.mean((ground_truth - mean_preds) ** 2))
print(f"RMSE: {rmse}")
crps = ps.crps_ensemble(ground_truth, np.moveaxis(preds, [1, 2, 0], [0, 1, 2])).mean()
print(f"CRPS: {crps}")