Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Update hyperopt_tuner.py #17

Merged
merged 3 commits into from
Sep 10, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions src/sdk/pynni/nni/hyperopt_tuner/hyperopt_tuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ def json2space(in_x, name=ROOT):
else:
out_y = dict()
for key in in_x.keys():
out_y[key] = json2space(in_x[key], name+"[%s]" % str(key))
out_y[key] = json2space(in_x[key], name+'[%s]' % str(key))
elif isinstance(in_x, list):
out_y = list()
for i, x_i in enumerate(in_x):
out_y.append(json2space(x_i, name+"[%d]" % i))
out_y.append(json2space(x_i, name+'[%d]' % i))
else:
logger.info('in_x is not a dict or a list in json2space fuinction %s', str(in_x))
return out_y
Expand All @@ -88,19 +88,19 @@ def json2paramater(in_x, paramater, name=ROOT):
_index = paramater[name]
out_y = {
INDEX: _index,
VALUE: json2paramater(in_x[VALUE][_index], paramater, name=name+"[%d]" % _index)
VALUE: json2paramater(in_x[VALUE][_index], paramater, name=name+'[%d]' % _index)
}
else:
out_y = paramater[name]
else:
out_y = dict()
for key in in_x.keys():
out_y[key] = json2paramater(
in_x[key], paramater, name + "[%s]" % str(key))
in_x[key], paramater, name + '[%s]' % str(key))
elif isinstance(in_x, list):
out_y = list()
for i, x_i in enumerate(in_x):
out_y.append(json2paramater(x_i, paramater, name + "[%d]" % i))
out_y.append(json2paramater(x_i, paramater, name + '[%d]' % i))
else:
logger.info('in_x is not a dict or a list in json2space fuinction %s', str(in_x))
return out_y
Expand All @@ -121,13 +121,13 @@ def json2vals(in_x, vals, out_y, name=ROOT):
if _type == 'choice':
_index = vals[INDEX]
json2vals(in_x[VALUE][_index], vals[VALUE],
out_y, name=name + "[%d]" % _index)
out_y, name=name + '[%d]' % _index)
else:
for key in in_x.keys():
json2vals(in_x[key], vals[key], out_y, name + "[%s]" % str(key))
json2vals(in_x[key], vals[key], out_y, name + '[%s]' % str(key))
elif isinstance(in_x, list):
for i, temp in enumerate(in_x):
json2vals(i, vals[temp], out_y, name + "[%d]" % temp)
json2vals(i, vals[temp], out_y, name + '[%d]' % temp)


def _split_index(params):
Expand All @@ -145,6 +145,7 @@ class HyperoptTuner(Tuner):
'''
HyperoptTuner is a tuner which using hyperopt algorithm.
'''

def __init__(self, algorithm_name, optimize_mode):
self.algorithm_name = algorithm_name
self.optimize_mode = OptimizeMode(optimize_mode)
Expand Down Expand Up @@ -179,10 +180,10 @@ def update_search_space(self, search_space):
self.rval.catch_eval_exceptions = False

def generate_parameters(self, parameter_id):
"""
'''
Returns a set of trial (hyper-)parameters, as a serializable object.
parameter_id : int
"""
'''
rval = self.rval
trials = rval.trials
algorithm = rval.algo
Expand All @@ -199,7 +200,7 @@ def generate_parameters(self, parameter_id):
except Exception:
parameter[key] = None

# remove "_index" from json2parameter and save params-id
# remove '_index' from json2parameter and save params-id
total_params = json2paramater(self.json, parameter)
self.total_data[parameter_id] = total_params
params = _split_index(total_params)
Expand All @@ -212,7 +213,7 @@ def receive_trial_result(self, parameter_id, parameters, reward):
parameters : dict of parameters
reward : reward of one trial
'''
# restore the paramsters contains "_index"
# restore the paramsters contains '_index'
if parameter_id not in self.total_data:
raise RuntimeError('Received parameter_id not in total_data.')
params = self.total_data[parameter_id]
Expand Down Expand Up @@ -258,13 +259,13 @@ def receive_trial_result(self, parameter_id, parameters, reward):
def miscs_update_idxs_vals(self, miscs, idxs, vals,
assert_all_vals_used=True,
idxs_map=None):
"""
'''
Unpack the idxs-vals format into the list of dictionaries that is
`misc`.

idxs_map: a dictionary of id->id mappings so that the misc['idxs'] can
contain different numbers than the idxs argument. XXX CLARIFY
"""
'''
if idxs_map is None:
idxs_map = {}

Expand Down