forked from HIPS/Spearmint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mymain.py
212 lines (163 loc) · 6.6 KB
/
mymain.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
import sys
import optparse
import importlib
import time
import os
import numpy as np
try: import simplejson as json
except ImportError: import json
from collections import OrderedDict
from spearmint.tasks.task_group import TaskGroup
from spearmint.resources.resource import parse_resources_from_config
from spearmint.resources.resource import print_resources_status
from spearmint.utils.parsing import parse_db_address
def get_options():
parser = optparse.OptionParser(usage="usage: %prog [options] directory")
parser.add_option("--config", dest="config_file",
help="Configuration file name.",
type="string", default="config.json")
(commandline_kwargs, args) = parser.parse_args()
# Read in the config file
expt_dir = os.path.realpath(os.path.expanduser(args[0]))
if not os.path.isdir(expt_dir):
raise Exception("Cannot find directory %s" % expt_dir)
expt_file = os.path.join(expt_dir, commandline_kwargs.config_file)
try:
with open(expt_file, 'r') as f:
options = json.load(f, object_pairs_hook=OrderedDict)
except:
raise Exception("config.json did not load properly. Perhaps a spurious comma?")
options["config"] = commandline_kwargs.config_file
options["expt_dir"] = expt_dir
# Set sensible defaults for options
options['chooser'] = options.get('chooser', 'default_chooser')
if 'tasks' not in options:
options['tasks'] = {'main' : {'type' : 'OBJECTIVE', 'likelihood' : options.get('likelihood', 'GAUSSIAN')}}
if not os.path.exists(expt_dir):
sys.stderr.write("Cannot find experiment directory '%s'. "
"Aborting.\n" % (expt_dir))
sys.exit(-1)
return options, expt_dir
def main():
options, expt_dir = get_options()
resources = parse_resources_from_config(options)
# Load up the chooser.
chooser_module = importlib.import_module('spearmint.choosers.' + options['chooser'])
chooser = chooser_module.init(options)
experiment_name = options.get("experiment-name", 'unnamed-experiment')
# Get a suggestion for the next job
task_names = ['main']
resource_name = ''
suggested_input = get_suggestion(chooser, task_names, options)
print 'suggested_input:'
print suggested_input
print
# TODO: support decoupling i.e. task_names containing more than one task,
# and the chooser must choose between them in addition to choosing X
def get_suggestion(chooser, task_names, options):
if len(task_names) == 0:
raise Exception("Error: trying to obtain suggestion for 0 tasks ")
experiment_name = options['experiment-name']
# We are only interested in the tasks in task_names
task_options = { task: options["tasks"][task] for task in task_names }
# For now we aren't doing any multi-task, so the below is simpler
# task_options = options["tasks"]
task_options = {'main': options['tasks']['main']}
task_group = TaskGroup(task_options, options['variables'])
# Load the tasks from the database -- only those in task_names!
task_group = load_task_group(task_group, options)
# Load the model hypers from the database.
hypers = load_hypers(options)
# "Fit" the chooser - give the chooser data and let it fit the model.
hypers = chooser.fit(task_group, hypers, task_options)
# Save the hyperparameters to the database.
save_hypers(options, hypers)
# Ask the chooser to actually pick one.
suggested_input = chooser.suggest()
try:
suggested_input_len = len(suggested_input)
except:
suggested_input = [suggested_input]
suggested_input = np.array(suggested_input)
jobs = load_jobs(task_group, options)
job_id = len(jobs) + 1
job = {
'params' : task_group.paramify(suggested_input),
'status' : 'pending'
}
jobs.append(job)
save_jobs(task_group, options, jobs)
return suggested_input
def save_hypers(options, hypers):
hyper_file = os.path.join(options['expt_dir'], 'hypers.json')
if hypers:
fd = open(hyper_file, 'w')
hypers['main']['hypers']['beta_alpha'] = hypers['main']['hypers']['beta_alpha'].tolist()
hypers['main']['hypers']['ls'] = hypers['main']['hypers']['ls'].tolist()
hypers['main']['hypers']['beta_beta'] = hypers['main']['hypers']['beta_beta'].tolist()
json.dump(hypers, fd)
def load_hypers(options):
hyper_file = os.path.join(options['expt_dir'], 'hypers.json')
try:
fd = open(hyper_file, 'r')
hypers = json.load(fd)
hypers['main']['hypers']['beta_alpha'] = np.array(hypers['main']['hypers']['beta_alpha'])
hypers['main']['hypers']['ls'] = np.array(hypers['main']['hypers']['ls'])
hypers['main']['hypers']['beta_beta'] = np.array(hypers['main']['hypers']['beta_beta'])
return hypers
except:
return {}
def load_jobs(task_group, options):
results_file = os.path.join(options['expt_dir'], 'results.dat')
jobs = []
try:
fd = open(results_file, 'r')
except:
print 'No jobs!'
return []
for line in fd:
strs = line.split()
if len(strs) < 2:
break
inputs = []
for i in range(2, len(strs)):
inputs.append(float(strs[i]))
inputs = np.array(inputs)
job = {'params' : task_group.paramify(inputs)}
if strs[0] == 'P':
job['status'] = 'pending'
else:
job['status'] = 'complete'
job['value'] = float(strs[0])
jobs.append(job)
return jobs
def save_jobs(task_group, options, jobs):
results_file = os.path.join(options['expt_dir'], 'results.dat')
fd = open(results_file, 'w')
for job in jobs:
line = ''
if job['status'] == 'pending':
line = line + 'P P'
else:
line = line + str(job['value']) + ' 1'
inputs = task_group.vectorify(job['params'])
for input in inputs:
line = line + ' ' + str(input)
fd.write(line)
fd.write('\n')
def load_task_group(task_group, options):
jobs = load_jobs(task_group, options)
if jobs:
task_group.inputs = np.array([task_group.vectorify(job['params'])
for job in jobs if job['status'] == 'complete'])
task_group.pending = np.array([task_group.vectorify(job['params'])
for job in jobs if job['status'] == 'pending'])
task_names = ['main']
task_group.values = {task : np.array([job['value']
for job in jobs if job['status'] == 'complete'])
for task in task_names}
task_group.add_nan_task_if_nans()
# TODO: record costs
return task_group
if __name__ == '__main__':
main()