-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
378 lines (327 loc) · 17.2 KB
/
main.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
import yaml
import argparse
import pickle
import ray
import numpy as np
from skopt.space import Real, Categorical,Integer
from wwlgpr.Utility import ClassifySpecies, writetofile, writetofolder
from wwlgpr.WWL_GPR import BayOptCv
import wwlgpr
import os, sys
from sklearn.model_selection import KFold,LeaveOneOut,StratifiedKFold
import time
def load_base_path():
return os.path.dirname(wwlgpr.__path__[0])
def load_db(ml_dict, task):
global base_path
if task == "train":
label = "database"
if task == "test" :
label = "test_database"
with open(base_path + ml_dict[label]["db_graphs"],'rb') as infile:
db_graphs = np.array(pickle.load(infile))
with open(base_path + ml_dict[label]["db_atoms"],'rb') as infile:
db_atoms = np.array(pickle.load(infile), dtype=object)
with open(base_path + ml_dict[label]["node_attributes"],'rb') as infile:
node_attributes = np.array(pickle.load(infile), dtype=object)
with open(base_path + ml_dict[label]["target_properties"],'rb') as infile:
list_ads_energies = np.array(pickle.load(infile))
with open(base_path + ml_dict[label]["file_names"],'rb') as infile:
file_names = np.array(pickle.load(infile))
return db_graphs, db_atoms, node_attributes, list_ads_energies, file_names
def SCV5(
ml_dict,
opt_dimensions,
default_para,
fix_hypers,
):
"""task1 and task2: 5-fold cross validation for in-domain prediction stratified by adsorbate
Args:
ml_dict ([type]): ML setting
default_para ([type]): user defined trials
opt_dimensions ([type]): dimensions object for skopt
fix_hypers ([type]): fixed hyperparameters
"""
global output_name
db_graphs, db_atoms, node_attributes, list_ads_energies, file_names = load_db(ml_dict, "train")
test_RMSEs = []
f_times = 1
skf=StratifiedKFold(n_splits=5, random_state=25, shuffle=True)
writetofolder(output_name)
for train_index, vali_index in skf.split(list_ads_energies, ClassifySpecies(file_names)):
train_db_graphs = db_graphs[train_index]
train_db_atoms = db_atoms[train_index]
train_node_attributes = node_attributes[train_index]
train_list_ads_energies = list_ads_energies[train_index]
train_file_names = file_names[train_index]
test_db_graphs = db_graphs[vali_index]
test_db_atoms = db_atoms[vali_index]
test_node_attributes = node_attributes[vali_index]
test_list_ads_energies = list_ads_energies[vali_index]
test_file_names = file_names[vali_index]
#initialize bayesian optimization
bayoptcv = BayOptCv(
classifyspecies = ClassifySpecies(train_file_names),
num_cpus = int(ml_dict["num_cpus"]),
db_graphs = train_db_graphs,
db_atoms = train_db_atoms,
node_attributes = train_node_attributes,
y = train_list_ads_energies,
drop_list = None,
num_iter = int(ml_dict["num_iter"]),
pre_data_type = ml_dict["pre_data_type"],
filenames = train_file_names
)
#starting bayesian optimization to minimize likelihood
res_opt = bayoptcv.BayOpt(
opt_dimensions = opt_dimensions,
default_para = default_para,
fix_hypers = fix_hypers
)
print("hyperparameters:" , res_opt.x)
#prediction with the use of optimized hyperparameters
test_RMSE, test_pre = bayoptcv.Predict(
test_graphs = test_db_graphs,
test_atoms = test_db_atoms,
test_node_attributes = test_node_attributes,
test_target = test_list_ads_energies,
opt_hypers = dict(zip(opt_dimensions.keys(), res_opt.x))
)
print(f"{f_times} fold RMSE: ",test_RMSE)
test_RMSEs.append(test_RMSE)
f_times +=1
writetofile(output_name, test_file_names, test_list_ads_energies, test_pre)
print("Cross validation RMSE: ",np.mean(test_RMSEs))
def SCV5_FHP(
ml_dict,
fix_hypers
):
"""task3: 5-fold cross validation stratified by adsorbate with fixed hyperparameters
Args:
ml_dict ([type]): ML setting
fix_hypers ([type]): fixed hyperparameters
"""
global output_name
db_graphs, db_atoms, node_attributes, list_ads_energies, file_names = load_db(ml_dict,"train")
test_RMSEs = []
f_times = 1
skf=StratifiedKFold(n_splits=5, random_state=25, shuffle=True)
writetofolder(output_name)
for train_index, vali_index in skf.split(list_ads_energies, ClassifySpecies(file_names)):
train_db_graphs = db_graphs[train_index]
train_db_atoms = db_atoms[train_index]
train_node_attributes = node_attributes[train_index]
train_list_ads_energies = list_ads_energies[train_index]
train_file_names = file_names[train_index]
test_db_graphs = db_graphs[vali_index]
test_db_atoms = db_atoms[vali_index]
test_node_attributes = node_attributes[vali_index]
test_list_ads_energies = list_ads_energies[vali_index]
test_file_names = file_names[vali_index]
bayoptcv = BayOptCv(
classifyspecies = ClassifySpecies(train_file_names),
num_cpus = int(ml_dict["num_cpus"]),
db_graphs = train_db_graphs,
db_atoms = train_db_atoms,
node_attributes = train_node_attributes,
y = train_list_ads_energies,
drop_list = None,
num_iter = int(ml_dict["num_iter"]),
pre_data_type = ml_dict["pre_data_type"],
filenames = train_file_names
)
test_RMSE, test_pre = bayoptcv.Predict(
test_graphs = test_db_graphs,
test_atoms = test_db_atoms,
test_node_attributes = test_node_attributes,
test_target = test_list_ads_energies,
opt_hypers = fix_hypers
)
print(f"{f_times} fold RMSE: ",test_RMSE)
test_RMSEs.append(test_RMSE)
f_times +=1
writetofile(output_name, test_file_names, test_list_ads_energies, test_pre)
print("Cross validation RMSE: ", np.mean(test_RMSEs))
def Extrapolation(
ml_dict,
fix_hypers
):
"""task4: extrapolation to CuCo alloy and new metal (Pt) when training pure metal database where
additionally include atomic species on Pt in the database. with pre-optimized hyperparameters.
Args:
ml_dict ([type]): ML setting
fix_hypers ([type]): fixed hyperparameters
"""
global output_name
train_db_graphs, train_db_atoms, train_node_attributes, \
train_list_ads_energies, train_file_names = load_db(ml_dict, "train")
test_db_graphs, test_db_atoms, test_node_attributes, \
test_list_ads_energies, test_file_names = load_db(ml_dict, "test")
#initialize bayesian optimization
bayoptcv = BayOptCv(
classifyspecies = ClassifySpecies(train_file_names),
num_cpus = int(ml_dict["num_cpus"]),
db_graphs = train_db_graphs,
db_atoms = train_db_atoms,
node_attributes = train_node_attributes,
y = train_list_ads_energies,
drop_list = None,
num_iter = int(ml_dict["num_iter"]),
pre_data_type = ml_dict["pre_data_type"],
filenames = train_file_names
)
test_RMSE, test_pre = bayoptcv.Predict(
test_graphs = test_db_graphs,
test_atoms = test_db_atoms,
test_node_attributes = test_node_attributes,
test_target = test_list_ads_energies,
opt_hypers = fix_hypers
)
writetofolder(output_name)
writetofile(output_name, test_file_names, test_list_ads_energies, test_pre)
print("extrapolation RMSE: ", test_RMSE)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Physic-inspired Wasserstein Weisfeiler-Lehman Graph Gaussian Process Regression')
parser.add_argument("--task", type=str, help="type of ML task", choices=["CV5", "CV5_FHP", "Extrapolation_t1", "Extrapolation_t2", "CV5_simpleads"])
parser.add_argument("--uuid", type=str, help="uuid for ray job in HPC")
parser.add_argument("--output", type=str, help="output file name")
args = parser.parse_args()
base_path = load_base_path()
if args.output:
output_name = base_path + "/results/" + args.output + ".txt"
else:
output_name = base_path + "/results/" + time.asctime() + ".txt"
if args.task == "CV5":
#! load_setting from input.yml
print("Load ML setting from input.yml")
with open(base_path + "/database/complexads_interpolation/" + 'input.yml') as f:
ml_dict = yaml.safe_load(f)
#! initialize ray for paralleization
ray.init(address=os.environ["ip_head"], _redis_password=args.uuid)
print("Nodes in the Ray cluster:", ray.nodes())
#cutoff = Integer(name='cutoff', low = 1, high=5)
#inner_cutoff = Integer(name='inner_cutoff', low = 1, high=3)
inner_weight = Real(name='inner_weight', low = 0, high=1, prior='uniform')
outer_weight = Real(name='outer_weight', low = 0, high=1, prior='uniform')
gpr_reg = Real(name='regularization of gpr', low = 1e-3, high=1e0, prior='uniform')
gpr_len = Real(name='lengthscale of gpr', low = 1, high=100, prior="uniform")
#gpr_sigma = 1
edge_s_s = Real(name='edge weight of surface-surface', low = 0, high=1, prior="uniform")
edge_s_a = Real(name='edge weight of surface-adsorbate', low = 0, high=1, prior="uniform")
edge_a_a = Real(name='edge weight of adsorbate-adsorbate', low = 0, high=1, prior="uniform")
fix_hypers = { "cutoff" : 2,
"inner_cutoff" : 1,
"gpr_sigma" : 1
}
opt_dimensions = {
"inner_weight" : inner_weight,
"outer_weight" : outer_weight,
"gpr_reg" : gpr_reg,
"gpr_len" : gpr_len,
"edge_s_s" : edge_s_s,
"edge_s_a" : edge_s_a,
"edge_a_a" : edge_a_a
}
default_para = [[1.0, 0, 0.03, 30, 0, 1, 0],
[0.6, 0.0544362754971445, 0.00824480194221483, 11.4733820390901, 0, 1, 0.6994924119498536]
]
SCV5(
ml_dict = ml_dict,
opt_dimensions = opt_dimensions,
default_para = default_para,
fix_hypers = fix_hypers
)
if args.task.startswith("Extrapolation"):
#! load_setting from input.yml
print("Load ML setting from input.yml")
if args.task == "Extrapolation_t1":
with open(base_path + "/database/complexads_extrapolation/" + 'input_CuCo_Pt.yml') as f:
ml_dict = yaml.safe_load(f)
if args.task == "Extrapolation_t2":
with open(base_path + "/database/complexads_extrapolation/" + 'input_PdRh_Ru.yml') as f:
ml_dict = yaml.safe_load(f)
#! initialize ray for paralleization
ray.init(address=os.environ["ip_head"], _redis_password=args.uuid)
print("Nodes in the Ray cluster:", ray.nodes())
fix_hypers = { "cutoff" : 2,
"inner_cutoff" : 1,
"inner_weight" : 0.6,
"outer_weight" : 0.0167135893463353,
"gpr_reg" : 0.02682695795279726,
"gpr_len" : 22.857142857142858,
"gpr_sigma" : 1,
"edge_s_s" : 0.49642857,
"edge_s_a" : 0.4674309212968105,
"edge_a_a" : 0.49795096939871913
}
Extrapolation(
ml_dict,
fix_hypers
)
if args.task == "CV5_FHP":
#! load_setting from input.yml
print("Load ML setting from input.yml")
with open(base_path + "/database/complexads_interpolation/" + 'input.yml') as f:
ml_dict = yaml.safe_load(f)
#! initialize ray for paralleization
#ray.init(address=os.environ["ip_head"], _redis_password=args.uuid)
#print("Nodes in the Ray cluster:", ray.nodes())
#! running on local desktop or laptop
ray.init(num_cpus=ml_dict["num_cpus"])
print("Job running on {} cpus".format(ml_dict["num_cpus"]))
fix_hypers = { "cutoff" : 2,
"inner_cutoff" : 1,
"inner_weight" : 0.6,
"outer_weight" : 0.0544362754971445,
"gpr_reg" : 0.00824480194221483,
"gpr_len" : 11.4733820390901,
"gpr_sigma" : 1,
"edge_s_s" : 0,
"edge_s_a" : 1,
"edge_a_a" : 0.6994924119498536
}
SCV5_FHP(
ml_dict,
fix_hypers
)
if args.task == "CV5_simpleads":
#! load_setting from input.yml
print("Load ML setting from input.yml")
with open(base_path + "/database/simpleads_interpolation/" + 'input.yml') as f:
ml_dict = yaml.safe_load(f)
#! initialize ray for paralleization
ray.init(address=os.environ["ip_head"], _redis_password=args.uuid)
print("Nodes in the Ray cluster:", ray.nodes())
cutoff = Integer(name='cutoff', low = 1, high=5)
inner_cutoff = Integer(name='inner_cutoff', low = 1, high=3)
inner_weight = Real(name='inner_weight', low = 0, high=1, prior='uniform')
outer_weight = Real(name='outer_weight', low = 0, high=1, prior='uniform')
gpr_reg = Real(name='regularization of gpr', low = 1e-3, high=1e0, prior='uniform')
gpr_len = Real(name='lengthscale of gpr', low = 1, high=100, prior="uniform")
#gpr_sigma = 1
edge_s_s = Real(name='edge weight of surface-surface', low = 0, high=1, prior="uniform")
edge_s_a = Real(name='edge weight of surface-adsorbate', low = 0, high=1, prior="uniform")
edge_a_a = Real(name='edge weight of adsorbate-adsorbate', low = 0, high=1, prior="uniform")
fix_hypers = {
"gpr_sigma" : 1
}
opt_dimensions = {
"cutoff" : cutoff,
"inner_cutoff" : inner_cutoff,
"inner_weight" : inner_weight,
"outer_weight" : outer_weight,
"gpr_reg" : gpr_reg,
"gpr_len" : gpr_len,
"edge_s_s" : edge_s_s,
"edge_s_a" : edge_s_a,
"edge_a_a" : edge_a_a
}
default_para = [[1.0, 1, 0.03, 30, 0, 1, 0],
[1.0, 0, 0.00428050586923317, 14.5556734435333, 0, 1, 0.8678710596753815]
]
SCV5(
ml_dict = ml_dict,
opt_dimensions = opt_dimensions,
default_para = default_para,
fix_hypers = fix_hypers
)