-
Notifications
You must be signed in to change notification settings - Fork 1
/
dl_tune.py
74 lines (61 loc) · 2.99 KB
/
dl_tune.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
import hydra
import lightning as L
from lightning.pytorch.callbacks import EarlyStopping, ModelCheckpoint
from lightning.pytorch.loggers import CSVLogger, WandbLogger
from omegaconf import DictConfig, OmegaConf
import wandb
from datasets.loader.datamodule import EhrDataModule
from datasets.loader.load_los_info import get_los_info
from pipelines import DlPipeline
# import os
# os.environ['WANDB_MODE'] = 'offline'
# os.environ['WANDB_LOG_LEVEL'] = 'debug'
project_name = "pyehr"
hydra.initialize(config_path="configs", version_base=None)
cfg = OmegaConf.to_container(hydra.compose(config_name="config"))
dataset_config = {
'tjh': {'demo_dim': 2, 'lab_dim': 73},
'cdsl': {'demo_dim': 2, 'lab_dim': 97},
}
sweep_configuration = {
'method': 'grid',
'name': 'sweep_dl_tjh',
'metric': {'goal': 'minimize', 'name': 'val_loss'},
'parameters':
{
'task': {'values': ['outcome', 'los', 'multitask']},
'dataset': {'values': ['tjh']},
'model': {'values': ['MLP', 'GRU', 'RNN', 'LSTM', 'TCN', 'Transformer', 'AdaCare', 'Agent', 'GRASP', 'RETAIN', 'StageNet', 'ConCare']},
'batch_size': {'values': [64]}, # TJH: 64, CDSL: 128
'hidden_dim': {'values': [32, 64, 128]},
'learning_rate': {'values': [1e-2, 1e-3, 1e-4]},
'fold': {'values': [0]},
'seed': {'values': [42]},
}
}
sweep_id = wandb.sweep(sweep_configuration, project=project_name)
def run_experiment():
run = wandb.init(project=project_name, config=cfg)
wandb_logger = WandbLogger(project=project_name, log_model=True) # log only the last (best) checkpoint
config = wandb.config
config.update(dataset_config[config['dataset']], allow_val_change=True)
los_config = get_los_info(f'datasets/{config["dataset"]}/processed/fold_{config["fold"]}')
main_metric = "mae" if config["task"] == "los" else "auprc"
config.update({"los_info": los_config, "main_metric": main_metric})
# data
dm = EhrDataModule(f'datasets/{config["dataset"]}/processed/fold_{config["fold"]}', batch_size=config["batch_size"])
# EarlyStop and checkpoint callback
if config["task"] in ["outcome", "multitask"]:
early_stopping_callback = EarlyStopping(monitor="auprc", patience=config["patience"], mode="max",)
checkpoint_callback = ModelCheckpoint(monitor="auprc", mode="max")
elif config["task"] == "los":
early_stopping_callback = EarlyStopping(monitor="mae", patience=config["patience"], mode="min",)
checkpoint_callback = ModelCheckpoint(monitor="mae", mode="min")
L.seed_everything(config["seed"]) # seed for reproducibility
# train/val/test
pipeline = DlPipeline(config.as_dict())
trainer = L.Trainer(accelerator="gpu", devices=[1], max_epochs=config["epochs"], logger=wandb_logger, callbacks=[early_stopping_callback, checkpoint_callback])
trainer.fit(pipeline, dm)
print("Best Score", checkpoint_callback.best_model_score)
if __name__ == "__main__":
wandb.agent(sweep_id, function=run_experiment, project=project_name)