Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _get_cs(self, n_dimensions):
configspace = smac.configspace.ConfigurationSpace()
for i in range(n_dimensions):
configspace.add_hyperparameter(smac.configspace.UniformFloatHyperparameter('x%d' % i, 0, 1))
return configspace
def get_rf(n_dimensions, rs):
bounds = [(0., 1.) for _ in range(n_dimensions)]
types = np.zeros(n_dimensions)
configspace = smac.configspace.ConfigurationSpace()
for i in range(n_dimensions):
configspace.add_hyperparameter(smac.configspace.UniformFloatHyperparameter('x%d' % i, 0, 1))
model = RandomForestWithInstancesHPO(
configspace=configspace, types=types, bounds=bounds, log_y=False, bootstrap=False, n_iters=5, n_splits=5, seed=1,
)
return model
noise_level=noise,
noise_level_bounds=(1e-10, 2),
prior=HorseshoePrior(scale=0.1, rng=rs),
)
kernel = cov_amp * exp_kernel + noise_kernel
n_mcmc_walkers = 3 * len(kernel.theta)
if n_mcmc_walkers % 2 == 1:
n_mcmc_walkers += 1
bounds = [(0., 1.) for _ in range(n_dimensions)]
types = np.zeros(n_dimensions)
configspace = ConfigurationSpace()
for i in range(n_dimensions):
configspace.add_hyperparameter(UniformFloatHyperparameter('x%d' % i, 0, 1))
model = GaussianProcessMCMC(
configspace=configspace,
types=types,
bounds=bounds,
kernel=kernel,
n_mcmc_walkers=n_mcmc_walkers,
chain_length=n_iter,
burnin_steps=n_iter,
normalize_y=normalize_y,
seed=rs.randint(low=1, high=10000),
mcmc_sampler='emcee',
average_samples=average_samples,
)
return model
def test_impute_inactive_hyperparameters(self):
cs = ConfigurationSpace()
a = cs.add_hyperparameter(CategoricalHyperparameter('a', [0, 1]))
b = cs.add_hyperparameter(CategoricalHyperparameter('b', [0, 1]))
c = cs.add_hyperparameter(UniformFloatHyperparameter('c', 0, 1))
cs.add_condition(EqualsCondition(b, a, 1))
cs.add_condition(EqualsCondition(c, a, 0))
cs.seed(1)
configs = cs.sample_configuration(size=100)
config_array = convert_configurations_to_array(configs)
for line in config_array:
if line[0] == 0:
self.assertTrue(np.isnan(line[1]))
elif line[0] == 1:
self.assertTrue(np.isnan(line[2]))
gp = get_gp(3, np.random.RandomState(1))
config_array = gp._impute_inactive(config_array)
for line in config_array:
if line[0] == 0:
def _convert_PHOTON_to_smac_space(self, hyperparam: object, name: str):
if not hyperparam:
return None
self.hyperparameter_list.append(name)
if isinstance(hyperparam, PhotonCategorical):
return CategoricalHyperparameter(choices=hyperparam.values, name=name)
elif isinstance(hyperparam, list):
return CategoricalHyperparameter(choices=hyperparam, name=name)
elif isinstance(hyperparam, FloatRange):
return UniformFloatHyperparameter(lower=hyperparam.start, upper=hyperparam.stop, name=name)
elif isinstance(hyperparam, IntegerRange):
return UniformIntegerHyperparameter(lower=hyperparam.start, upper=hyperparam.stop, name=name)
elif isinstance(hyperparam, BooleanSwitch):
return CategoricalHyperparameter(choices=["true", "false"], name=name)
ratio_features = CategoricalHyperparameter("ratio_features", choices=(3 / 6, 4 / 6, 5 / 6, 1), default_value=1)
min_split = UniformIntegerHyperparameter("min_samples_to_split", lower=1, upper=10, default_value=2)
min_leaves = UniformIntegerHyperparameter("min_samples_in_leaf", lower=1, upper=10, default_value=1)
cs.add_hyperparameters([model, num_trees, bootstrap, ratio_features, min_split, min_leaves])
inc_num_trees = InCondition(num_trees, model, ["RF"])
inc_bootstrap = InCondition(bootstrap, model, ["RF"])
inc_ratio_features = InCondition(ratio_features, model, ["RF"])
inc_min_split = InCondition(min_split, model, ["RF"])
inc_min_leavs = InCondition(min_leaves, model, ["RF"])
cs.add_conditions([inc_num_trees, inc_bootstrap, inc_ratio_features, inc_min_split, inc_min_leavs])
acq = CategoricalHyperparameter("acq_func", choices=("EI", "LCB", "PI", "LogEI"))
par_ei = UniformFloatHyperparameter("par_ei", lower=-10, upper=10)
par_pi = UniformFloatHyperparameter("par_pi", lower=-10, upper=10)
par_logei = UniformFloatHyperparameter("par_logei", lower=0.001, upper=100, log=True)
par_lcb = UniformFloatHyperparameter("par_lcb", lower=0.0001, upper=0.9999)
cs.add_hyperparameters([acq, par_ei, par_pi, par_logei, par_lcb])
inc_par_ei = InCondition(par_ei, acq, ["EI"])
inc_par_pi = InCondition(par_pi, acq, ["PI"])
inc_par_logei = InCondition(par_logei, acq, ["LogEI"])
inc_par_lcb = InCondition(par_lcb, acq, ["LCB"])
cs.add_conditions([inc_par_ei, inc_par_pi, inc_par_logei, inc_par_lcb])
return cs