def get_cs_bc(): cs_bc = ConfigurationSpace() x0 = UniformFloatHyperparameter("x0", scale1[0], scale1[1]) # x0 = UniformIntegerHyperparameter("x0", scale1[0], scale1[1]) # test int x1 = UniformFloatHyperparameter("x1", scale2[0], scale2[1]) cs_bc.add_hyperparameters([x0, x1]) return cs_bc
def impute_default_values( configuration_space: ConfigurationSpace, configs_array: np.ndarray ) -> np.ndarray: """Impute inactive hyperparameters in configuration array with their default. Necessary to apply an EPM to the data. Parameters ---------- configuration_space : ConfigurationSpace configs_array : np.ndarray Array of configurations. Returns ------- np.ndarray Array with configuration hyperparameters. Inactive values are imputed with their default value. """ for hp in configuration_space.get_hyperparameters(): default = hp.normalized_default_value idx = configuration_space.get_idx_by_hyperparameter_name(hp.name) nonfinite_mask = ~np.isfinite(configs_array[:, idx]) configs_array[nonfinite_mask, idx] = default return configs_array
def __init__(self, dim=2, bounds=None, noise_std=0, random_state=None): self.dim = dim params = {f'x{i}': (0, 10, 5) for i in range(1, 1+self.dim)} config_space = ConfigurationSpace() config_space.add_hyperparameters([UniformFloatHyperparameter(e, *params[e]) for e in params]) super().__init__(config_space, noise_std, optimal_value=0, 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'] elif 'log' in param_dict: optional_args['log'] = parse_bool(param_dict['log']) elif '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) else: raise ValueError("Parameter type %s not supported!" % param_type) cs.add_hyperparameter(param) return cs
def __init__(self, noise_std=0, random_state=None): params = {'x1': (-10, 0, -5), 'x2': (-6.5, 0, -3.25)} config_space = ConfigurationSpace() config_space.add_hyperparameters([UniformFloatHyperparameter(e, *params[e]) for e in params]) super().__init__(config_space, noise_std, optimal_value=-106.7645367, optimal_point=[(-3.1302468, -1.5821422)], random_state=random_state)
def __init__(self, noise_std=0, random_state=None): params = {f'x{i}': (-1.25, 1.25, 1) for i in [1, 2]} config_space = ConfigurationSpace() config_space.add_hyperparameters([UniformFloatHyperparameter(e, *params[e]) for e in params]) super().__init__(config_space, noise_std, optimal_value=-0.072, optimal_point=[(0.84852813, -0.84852813), (-0.84852813, 0.84852813)], random_state=random_state)
def __init__(self, noise_std=0, random_state=None): lb, ub = -4.5, 4.5 dim = 2 params = {f'x{i}': (lb, ub, (lb + ub)/2) for i in range(1, dim+1)} config_space = ConfigurationSpace() config_space.add_hyperparameters([UniformFloatHyperparameter(e, *params[e]) for e in params]) super().__init__(config_space, noise_std, optimal_value=0, random_state=random_state)
def __init__(self, noise_std=0, random_state=None): params = {'x1': (-5, 10, 2.5), 'x2': (0, 15, 7.5)} config_space = ConfigurationSpace() config_space.add_hyperparameters([UniformFloatHyperparameter(e, *params[e]) for e in params]) super().__init__(config_space, noise_std, optimal_value=0.397887, optimal_point=[(-np.pi, 12.275), (np.pi, 2.275), (9.42478, 2.475)], random_state=random_state)
def __init__(self, noise_std=0, random_state=None): params = {'x1': (-15.0, -5.0, -10.0), 'x2': (-3.0, 3.0, 0)} config_space = ConfigurationSpace() config_space.add_hyperparameters([UniformFloatHyperparameter(e, *params[e]) for e in params]) super().__init__(config_space, noise_std, optimal_value=0, optimal_point=[(-10.0, 1.0)], random_state=random_state)
def __init__(self, noise_std=0, random_state=None): self.ref_point = [1864.72022, 11.81993945, 0.2903999384] params = {f'x{i}': (1.0, 3.0) for i in range(1, 6)} config_space = ConfigurationSpace() config_space.add_hyperparameters([UniformFloatHyperparameter(e, *params[e]) for e in params]) super().__init__(config_space, noise_std, num_objs=3, random_state=random_state)
def __init__(self, dim: int, num_constraints=0, noise_std=0, random_state=None): self.dim = dim self.ref_point = [11.0, 11.0] params = {f'x{i}': (0, 1) for i in range(1, dim+1)} config_space = ConfigurationSpace() config_space.add_hyperparameters([UniformFloatHyperparameter(e, *params[e]) for e in params]) super().__init__(config_space, noise_std, num_objs=2, num_constraints=num_constraints, random_state=random_state)
def __init__(self, noise_std=0, random_state=None): self.ref_point = [10.0, 10.0] params = {'x1': (0.1, 10.0), 'x2': (0.0, 5.0)} config_space = ConfigurationSpace() config_space.add_hyperparameters([UniformFloatHyperparameter(e, *params[e]) for e in params]) super().__init__(config_space, noise_std, num_objs=2, num_constraints=2, random_state=random_state)
def __init__(self, dim=2, constrained=False, noise_std=0, random_state=None): self.dim = dim self.constrained = constrained params = {f'x{i}': (-5.0, 10.0, 2.5) for i in range(1, 1+self.dim)} config_space = ConfigurationSpace() config_space.add_hyperparameters([UniformFloatHyperparameter(e, *params[e]) for e in params]) super().__init__(config_space, noise_std, optimal_value=0, optimal_point=[tuple(1.0 for _ in range(self.dim))], random_state=random_state)
def __init__(self, constrained=False, noise_std=0, random_state=None): self.ref_point = [18.0, 6.0] self.constrained = constrained num_constraints = 1 if self.constrained else 0 params = {'x1': (0, 1, 0.5), 'x2': (0, 1, 0.5)} config_space = ConfigurationSpace() config_space.add_hyperparameters([UniformFloatHyperparameter(e, *params[e]) for e in params]) super().__init__(config_space, noise_std, num_objs=2, num_constraints=num_constraints, random_state=random_state)
def __init__(self, dim, num_objs=2, num_constraints=0, noise_std=0, random_state=None): if dim <= num_objs: raise ValueError( "dim must be > num_objs, but got %s and %s" % (dim, num_objs) ) self.dim = dim self.k = self.dim - num_objs + 1 self.bounds = [(0.0, 1.0) for _ in range(self.dim)] self.ref_point = [self._ref_val for _ in range(num_objs)] params = {f'x{i}': (0, 1, i/dim) for i in range(1, dim+1)} config_space = ConfigurationSpace() config_space.add_hyperparameters([UniformFloatHyperparameter(e, *params[e]) for e in params]) super().__init__(config_space, noise_std, num_objs, num_constraints, random_state=random_state)
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 __init__(self, dim=2, bounds=None, constrained=False, noise_std=0, random_state=None): self.constrained = constrained if bounds is None: if constrained: lb, ub = -5, 10 else: lb, ub = -10, 15 else: lb, ub = bounds params = {f'x{i}': (lb, ub, (lb + ub)/2) for i in range(1, dim+1)} config_space = ConfigurationSpace() config_space.add_hyperparameters([UniformFloatHyperparameter(e, *params[e]) for e in params]) super().__init__(config_space, noise_std, optimal_value=0, random_state=random_state)
def config_space2string(config_space: ConfigurationSpace): pattern = r'[,|{}\'=<>&]' for hp in config_space.get_hyperparameters(): if re.search(pattern, hp.name): raise NameError('Invalid character in hyperparameter name!') if hasattr(hp, 'choices'): for value in hp.choices: if re.search(pattern, value): raise NameError('Invalid character in categorical hyperparameter value!') return str(config_space)
def get_cs_lightgbm(): # todo q and int for compare? cs = ConfigurationSpace() n_estimators = UniformFloatHyperparameter("n_estimators", 100, 1000, default_value=500, q=50) num_leaves = UniformIntegerHyperparameter("num_leaves", 31, 2047, default_value=128) # max_depth = Constant('max_depth', 15) learning_rate = UniformFloatHyperparameter("learning_rate", 1e-3, 0.3, default_value=0.1, log=True) min_child_samples = UniformIntegerHyperparameter("min_child_samples", 5, 30, default_value=20) subsample = UniformFloatHyperparameter("subsample", 0.7, 1, default_value=1, q=0.1) colsample_bytree = UniformFloatHyperparameter("colsample_bytree", 0.7, 1, default_value=1, q=0.1) # cs.add_hyperparameters([n_estimators, num_leaves, max_depth, learning_rate, min_child_samples, subsample, # colsample_bytree]) cs.add_hyperparameters([ n_estimators, num_leaves, learning_rate, min_child_samples, subsample, colsample_bytree ]) return cs
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)
import numpy as np from litebo.optimizer.generic_smbo import SMBO from litebo.utils.config_space import ConfigurationSpace, UniformFloatHyperparameter def townsend(config): X = np.array(list(config.get_dictionary().values())) res = dict() res['objs'] = (-(np.cos( (X[0] - 0.1) * X[1])**2 + X[0] * np.sin(3 * X[0] + X[1])), ) res['constraints'] = (-(-np.cos(1.5 * X[0] + np.pi) * np.cos(1.5 * X[1]) + np.sin(1.5 * X[0] + np.pi) * np.sin(1.5 * X[1])), ) return res townsend_params = {'float': {'x1': (-2.25, 2.5, 0), 'x2': (-2.5, 1.75, 0)}} townsend_cs = ConfigurationSpace() townsend_cs.add_hyperparameters([ UniformFloatHyperparameter(e, *townsend_params['float'][e]) for e in townsend_params['float'] ]) bo = SMBO(townsend, townsend_cs, num_constraints=1, acq_optimizer_type='random_scipy', max_runs=60, task_id='smbo_eic') bo.run()
# We translate boolean values: cfg["shrinking"] = True if cfg["shrinking"] == "true" else False # And for gamma, we set it to a fixed value or to "auto" (if used) if "gamma" in cfg: cfg["gamma"] = cfg["gamma_value"] if cfg["gamma"] == "value" else "auto" cfg.pop("gamma_value", None) # Remove "gamma_value" 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(
px2 = 15*x2 res = dict() f1 = (px2 - 5.1/(4*np.pi**2) * px1**2 + 5/np.pi * px1 - 6)**2 + 10 * (1 - 1/(8*np.pi)) * np.cos(px1) + 10 f2 = (1 - np.exp(-1/(2*x2))) * (2300 * x1**3 + 1900 * x1**2 + 2092 * x1 + 60) / (100 * x1**3 + 500 * x1**2 + 4*x1 + 20) res['objs'] = [f1, f2] res['constraints'] = [] return res bc_params = { 'float': { 'x1': (0, 1, 0.5), 'x2': (0, 1, 0.5) } } bc_cs = ConfigurationSpace() bc_cs.add_hyperparameters([UniformFloatHyperparameter(e, *bc_params['float'][e]) for e in bc_params['float']]) bc_max_hv = 59.36011874867746 bc_ref_point = [18., 6.] bo = SMBO(branin_currin, bc_cs, task_id='parego', num_objs=2, acq_type='parego', ref_point=bc_ref_point, max_runs=100) bo.run() hvs = bo.get_history().hv_data log_hv_diff = np.log10(bc_max_hv - np.asarray(hvs))
def branin(x): xs = x.get_dictionary() x1 = xs['x1'] x2 = xs['x2'] a = 1. b = 5.1 / (4. * np.pi**2) c = 5. / np.pi r = 6. s = 10. t = 1. / (8. * np.pi) ret = a * (x2 - b * x1**2 + c * x1 - r)**2 + s * (1 - t) * np.cos(x1) + s return {'objs': (ret, )} cs = ConfigurationSpace() x1 = UniformFloatHyperparameter("x1", -5, 10, default_value=0) x2 = UniformFloatHyperparameter("x2", 0, 15, default_value=0) cs.add_hyperparameters([x1, x2]) seed = np.random.randint(100) # random search # bo = SMBO(branin, cs, sample_strategy='random', max_runs=100, task_id='mcei', random_state=seed) bo = SMBO(branin, cs, advisor_type='mcadvisor', acq_type='mcei', mc_times=10, max_runs=50,
def get_configspace(): cs = ConfigurationSpace() x1 = UniformFloatHyperparameter("x1", -5, 10, default_value=0) x2 = UniformFloatHyperparameter("x2", 0, 15, default_value=0) cs.add_hyperparameters([x1, x2]) return cs