-
Notifications
You must be signed in to change notification settings - Fork 20
/
acnn.py
256 lines (217 loc) · 7.83 KB
/
acnn.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
import deepchem as dc
import json
import numpy as np
from copy import deepcopy
from hyperopt import hp, fmin, tpe
from shutil import copyfile
from utils import init_trial_path, load_dataset, EarlyStopper
def load_model(save_path, args, tasks, hyperparams):
if args['dataset'] in ['PDBbind']:
mode = 'regression'
else:
raise ValueError('Unexpected dataset: {}'.format(args['dataset']))
if args['featurizer'] == 'AC':
f1_num_atoms = 100
f2_num_atoms = 1000
c_num_atoms = f1_num_atoms + f2_num_atoms
max_num_neighbors = 12
if args['model'] == 'ACNN':
model = dc.models.AtomicConvModel(
n_tasks=len(tasks),
frag1_num_atoms=f1_num_atoms,
frag2_num_atoms=f2_num_atoms,
complex_num_atoms=c_num_atoms,
max_num_neighbors=max_num_neighbors,
layer_sizes=hyperparams['layer_sizes'],
dropout=hyperparams['dropout'],
learning_rate=hyperparams['lr'],
batch_size=12,
model_dir=save_path,
)
else:
raise ValueError('Unexpected model: {}'.format(args['model']))
return model
def main(save_path, args, hyperparams):
# Dataset
args, tasks, all_dataset, transformers = load_dataset(args)
train_set, val_set, test_set = all_dataset
# Metric
if args['metric'] == 'roc_auc':
metric = dc.metrics.Metric(dc.metrics.roc_auc_score, np.mean)
elif args['metric'] == 'rmse':
metric = dc.metrics.Metric(dc.metrics.rms_score, np.mean)
elif args['metric'] == 'r2':
metric = dc.metrics.Metric(dc.metrics.pearson_r2_score, np.mean)
else:
raise ValueError('Unexpected metric: {}'.format(args['metric']))
all_run_val_metrics = []
all_run_test_metrics = []
for _ in range(args['num_runs']):
# Model
model = load_model(save_path, args, tasks, hyperparams)
# Object for early stop tracking
stopper = EarlyStopper(save_path, args['metric'], args['patience'])
# 100 for maximum number of epochs
for epoch in range(100):
model.fit(
train_set,
nb_epoch=1,
max_checkpoints_to_keep=1,
deterministic=False,
restore=epoch > 0)
val_metric = model.evaluate(val_set, [metric], transformers)
if args['metric'] == 'roc_auc':
val_metric = val_metric['mean-roc_auc_score']
if args['metric'] == 'rmse':
val_metric = val_metric['mean-rms_score']
if args['metric'] == 'r2':
val_metric = val_metric['mean-pearson_r2_score']
# Early stop
to_stop = stopper(model, val_metric)
if to_stop:
break
stopper.load_keras_model(model)
val_metric = model.evaluate(val_set, [metric], transformers)
test_metric = model.evaluate(test_set, [metric], transformers)
if args['metric'] == 'roc_auc':
val_metric = val_metric['mean-roc_auc_score']
test_metric = test_metric['mean-roc_auc_score']
elif args['metric'] == 'rmse':
val_metric = val_metric['mean-rms_score']
test_metric = test_metric['mean-rms_score']
elif args['metric'] == 'r2':
val_metric = val_metric['mean-pearson_r2_score']
test_metric = test_metric['mean-pearson_r2_score']
all_run_val_metrics.append(val_metric)
all_run_test_metrics.append(test_metric)
with open(save_path + '/eval.txt', 'w') as f:
f.write('Best val {}: {:.4f} +- {:.4f}\n'.format(
args['metric'], np.mean(all_run_val_metrics),
np.std(all_run_val_metrics)))
f.write('Test {}: {:.4f} +- {:.4f}\n'.format(args['metric'],
np.mean(all_run_test_metrics),
np.std(all_run_test_metrics)))
with open(save_path + '/configure.json', 'w') as f:
json.dump(hyperparams, f, indent=2)
return all_run_val_metrics, all_run_test_metrics
def init_hyper_search_space(args):
# Model-based search space
if args['model'] == 'ACNN':
search_space = {
'lr':
hp.uniform('lr', low=1e-4, high=3e-1),
'layer_sizes':
hp.choice('layer_sizes', [[64, 64, 32], [32, 32, 16], [16, 16, 8]]),
'dropout':
hp.uniform('dropout', low=0., high=0.6),
}
else:
raise ValueError('Unexpected model: {}'.format(args['model']))
return search_space
def bayesian_optimization(args):
results = []
candidate_hypers = init_hyper_search_space(args)
def objective(hyperparams):
configure = deepcopy(args)
save_path = init_trial_path(args)
val_metrics, test_metrics = main(save_path, configure, hyperparams)
if args['metric'] in ['roc_auc', 'r2']:
# To maximize a non-negative value is equivalent to minimize its opposite number
val_metric_to_minimize = -1 * np.mean(val_metrics)
else:
val_metric_to_minimize = np.mean(val_metrics)
results.append((save_path, val_metric_to_minimize, val_metrics,
test_metrics))
return val_metric_to_minimize
fmin(
objective,
candidate_hypers,
algo=tpe.suggest,
max_evals=args['num_trials'])
results.sort(key=lambda tup: tup[1])
best_trial_path, _, best_val_metrics, best_test_metrics = results[0]
copyfile(best_trial_path + '/configure.json',
args['result_path'] + '/configure.json')
copyfile(best_trial_path + '/eval.txt', args['result_path'] + '/eval.txt')
return best_val_metrics, best_test_metrics
if __name__ == '__main__':
import argparse
from utils import mkdir_p
parser = argparse.ArgumentParser('Examples for MoleculeNet with ACNN')
parser.add_argument(
'-d',
'--dataset',
choices=['PDBbind'],
default='PDBbind',
help='Dataset to use')
parser.add_argument(
'-m',
'--model',
choices=['ACNN'],
default='ACNN',
help=
'Options include 1) Atomic Convolutional Neural Network (ACNN) (default: ACNN)'
)
parser.add_argument(
'-f',
'--featurizer',
choices=['AC'],
default='AC',
help='Options include 1) Atomic Convolution (AC) (default: AC)')
parser.add_argument(
'-p',
'--result-path',
type=str,
default='results',
help='Path to save training results (default: results)')
parser.add_argument(
'-r',
'--num-runs',
type=int,
default=3,
help='Number of runs for each hyperparameter configuration (default: 3)')
parser.add_argument(
'-pa',
'--patience',
type=int,
default=30,
help='Number of epochs to wait before early stop if validation performance '
'stops getting improved (default: 30)')
parser.add_argument(
'-hs',
'--hyper-search',
action='store_true',
help='Whether to perform hyperparameter search '
'or use the default configuration. (default: False)')
parser.add_argument(
'-nt',
'--num-trials',
type=int,
default=16,
help='Number of trials for hyperparameter search (default: 16)')
parser.add_argument(
'-me',
'--metric',
type=str,
choices=['rmse', 'r2'],
default='rmse',
help=
'Validation metric to optimize. Options inclue 1) rmse and 2) r2 (default: rmse)'
)
args = parser.parse_args().__dict__
mkdir_p(args['result_path'])
if args['hyper_search']:
print('Start hyperparameter search with Bayesian '
'optimization for {:d} trials'.format(args['num_trials']))
val_metrics, test_metrics = bayesian_optimization(args)
else:
print('Use the manually specified hyperparameters')
with open('configures/{}_{}/{}.json'.format(
args['model'], args['featurizer'], args['dataset'])) as f:
default_hyperparams = json.load(f)
val_metrics, test_metrics = main(args['result_path'], args,
default_hyperparams)
print('Val metric for 3 runs: {:.4f} +- {:.4f}'.format(
np.mean(val_metrics), np.std(val_metrics)))
print('Test metric for 3 runs: {:.4f} +- {:.4f}'.format(
np.mean(test_metrics), np.std(test_metrics)))