def string2config_space(space_desc: str): line_list = space_desc.split('\n') cur_line = 2 cs = ConfigurationSpace() status = 'hp' hp_list = list() while cur_line != len(line_list) - 1: line_content = line_list[cur_line] if line_content == ' Conditions:': hp_dict = {hp.name: hp for hp in hp_list} status = 'cond' elif line_content == ' Forbidden Clauses:': status = 'bid' else: if status == 'hp': hp = string2hyperparameter(line_content) hp_list.append(hp) cs.add_hyperparameter(hp) elif status == 'cond': cond = string2condition(line_content, hp_dict) cs.add_condition(cond) else: forbid = string2forbidden(line_content, hp_dict) cs.add_forbidden_clause(forbid) cur_line += 1 return cs
def get_configspace(self): hp_num = self._hp_cnt + self._delta if hp_num > self.hp_size: hp_num = self.hp_size hps = self.config_space.get_hyperparameters() cs = ConfigurationSpace() for _id in range(hp_num): _hp_id = self.importance_list[_id] for _hp in hps: if _hp.name == _hp_id: cs.add_hyperparameter(_hp) history_list = list() if len(self.history_dict.keys()) > 0 and self._hp_cnt < self.hp_size: for _config in self.history_dict.keys(): # Impute the default value for new hyperparameter. _config_dict = _config.get_dictionary().copy() for _idx in range(self._hp_cnt, hp_num): new_hp = self.importance_list[_idx] # print('hp_num=', self._hp_cnt, 'new hp is', new_hp) _config_dict[new_hp] = self.defaults[new_hp] history_list.append((_config_dict, self.history_dict[_config])) if len(history_list) == 0: history_list = [(_config, self.history_dict[_config]) for _config in self.history_dict.keys()] return cs, history_list
def __init__(self, bounds=None, noise_std=0, random_state=None): if bounds is None: bounds = [0, 20] lb, ub = bounds config_space = ConfigurationSpace() config_space.add_hyperparameter(UniformFloatHyperparameter('x1', lb, ub, 1)) config_space.add_hyperparameter(UniformIntegerHyperparameter('x2', lb, ub, 1)) super().__init__(config_space, noise_std, optimal_value=-31.9998, optimal_point=[(5.333, 4)], random_state=random_state)
def get_config_space_from_dict(space_dict: dict): cs = ConfigurationSpace() params_dict = space_dict['parameters'] for key in params_dict: param_dict = params_dict[key] param_type = param_dict['type'] if param_type in ['float', 'int']: bound = param_dict['bound'] optional_args = dict() if 'default' in param_dict: optional_args['default_value'] = param_dict['default'] if 'log' in param_dict: optional_args['log'] = parse_bool(param_dict['log']) if 'q' in param_dict: optional_args['q'] = param_dict['q'] if param_type == 'float': param = UniformFloatHyperparameter(key, bound[0], bound[1], **optional_args) else: param = UniformIntegerHyperparameter(key, bound[0], bound[1], **optional_args) elif param_type == 'cat': choices = param_dict['choice'] optional_args = dict() if 'default' in param_dict: optional_args['default_value'] = param_dict['default'] param = CategoricalHyperparameter(key, choices, **optional_args) elif param_type == 'const': value = param_dict['value'] param = Constant(key, value) else: raise ValueError("Parameter type %s not supported!" % param_type) cs.add_hyperparameter(param) return cs
clf = svm.SVC(**cfg, random_state=42) scores = cross_val_score(clf, iris.data, iris.target, cv=5) return 1 - np.mean(scores) # Minimize! logging.basicConfig(level=logging.INFO) # Build Configuration Space which defines all parameters and their ranges cs = ConfigurationSpace() # We define a few possible types of SVM-kernels and add them as "kernel" to our cs kernel = CategoricalHyperparameter("kernel", ["linear", "rbf", "poly", "sigmoid"], default_value="poly") cs.add_hyperparameter(kernel) # There are some hyperparameters shared by all kernels C = UniformFloatHyperparameter("C", 0.001, 1000.0, default_value=1.0) shrinking = CategoricalHyperparameter("shrinking", ["true", "false"], default_value="true") cs.add_hyperparameters([C, shrinking]) # Others are kernel-specific, so we can add conditions to limit the searchspace degree = UniformIntegerHyperparameter( "degree", 1, 5, default_value=3) # Only used by kernel poly coef0 = UniformFloatHyperparameter("coef0", 0.0, 10.0, default_value=0.0) # poly, sigmoid cs.add_hyperparameters([degree, coef0]) use_degree = InCondition(child=degree, parent=kernel, values=["poly"]) use_coef0 = InCondition(child=coef0, parent=kernel, values=["poly", "sigmoid"])