forked from saleha1wer/MolGen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhpo.py
174 lines (141 loc) · 7.65 KB
/
hpo.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
import os
from unicodedata import name
import numpy as np
import pytorch_lightning as pl
import optuna
import time
from sklearn.model_selection import train_test_split
from functools import partial
from data_module import GNNDataModule, MoleculeDataset, create_pretraining_finetuning_DataModules
from ray import tune
from ray.tune import CLIReporter
from ray.tune.schedulers import ASHAScheduler, PopulationBasedTraining, HyperBandForBOHB
from ray.tune.integration.pytorch_lightning import TuneReportCallback, TuneReportCheckpointCallback
from ray.tune.suggest.optuna import OptunaSearch
from ray.tune.suggest.bohb import TuneBOHB
from ray.tune.suggest.bayesopt import BayesOptSearch
from ray.tune.suggest.hyperopt import HyperOptSearch
from torch_geometric.nn.models import GIN, GAT, PNA, GraphSAGE
from ray.tune.utils import wait_for_gpu
from finetune import finetune
from datetime import datetime
import torch
from network import GNN
test_pretraining_epochs = 125
test_finetuning_epochs = 75
raytune_callback = TuneReportCheckpointCallback(
metrics={
'loss': 'val_loss'
},
filename='checkpoint',
on='validation_end')
# this function makes a custom logging directory name since the normal way (concat all ...
# the parameters) makes the name too long for windows and it errors on creation of the logging dir
def trial_name_generator(trial):
try:
namestring = str(trial.config['N']) + str(trial.config['E']) + str(trial.config['hidden']) + str(
trial.config['n_layers']) + str(trial.trial_id)
except:
namestring = str(trial.config['order']) + str(trial.config['trade_off_backbone']) + str(trial.config['trade_off_head'])
return namestring
def save_loss_and_config(val_loss='', test_loss='', configuration=''):
now_string = datetime.now().strftime("%d-%m-%Y-%H-%M-%S")
file = open("HPO_result_logs/" + now_string + "-HPO_result.txt", 'w')
message = f"Test loss achieved: {str(test_loss)} \nVal loss achieved:{str(val_loss)} \nConfiguration found: {str(configuration)}"
file.write(message)
def meta_hpo_basic(train_epochs, n_samples,space, data_module,report_test_loss = True):
best_configuration, best_val_loss = run_hpo_basic(train_epochs, n_samples,
data_module, space)
return best_val_loss, best_configuration
def meta_hpo_finetuning(finetune_epochs, patience,n_samples, train_size,source_model, space,report_test_loss = True):
batch_size = 64
no_a2a = True # use a2a data or not in adenosine set
no_a2a = '_no_a2a' if no_a2a else ''
# for prot_target_encoding choose: None or 'one-hot-encoding'
# if choosing one-hot-encoding change input_heads in gnn_config
gnn_config = space
pre_datamodule, fine_datamodule = create_pretraining_finetuning_DataModules(batch_size, no_a2a, train_size)
best_configuration, best_val_loss = run_hpo_finetuning(finetune_epochs, patience, n_samples,
fine_datamodule, gnn_config,source_model)
return best_val_loss, best_configuration
def calculate_test_loss(pre_datamodule, finetune_data_module, config):
pretrain_model = GNN(config)
trainer = pl.Trainer(max_epochs=test_pretraining_epochs,
accelerator=config['accelerator'],
devices=1,
enable_progress_bar=True,
enable_checkpointing=True,
callbacks=[raytune_callback])
trainer.fit(pretrain_model, pre_datamodule)
finetuned_model = finetune(save_model_name='final_',
source_model=pretrain_model,
data_module=finetune_data_module,
epochs=test_finetuning_epochs,
patience=config['patience'],
trade_off_backbone=config['trade_off_backbone'],
trade_off_head=config['trade_off_head'],
order=config['order'],
report_to_raytune=False)
test_result = trainer.test(finetuned_model, finetune_data_module)
torch.save(finetuned_model.state_dict(), 'models/final_model')
return test_result
def run_hpo_finetuning(finetune_epochs,patience, n_samples, fine_data_module, gnn_config, source_model):
def train_tune(config):
finetuned_model = finetune(save_model_name = 'final_',
source_model = source_model,
data_module = fine_data_module,
epochs=finetune_epochs,
patience=patience,
trade_off_backbone=config['trade_off_backbone'],
trade_off_head=config['trade_off_head'],
order=config['order'],
report_to_raytune=True)
start_time = time.time()
tpe = HyperOptSearch(
metric="loss", mode="min", n_initial_points=10)
analysis = tune.run(partial(train_tune),
config=gnn_config,
num_samples=n_samples, # number of samples taken in the entire sample space
search_alg=tpe,
resources_per_trial={
gnn_config['accelerator']: 1},
local_dir=os.getcwd(),
trial_dirname_creator=trial_name_generator)
print('Finished hyperparameter optimization.')
best_configuration = analysis.get_best_config(metric='loss', mode='min', scope='last')
best_trial = analysis.get_best_trial(metric='loss', mode='min', scope='last')
end_time = time.time()
print(f"Elapsed time:{end_time - start_time}")
return best_configuration, best_trial.last_result['loss']
def run_hpo_basic(max_epochs, n_samples, data_module, gnn_config):
def train_tune(config):
model = GNN(config)
trainer = pl.Trainer(max_epochs=max_epochs,
accelerator=gnn_config['accelerator'],
devices=1,
enable_progress_bar=True,
enable_checkpointing=True,
callbacks=[raytune_callback])
trainer.fit(model, data_module)
start = time.time()
tpe = HyperOptSearch(
metric="loss", mode="min", n_initial_points=10)
analysis = tune.run(partial(train_tune),
config=gnn_config,
num_samples=n_samples, # number of samples taken in the entire sample space
search_alg=tpe,
local_dir=os.getcwd(),
resources_per_trial={
gnn_config['accelerator']: 1
# 'memory' : 10 * 1024 * 1024 * 1024
})
print('Finished hyperparameter optimization.')
best_configuration = analysis.get_best_config(metric='loss', mode='min', scope='last')
best_trial = analysis.get_best_trial(metric='loss', mode='min', scope='last')
print(f"Best trial configuration:{best_trial.config}")
print(f"Best trial final validation loss:{best_trial.last_result['loss']}")
# print(f"attempting to load from dir: {best_trial.checkpoint.value}")
# print(f"attempting to load file: {best_trial.checkpoint.value + 'checkpoint'}")
best_checkpoint_model = GNN.load_from_checkpoint(best_trial.checkpoint.value + '/checkpoint')
torch.save(best_checkpoint_model.state_dict(), 'models_saved/GIN_afterHPO')
return best_configuration,best_trial.last_result['loss']