-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_exp_gp_los_metric.py
390 lines (325 loc) · 15.7 KB
/
run_exp_gp_los_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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#
# DKAFT
#
# Copyright (c) Siemens AG, 2021
# Authors:
# Zhiliang Wu <zhiliang.wu@siemens.com>
# License-Identifier: MIT
import copy
import gc
from functools import partial
from pathlib import Path
import shutil
import uuid
import numpy as np
import pandas as pd
import mlflow
import torch
from torch import nn
from torch.optim import Adam
from torch.optim.lr_scheduler import StepLR
import gpytorch
from sklearn.metrics import mean_squared_error, r2_score
import ignite
from ignite.contrib.handlers.mlflow_logger import MLflowLogger, \
global_step_from_engine
from ignite.contrib.handlers import ProgressBar
from ignite.contrib.handlers.param_scheduler import LRScheduler
from ignite.contrib.metrics.regression import R2Score
from ignite.engine import Events
from ignite.handlers import DiskSaver, EarlyStopping
from ignite.metrics import MeanSquaredError, Average
from data_utils import get_tensor_loaders_in_file, prepare_batch_tensor
from gp_layer import SVGPLayer
from plot_utils import residual_plot
from logging_conf import logger
from model_utils import SequenceFeature, LinearModel, \
get_initial_inducing_points_seq, DKLModel, create_dkl_trainer, \
create_dkl_evaluator, EpochOutputStore, CheckPointAfter
from pml_los import run_metric_learning
def run(batch_size=64, lr=3e-4, alpha=1e-3, n_hidden_sta=4, n_hidden_temp=32,
num_gp=1, num_inducing=10, model_name='lstm', n_embedding_temp=32,
len_int=1, len_ts=48, epoch=50, fold_idx=0,
device=torch.device('cpu'), exp_name='dataset_xxx',
run_name='model_xxx', seed=42):
"""Run the experiment with the metric learning as the pre-training.
Args:
batch_size (int): Batch size.
lr (float): The value of the learning rate, possibly from lrfinder.
alpha (float): The value of weight decay (a.k.a. regularization).
n_hidden_sta (int): The dimension of the hidden static
representations.
n_hidden_temp (int): The dimension of the hidden sequential
representations.
num_gp (int): The number of involved GPs.
num_inducing (int): The multiple of inducing points. The total number
of inducing points is num_inducing x batch_size.
model_name (str): The name of the backbone.
n_embedding_temp (int): The dimension of the temporal embeddings.
len_int (int): The length of the time interval (hours).
len_ts (int): The length of the number of time steps (hours).
epoch (int): The number of training epochs.
fold_idx (int): The index of the training/validation set.
device (torch.device or str): The device to load the models.
exp_name (str): The name of the experiments with a format of
dataset_xxx, which defines the experiment name inside MLflow.
run_name (str): The name of the run with a format of
[model_name]_linear_regressor, which defines the run name inside
MLflow.
seed (int): The number of the random seed to ensure the reproducibility.
Returns:
None: The evolution of training loss and evaluation loss are saved to
MLflow.
"""
np.random.seed(seed)
torch.manual_seed(seed)
backbone, _ = run_metric_learning(batch_size=batch_size,
lr=3e-4,
n_hidden_sta=n_hidden_sta,
n_hidden_temp=n_hidden_temp,
model_name=model_name,
n_embedding_temp=n_embedding_temp,
epoch=200,
len_int=len_int,
len_ts=len_ts,
fold_idx=fold_idx,
device=device,
exp_name=exp_name,
run_name=f'{run_name}_metric',
seed=seed
)
dp = Path(f'../mimic_setc')
fn = f'imputed-normed-ep_{len_int}_{len_ts}_los.data'
data_fp = dp / fn
split_df = pd.read_csv(dp / f'{fn[:-5]}_split.csv', index_col=0)
train_loader, valid_loader, test_loader, n_feature_sta, n_feature_temp = \
get_tensor_loaders_in_file(data_fp, split_df,
train_batch_size=batch_size,
valid_batch_size=batch_size,
n_fold=fold_idx)
feature_extractor = backbone.to(device)
inducing_points = get_initial_inducing_points_seq(feature_extractor,
train_loader,
device,
num_inducing=num_inducing)
gp_layer = SVGPLayer(inducing_points=inducing_points)
model = DKLModel(feature_extractor=feature_extractor, gp_layer=gp_layer)
likelihood = gpytorch.likelihoods.GaussianLikelihood()
model = model.to(device)
likelihood = likelihood.to(device)
model_bk = copy.deepcopy(model)
likelihood_bk = copy.deepcopy(likelihood)
optimizer = Adam([{'params': model.feature_extractor.parameters(),
'weight_decay': alpha,
'lr': lr
},
{'params': model.gp_layer.parameters()},
{'params': likelihood.parameters()}
], lr=0.01)
mll = gpytorch.mlls.VariationalELBO(likelihood, model.gp_layer,
num_data=len(train_loader.dataset))
# mll = gpytorch.mlls.PredictiveLogLikelihood(likelihood, model.gp_layer,
# num_data=len(train_loader.dataset))
def train_output_transform(x, y, y_pred, loss):
return {'y': y, 'y_pred': y_pred, 'loss': loss.item()}
prepare_batch_tensor_reshape = partial(prepare_batch_tensor,
new_shape=(-1,))
trainer = create_dkl_trainer(model, likelihood, optimizer, mll,
device=device,
prepare_batch=prepare_batch_tensor_reshape,
output_transform=train_output_transform)
train_metrics = {'mll': Average(output_transform=lambda x: -x['loss']),
'mse': MeanSquaredError(),
'r2score': R2Score()
}
for name, metric in train_metrics.items():
metric.attach(trainer, name)
pbar = ProgressBar(persist=True)
pbar.attach(trainer,
output_transform=lambda out: {'batch loss': out['loss']})
# evaluators
val_metrics = {'mse': MeanSquaredError(),
'r2score': R2Score()
}
for name, metric in val_metrics.items():
metric.attach(trainer, name)
evaluator = create_dkl_evaluator(model, likelihood, metrics=val_metrics,
device=device,
prepare_batch=prepare_batch_tensor_reshape
)
pbar.attach(evaluator)
eos_train = EpochOutputStore(output_transform=lambda out: (out['y_pred'],
out['y']))
eos_valid = EpochOutputStore()
eos_train.attach(trainer)
eos_valid.attach(evaluator)
mlflow.set_experiment(exp_name)
with mlflow.start_run(run_name=run_name):
mlflow_logger = MLflowLogger()
mlflow_logger.log_params({
'seed': seed,
'batch_size': batch_size,
'num_epoch': epoch,
'model': model_name,
'number inducing points': int(num_gp * num_inducing * batch_size),
'gp_input_dim': n_hidden_sta+n_hidden_temp,
'weight_decay': alpha,
'hidden_dim_sta': n_hidden_sta,
'hidden_dim_temp': n_hidden_temp,
'embedding_temp': n_embedding_temp,
'fold_index': fold_idx,
'file_data': fn,
'pytorch version': torch.__version__,
'ignite version': ignite.__version__,
'cuda version': torch.version.cuda,
'device name': torch.cuda.get_device_name(0)
})
# handlers for evaluator
# note, this actually calls the evaluator
@trainer.on(Events.EPOCH_COMPLETED)
def log_validation_results(engine):
evaluator.run(valid_loader)
metrics = evaluator.state.metrics
pbar.log_message(f"Validation Results "
f"- Epoch: {engine.state.epoch} "
f"- Mean Square Error: {metrics['mse']:.4f} "
f"- R squared: {metrics['r2score']:.2f}"
)
log_metrics = {f'validation {k}': v for k, v in metrics.items()}
mlflow_logger.log_metrics(log_metrics, step=engine.state.epoch)
temp_name = f'temp_{uuid.uuid4()}'
def score_function(engine):
return -engine.state.metrics['mse']
to_save = {'model': model,
'likelihood': likelihood,
# 'optimizer': optimizer
}
handler = CheckPointAfter(start_epoch=50,
to_save=to_save,
save_handler=DiskSaver(f'./{temp_name}',
create_dir=True),
n_saved=1,
filename_prefix='best',
score_function=score_function,
score_name="val_mse",
global_step_transform=global_step_from_engine(
trainer))
evaluator.add_event_handler(Events.COMPLETED, handler)
es_handler = EarlyStopping(patience=30, score_function=score_function,
trainer=trainer)
evaluator.add_event_handler(Events.COMPLETED, es_handler)
# handlers for trainer
@trainer.on(Events.EPOCH_COMPLETED)
def log_training_results(engine):
metrics = engine.state.metrics
pbar.log_message(f"Training Set "
f"- Epoch: {engine.state.epoch} "
f"- MLL: {metrics['mll']:.4f} "
f"- Mean Square Error: {metrics['mse']:.4f} "
f"- R squared: {metrics['r2score']:.2f}"
)
def log_plots(engine, label='valid'):
train_hist_y_p, train_hist_y = eos_train.get_output(to_numpy=True)
val_hist_y_p, val_hist_y = eos_valid.get_output(to_numpy=True)
residual_plot(train_hist_y, train_hist_y_p,
val_hist_y, val_hist_y_p, dp=temp_name,
n_epoch=engine.state.epoch, label=f'y_{label}')
trainer.add_event_handler(Events.EPOCH_COMPLETED(every=10),
log_plots, label='valid')
def final_evaluation(engine):
to_load = {'model': model_bk,
'likelihood': likelihood_bk,
}
last_checkpoint_fp = f'./{temp_name}/{handler.last_checkpoint}'
print(last_checkpoint_fp)
checkpoint = torch.load(last_checkpoint_fp, map_location=device)
CheckPointAfter.load_objects(to_load=to_load, checkpoint=checkpoint)
logger.info('The best model on validation is reloaded for '
'evaluation on the test set')
model_bk.eval()
likelihood_bk.eval()
y_true_list = []
y_pred_list = []
for batch in test_loader:
with torch.no_grad():
x, y = prepare_batch_tensor(batch, device=device,
non_blocking=False,
new_shape=(-1, )
)
output = model_bk(x)
y_pred = output.mean
y_true_list.append(y)
y_pred_list.append(y_pred)
y_true = torch.cat(y_true_list, dim=0).cpu().numpy()
y_pred = torch.cat(y_pred_list, dim=0).cpu().numpy()
test_mse = mean_squared_error(y_true, y_pred)
test_r2score = r2_score(y_true, y_pred)
pbar.log_message(f"Testing Results "
f"- Epoch: {engine.state.epoch} "
f"- Mean Square Error: {test_mse:.4f} "
f"- R squared of x: {test_r2score:.2f}"
)
log_metrics = {'test mse': test_mse,
'test r2score': test_r2score,
}
mlflow_logger.log_metrics(log_metrics, step=engine.state.epoch)
trainer.add_event_handler(Events.COMPLETED, final_evaluation)
@trainer.on(Events.COMPLETED)
def save_model_to_mlflow(engine):
mlflow_logger.log_artifacts(f'./{temp_name}/')
try:
shutil.rmtree(temp_name)
except FileNotFoundError:
logger.warning('Temp drectory not found!')
raise
# log training loss at each iteration
mlflow_logger.attach_output_handler(trainer,
event_name=Events.ITERATION_COMPLETED,
tag='training',
output_transform=lambda out: {
'batch_mll': -out['loss']}
)
# setup `global_step_transform=global_step_from_engine(trainer)` to
# take the epoch of the `trainer` instead of `train_evaluator`.
mlflow_logger.attach_output_handler(trainer,
event_name=Events.EPOCH_COMPLETED,
tag='training',
metric_names=['mll',
'mse',
'r2score']
)
# Attach the logger to the trainer to log optimizer's parameters,
# e.g. learning rate at each iteration
mlflow_logger.attach_opt_params_handler(trainer,
event_name=Events.ITERATION_STARTED,
optimizer=optimizer,
param_name='lr'
)
_ = trainer.run(train_loader, max_epochs=epoch)
if __name__ == '__main__':
sd = 42
dc = torch.device('cuda:1' if torch.cuda.is_available() else 'cpu')
# dataset parameter
exp = 'mimic_los_SetC'
interval = 1
length_time = 48
# model building/training parameters
n_hidden_s = 4
n_hidden_t = 64
n_embedding_temp = 64
bs = 256
n_epoch = 400
m_name = 'gru' # or 'gru'
a = 0.01 # this could be from linear regressor
lrt = 3e-4 # this is from lr finder
# gp parameters
n_gp = 1
n_idc = 4
r_name = f'{m_name}_svgp_metric'
for f in range(5):
run(batch_size=bs, lr=lrt, alpha=a, n_hidden_sta=n_hidden_s,
n_hidden_temp=n_hidden_t, num_gp=n_gp, num_inducing=n_idc,
model_name=m_name, n_embedding_temp=n_embedding_temp,
len_int=interval, len_ts=length_time, epoch=n_epoch, fold_idx=f,
device=dc, exp_name=exp, run_name=r_name, seed=sd)
gc.collect()