You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
I found the class below made working with 2d numpy arrays that configure optimization choices much simpler for my use cases. You simply add the parameters you want to it, then when you get a callback with a configuration it will convert your settings back to the actual format you need automatically. Would you be interested in a pull request, and if so where should I put it?
importosimportsysimportcopyimportsiximportjsonimportGPyimportGPyOptimportnumpyasnpclassHyperparameterOptions(object):
def__init__(self, verbose=1):
self.index_dict= {}
self.search_space= []
self.verbose=verbosedefadd_param(self, name, domain, domain_type='discrete', enable=True, required=True, default=None):
""" # Arguments search_space: list of hyperparameter configurations required by BayseanOptimizer index_dict: dictionary that will be used to lookup real values and types when we get the hyperopt callback with ints or floats enable: this parameter will be part of hyperparameter search required: this parameter must be passed to the model default: default value if required """ifself.search_spaceisNone:
self.search_space= []
ifself.index_dictisNone:
self.index_dict= {'current_index': 0}
if'current_index'notinself.index_dict:
self.index_dict['current_index'] =0ifenable:
param_index=self.index_dict['current_index']
numerical_domain=domainneeds_reverse_lookup=Falselookup_as=float# convert string domains to a domain of integer indexesifdomain_type=='discrete':
ifisinstance(domain, list) andisinstance(domain[0], str):
numerical_domain= [iforiinrange(len(domain))]
lookup_as=strneeds_reverse_lookup=Trueelifisinstance(domain, list) andisinstance(domain[0], bool):
numerical_domain= [iforiinrange(len(domain))]
lookup_as=boolneeds_reverse_lookup=Trueelifisinstance(domain, list) andisinstance(domain[0], float):
lookup_as=floatelse:
lookup_as=intopt_dict= {
'name': name,
'type': domain_type,
'domain': numerical_domain}
ifenable:
self.search_space+= [opt_dict]
# create a second version for us to construct the real function callopt_dict=copy.deepcopy(opt_dict)
opt_dict['lookup_as'] =lookup_aselse:
opt_dict['lookup_as'] =Noneopt_dict['enable'] =enableopt_dict['required'] =requiredopt_dict['default'] =defaultopt_dict['index'] =param_indexopt_dict['domain'] =domainopt_dict['needs_reverse_lookup'] =needs_reverse_lookupself.index_dict[name] =opt_dictself.index_dict['current_index'] +=1defparams_to_args(self, x):
""" Convert GPyOpt Bayesian Optimizer params back into function call arguments Arguments: x: the callback parameter of the GPyOpt Bayesian Optimizer index_dict: a dictionary with all the information necessary to convert back to function call arguments """iflen(x.shape) ==1:
# if we get a 1d array convert it to 2d so we are consistentx=np.expand_dims(x, axis=0)
# x is a funky 2d numpy array, so we convert it back to normal parameterskwargs= {}
forkey, opt_dictinsix.iteritems(self.index_dict):
ifkey=='current_index':
continueifopt_dict['enable']:
arg_name=opt_dict['name']
optimizer_param_column=opt_dict['index']
ifoptimizer_param_column>x.shape[-1]:
raiseValueError('Attempting to access optimizer_param_column'+str(optimizer_param_column) +' outside parameter bounds'+str(x.shape) +' of optimizer array with index dict: '+str(self.index_dict) +'and array x: '+str(x))
param_value=x[:, optimizer_param_column]
ifopt_dict['type'] =='discrete':
# the value is an integer indexing into the lookup dictifopt_dict['needs_reverse_lookup']:
domain_index=int(param_value)
domain_value=opt_dict['domain'][domain_index]
value=opt_dict['lookup_as'](domain_value)
else:
value=opt_dict['lookup_as'](param_value)
else:
# the value is a param to use directlyvalue=opt_dict['lookup_as'](param_value)
kwargs[arg_name] =valueelifopt_dict['required']:
kwargs[opt_dict['name']] =opt_dict['default']
returnkwargsdefget_domain(self):
""" Get the hyperparameter search space in the gpyopt domain format. """returnself.search_space
The text was updated successfully, but these errors were encountered:
ahundt
changed the title
Hyperparameter Options class to make life easier in some use cases.
HyperparameterOptions class to make life easier in some use cases.
Mar 29, 2018
I found the class below made working with 2d numpy arrays that configure optimization choices much simpler for my use cases. You simply add the parameters you want to it, then when you get a callback with a configuration it will convert your settings back to the actual format you need automatically. Would you be interested in a pull request, and if so where should I put it?
The text was updated successfully, but these errors were encountered: