-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipeline.py
208 lines (177 loc) · 5.45 KB
/
pipeline.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
from models.pipeline_model import CLFUModel
from data_preprocessing import RawMoleculeDataset
from trainer import Trainer
from pipeline_trainer import LateFusion
from models.pretrain_model import GNNPreModel
from models.pretrain_model import SeqPreModel
import os
from config import (
gnn_cls_config,
seq_cls_config,
clfu_config,
trainer_clfu_config,
trainer_gnn_config,
trainer_seq_config,
)
cur_path = os.getcwd()
root_path = cur_path + "/dataset/{}/".format(trainer_clfu_config["dataset"])
train_dataset = RawMoleculeDataset(
root=root_path,
seed=trainer_clfu_config["seed"],
mode="train",
dataset=trainer_clfu_config["dataset"],
)
valid_dataset = RawMoleculeDataset(
root=root_path,
seed=trainer_clfu_config["seed"],
mode="valid",
dataset=trainer_clfu_config["dataset"],
)
test_dataset = RawMoleculeDataset(
root=root_path,
seed=trainer_clfu_config["seed"],
mode="test",
dataset=trainer_clfu_config["dataset"],
)
print("DataSet Loaded! Current DataSet is {}".format(
trainer_clfu_config["dataset"]))
def grid_search():
eval_dict = {}
lr_list = [
1e-2,
8e-3,
6e-3,
4e-3,
2e-3,
1e-3,
8e-4,
6e-4,
4e-4,
2e-4,
1e-4,
8e-5,
4e-5,
2e-5,
1e-5,
]
alpha_list = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
cur_config = trainer_clfu_config
cur_config["save_id"] = trainer_clfu_config["save_id"] * 100
for lr in lr_list:
cur_config["lr"] = lr
for alpha in alpha_list:
print("save_id: {}".format(cur_config["save_id"]))
model = CLFUModel(clfu_config)
gnn_cls_model = GNNPreModel(gnn_cls_config)
trainer_graph = Trainer(gnn_cls_model, trainer_gnn_config)
seq_cls_model = SeqPreModel(seq_cls_config)
trainer_seq = Trainer(seq_cls_model, trainer_seq_config)
trainer = LateFusion(
trainer_graph,
trainer_seq,
model,
cur_config,
train_dataset,
valid_dataset,
test_dataset,
)
trainer.set_alpha(alpha)
print("cur lr is: {}, cur alpha is:{}".format(lr, alpha))
trainer.train()
auc_mean, auc_std = trainer.eval_model()
print(
"cur lr is: {}, cur alpha is:{}, auc_mean:{}, auc_std:{}".format(
lr, alpha, auc_mean, auc_std
)
)
eval_dict[cur_config["save_id"]] = (lr, alpha, auc_mean, auc_std)
cur_config["save_id"] = cur_config["save_id"] + 1
for key in eval_dict.keys():
print(key, eval_dict[key])
def train_gnn_model():
eval_dict = {}
cur_gnn_config = trainer_gnn_config
cur_gnn_config["save_id"] = cur_gnn_config["save_id"] * 100
lr_list = [
1e-2,
8e-3,
6e-3,
4e-3,
2e-3,
1e-3,
8e-4,
6e-4,
4e-4,
2e-4,
1e-4,
8e-5,
6e-5,
4e-5,
2e-5,
1e-5,
]
for cur_lr in lr_list:
gnn_cls_model = GNNPreModel(gnn_cls_config)
cur_gnn_config["lr"] = cur_lr
print("cur lr is {:.6f}".format(cur_lr))
trainer = Trainer(
gnn_cls_model, cur_gnn_config, train_dataset, valid_dataset, test_dataset
)
trainer.train()
auc_mean, auc_std = trainer.eval_model()
print(
"cur lr: {} ; auc mean: {}; auc std: {}".format(
cur_lr, auc_mean, auc_std)
)
eval_dict[cur_gnn_config["save_id"]] = (cur_lr, auc_mean, auc_std)
cur_gnn_config["save_id"] = cur_gnn_config["save_id"] + 1
for key in eval_dict.keys():
print(key, eval_dict[key])
def train_seq_model():
eval_dict = {}
cur_seq_config = trainer_seq_config
cur_seq_config["save_id"] = cur_seq_config["save_id"] * 100
lr_list = [
1e-2,
8e-3,
6e-3,
4e-3,
2e-3,
1e-3,
8e-4,
6e-4,
4e-4,
2e-4,
1e-4,
8e-5,
6e-5,
4e-5,
2e-5,
1e-5,
]
for cur_lr in lr_list:
seq_cls_model = SeqPreModel(seq_cls_config)
cur_seq_config["lr"] = cur_lr
print("cur lr is {:.6f}".format(cur_lr))
trainer = Trainer(
seq_cls_model, cur_seq_config, train_dataset, valid_dataset, test_dataset
)
trainer.train()
auc_mean, auc_std = trainer.eval_model()
print("cur lr: {} auc mean: {} auc std: {}".format(
cur_lr, auc_mean, auc_std))
eval_dict[cur_seq_config["save_id"]] = (cur_lr, auc_mean, auc_std)
cur_seq_config["save_id"] = cur_seq_config["save_id"] + 1
for key in eval_dict.keys():
print(key, eval_dict[key])
if __name__ == "__main__":
# step 1: train gnn model, and select the best model to use in step 3
# you should comment the other steps code before run step 1
# train_gnn_model()
# step 2: train seq model, and select the best model to use in step 3
# you should comment the other steps code before run step 2
# train_seq_model()
# step 3: train late fusion model, use the pretrained gnn and seq model in step 1 and step 2
# fill the best ckpt id in the config file for fusion model
# you should comment the other steps code before run step 3
grid_search()