-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunch_experiments.py
245 lines (222 loc) · 10.5 KB
/
launch_experiments.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
import os
import sys
import json
import subprocess
from subprocess import Popen, PIPE
import numpy as np
import numpy.random as npr
from time import sleep
from tempfile import NamedTemporaryFile
import pandas as pd
from nfpexperiment import util
LAUNCH_USING_SLURM = True
DEBUG_JOB = False
def make_sbatch_args(outfile):
return ['sbatch',
'-n', '1',
'-N', '1',
'-t', '10:00:00',
'-c', '20'
'-o', outfile]
experiment_command = sys.argv[1]
if sys.argv[2] == "test":
test_mode = True
elif sys.argv[2] == "validation":
test_mode = False
else:
raise Exception("Need to choose between validation and test mode using second argument.")
if __debug__:
N_cores = 1
N_jobs = 2
num_iters = 1
num_folds = 3
#datasets = ['toxin', 'delaney', 'malaria', 'cep']
#dataset_sizes = [100, 100, 100, 100]
datasets = ['toxin']
dataset_sizes = [7297]
else:
N_cores = 8
N_jobs = 50
num_iters = 10000
num_folds = 5
datasets = ['delaney', 'toxin', 'malaria', 'cep']
dataset_sizes = [1124, 7297, 10000, 20000]
morgan_bounds = dict(fp_length = [16, 1024],
fp_depth = [1, 4],
log_init_scale = [-2, -6],
log_step_size = [-8, -4],
log_L2_reg = [-6, 2],
h1_size = [50, 100],
conv_width = [5, 20])
neural_bounds = dict(fp_length = [16, 128], # Smaller upper range.
fp_depth = [1, 4],
log_init_scale = [-2, -6],
log_step_size = [-8, -4],
log_L2_reg = [-6, 2],
h1_size = [50, 100],
conv_width = [5, 20])
model_bounds = dict(mean = morgan_bounds,
morgan_plus_linear = morgan_bounds,
morgan_plus_net = morgan_bounds,
conv_plus_linear = neural_bounds,
conv_plus_net = neural_bounds)
def generate_params(bounds, N_jobs):
rs = npr.RandomState(0)
for jobnum in xrange(N_jobs):
yield jobnum, {param_name : rs.uniform(*param_bounds)
for param_name, param_bounds in bounds.iteritems()}
def outputfile(jobnum, dataset, model, fold):
debug_text = '_DEBUG' if __debug__ else ''
return 'results{}/{}_{}_{}_{}.json'.format(debug_text, fold, jobnum, dataset, model)
def outputfile_test(dataset, model, fold):
debug_text = '_DEBUG' if __debug__ else ''
return 'test_results{}/test_{}_{}_{}.json'.format(debug_text, fold, dataset, model)
def build_params_string(jobnum, varied_params, dataset, model, train_slices, test_slices, fold):
params = dict(num_records = 20,
jobnum = jobnum,
model = dict(net_type = model,
fp_length = int(round(varied_params['fp_length'])),
fp_depth = int(round(varied_params['fp_depth'])),
conv_width = int(round(varied_params['conv_width'])),
h1_size = int(round(varied_params['h1_size'])),
L2_reg = np.exp(varied_params['log_L2_reg'])),
train = dict(num_iters = num_iters,
batch_size = 100,
init_scale = np.exp(varied_params['log_init_scale']),
step_size = np.exp(varied_params['log_step_size']),
seed = fold + 1),
task = dict(name = dataset,
train_slices = train_slices,
test_slices = test_slices),
varied_params = varied_params)
return json.dumps(params)
def runjob_debug(params_string, outfile):
print params_string
print outfile
def runjob_local(params_string, outfile):
with open(outfile, 'w') as f:
p = Popen(experiment_command, stdin=PIPE, stdout=f, shell=True)
p.stdin.write(params_string)
p.stdin.close()
return p
def runjob_sbatch(params_string, outfile):
infile_obj = NamedTemporaryFile(dir='./tmp', delete=False)
with infile_obj as f:
f.write(params_string)
infile = infile_obj.name
logfile = outfile + '_log.txt'
bash_script = '#!/bin/bash\n{} >{} <{}'.format(experiment_command, outfile, infile)
p = Popen(make_sbatch_args(logfile), stdin=subprocess.PIPE)
p.communicate(bash_script)
if p.returncode != 0:
raise RuntimeError
def prepare_job_sbatch(params_string, outfile):
dir_name, file_name = outfile.split('/')
raw_file_name, extension = file_name.split('.')
params_dir = 'tmp'
params_file_name = '.'.join([raw_file_name, 'params'])
params_file = '/'.join([params_dir, params_file_name])
# with open(params_file, 'w') as pf:
# pf.write(params_string)
logfile = outfile + '_log.txt'
return ' '.join([params_file, outfile, logfile]) + '\n'
def generate_slice_lists(num_folds, N_data):
chunk_boundaries = map(int, np.linspace(0, N_data, num_folds + 1))
chunk_slices = zip(chunk_boundaries[0:-1], chunk_boundaries[1:])
for f_ix in range(num_folds):
validation_chunk_ixs = [f_ix]
test_chunk_ixs = [(f_ix + 1) % num_folds]
train_chunk_ixs = [i for i in range(num_folds) if i not in validation_chunk_ixs + test_chunk_ixs]
yield (map(chunk_slices.__getitem__, train_chunk_ixs),
map(chunk_slices.__getitem__, validation_chunk_ixs),
map(chunk_slices.__getitem__, test_chunk_ixs))
def extract_best_results():
loss_file = 'all_losses.csv'
loss_data_frame = pd.DataFrame(dict(dataset=[], model=[], loss=[]))
for dataset, num_data in zip(datasets, dataset_sizes):
for model in model_bounds:
all_outfiles = [outputfile(jobnum, dataset, model, fold) for jobnum in range(N_jobs) for fold in range(num_folds)]
all_results = util.get_jobs_data(all_outfiles)
print "Loaded {} results for fold {} dataset {} model {}"\
.format(len(all_results), fold, dataset, model)
extract_test_loss = lambda job_data : job_data['test_loss']
not_nan = lambda job_data : not np.isnan(extract_test_loss(job_data))
ok_results = filter(not_nan, all_results)
best_run = sorted(ok_results, key=extract_test_loss)[0]
best_hypers = best_run['params']
best_loss = best_run['test_loss']
params_string = json.dumps(best_hypers)
params_file = '_'.join(['best_params', dataset, model])
with open(params_file, 'w') as pf:
pf.write(params_string)
loss_data_frame = loss_data_frame.append(
dict(dataset=dataset, model=model, loss=best_loss),
ignore_index=True
)
# Re-order columns
loss_data_frame = loss_data_frame[['dataset', 'model', 'loss']]
loss_data_frame.to_csv(loss_file, index=False)
if not test_mode:
print "Starting validation experiments..."
all_jobs = []
still_running = lambda job : job.poll() is None
lines = []
for dataset, num_data in zip(datasets, dataset_sizes):
for fold, (train_slices, validation_slices, test_slices)\
in enumerate(generate_slice_lists(num_folds, num_data)):
for model, bounds in model_bounds.iteritems():
for jobnum, varied_params in generate_params(bounds, N_jobs):
print "\nRunning fold {} job {} on dataset {} with model {} " \
"and params {}".format(fold, jobnum, dataset, model, varied_params)
cur_outfile = outputfile(jobnum, dataset, model, fold)
if os.path.exists(cur_outfile) and os.path.getsize(cur_outfile) > 0:
print "SKIPPING because it's already done."
continue
params_string = build_params_string(jobnum, varied_params, dataset, model,
train_slices, validation_slices, fold)
if DEBUG_JOB:
runjob_debug(params_string, cur_outfile)
elif LAUNCH_USING_SLURM:
lines.append(prepare_job_sbatch(params_string, cur_outfile))
else:
p = runjob_local(params_string, cur_outfile)
all_jobs.append(p) # Only have a few jobs running at a time.
while len(all_jobs) >= N_cores:
sleep(1)
all_jobs = filter(still_running, all_jobs)
if (lines!=[]):
with open('params_and_outs.txt', 'w') as params_and_outs:
params_and_outs.writelines(lines)
else:
pass
else:
print "Starting test experiments..."
all_jobs = []
still_running = lambda job : job.poll() is None
for dataset, num_data in zip(datasets, dataset_sizes):
for fold, (train_slices, validation_slices, test_slices)\
in enumerate(generate_slice_lists(num_folds, num_data)):
for model in model_bounds:
cur_outfile = outputfile_test(dataset, model, fold)
if os.path.exists(cur_outfile) and os.path.getsize(cur_outfile) > 0:
print "SKIPPING because it's already done."
continue
all_outfiles = [outputfile(jobnum, dataset, model, fold) for jobnum in range(N_jobs)]
all_results = util.get_jobs_data(all_outfiles)
print "Loaded {} results for fold {} dataset {} model {}"\
.format(len(all_results), fold, dataset, model)
best_hypers = sorted(all_results, key=extract_test_loss)[0]['params']
best_hypers['seed'] = 0
best_hypers['task']['train_slices'] = train_slices + validation_slices
best_hypers['task']['test_slices'] = test_slices
params_string = json.dumps(best_hypers)
if DEBUG_JOB:
runjob_debug(params_string, cur_outfile)
elif LAUNCH_USING_SLURM:
runjob_sbatch(params_string, cur_outfile)
else:
p = runjob_local(params_string, cur_outfile)
all_jobs.append(p) # Only have a few jobs running at a time.
while len(all_jobs) >= N_cores:
sleep(1)
all_jobs = filter(still_running, all_jobs)