Ejemplo n.º 1
0
    def _convert_photon_to_smac_param(hyperparam: object, name: str):
        """
        Helper function: Convert PHOTON hyperparameter to SMAC hyperparameter.

        Parameters
        ----------
        * `hyperparam` [object]:
             One of photonai.optimization.hyperparameters.
        * `name` [str]
            Name of hyperparameter.
        """
        if not hyperparam:
            return None
        if isinstance(hyperparam, PhotonCategorical):
            return CategoricalHyperparameter(name, hyperparam.values)
        elif isinstance(hyperparam, list):
            return CategoricalHyperparameter(name, hyperparam)
        elif isinstance(hyperparam, FloatRange):
            if hyperparam.range_type == 'linspace':
                return UniformFloatHyperparameter(name, hyperparam.start, hyperparam.stop)
            elif hyperparam.range_type == 'logspace':
                raise NotImplementedError("Logspace in your float hyperparameter is not implemented in SMAC.")
            else:
                return UniformFloatHyperparameter(name, hyperparam.start, hyperparam.stop)
        elif isinstance(hyperparam, IntegerRange):
            return UniformIntegerHyperparameter(name, hyperparam.start, hyperparam.stop)
 def _create_configuration_space(self):
     config_space = ConfigurationSpace()
     min = self.parameter_domain.get_min_vector()
     max = self.parameter_domain.get_max_vector()
     for i in range(len(min)):
         param = None
         if min[i] == max[i]:
             param = UniformFloatHyperparameter(name=str(i),
                                                lower=min[i],
                                                upper=max[i] + 0.0001)
         else:
             param = UniformFloatHyperparameter(name=str(i),
                                                lower=min[i],
                                                upper=max[i])
         config_space.add_hyperparameter(param)
     return config_space
Ejemplo n.º 3
0
 def _get_acm_cs(self):
     """
         returns a configuration space 
         designed for querying ~smac.optimizer.smbo._component_builder
         
         Returns
         ------- 
             ConfigurationSpace
     """
     
     cs = ConfigurationSpace()
     cs.seed(self.rng.randint(0,2**20))
     
     model = CategoricalHyperparameter("model", choices=("RF", "GP"))
     
     num_trees = Constant("num_trees", value=10)
     bootstrap = CategoricalHyperparameter("do_bootstrapping", choices=(True, False), default_value=True)
     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
Ejemplo n.º 4
0
def get_mixed_gp(cat_dims, cont_dims, rs, noise=1e-3, normalize_y=True):
    from smac.epm.gp_kernels import ConstantKernel, Matern, WhiteKernel, HammingKernel

    cat_dims = np.array(cat_dims, dtype=np.int)
    cont_dims = np.array(cont_dims, dtype=np.int)
    n_dimensions = len(cat_dims) + len(cont_dims)
    cov_amp = ConstantKernel(
        2.0,
        constant_value_bounds=(1e-10, 2),
        prior=LognormalPrior(mean=0.0, sigma=1.0, rng=rs),
    )

    exp_kernel = Matern(
        np.ones([len(cont_dims)]),
        [(np.exp(-10), np.exp(2)) for _ in range(len(cont_dims))],
        nu=2.5,
        operate_on=cont_dims,
    )

    ham_kernel = HammingKernel(
        np.ones([len(cat_dims)]),
        [(np.exp(-10), np.exp(2)) for _ in range(len(cat_dims))],
        operate_on=cat_dims,
    )
    noise_kernel = WhiteKernel(
        noise_level=noise,
        noise_level_bounds=(1e-10, 2),
        prior=HorseshoePrior(scale=0.1, rng=rs),
    )
    kernel = cov_amp * (exp_kernel * ham_kernel) + noise_kernel

    bounds = [0] * n_dimensions
    types = np.zeros(n_dimensions)
    for c in cont_dims:
        bounds[c] = (0., 1.)
    for c in cat_dims:
        types[c] = 3
        bounds[c] = (3, np.nan)

    cs = ConfigurationSpace()
    for c in cont_dims:
        cs.add_hyperparameter(UniformFloatHyperparameter('X%d' % c, 0, 1))
    for c in cat_dims:
        cs.add_hyperparameter(
            CategoricalHyperparameter('X%d' % c, [0, 1, 2, 3]))

    model = GaussianProcess(
        configspace=cs,
        bounds=bounds,
        types=types,
        kernel=kernel,
        seed=rs.randint(low=1, high=10000),
        normalize_y=normalize_y,
    )
    return model
Ejemplo n.º 5
0
def get_gp(n_dimensions,
           rs,
           noise=1e-3,
           normalize_y=True,
           average_samples=False,
           n_iter=50):
    from smac.epm.gp_kernels import ConstantKernel, Matern, WhiteKernel

    cov_amp = ConstantKernel(
        2.0,
        constant_value_bounds=(1e-10, 2),
        prior=LognormalPrior(mean=0.0, sigma=1.0, rng=rs),
    )
    exp_kernel = Matern(
        np.ones([n_dimensions]),
        [(np.exp(-10), np.exp(2)) for _ in range(n_dimensions)],
        nu=2.5,
        prior=None,
    )
    noise_kernel = WhiteKernel(
        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
Ejemplo n.º 6
0
 def _convert_photon_to_smac_space(hyperparam: object, name: str):
     """
     Helper function: Convert PHOTON hyperparams to smac params.
     """
     if not hyperparam:
         return None
     if isinstance(hyperparam, PhotonCategorical):
         return CategoricalHyperparameter(name, hyperparam.values)
     elif isinstance(hyperparam, list):
         return CategoricalHyperparameter(name, hyperparam)
     elif isinstance(hyperparam, FloatRange):
         if hyperparam.range_type == 'linspace':
             return UniformFloatHyperparameter(name, hyperparam.start,
                                               hyperparam.stop)
         elif hyperparam.range_type == 'logspace':
             raise NotImplementedError(
                 "Logspace in your float hyperparameter is not implemented in SMAC."
             )
         else:
             return UniformFloatHyperparameter(name, hyperparam.start,
                                               hyperparam.stop)
     elif isinstance(hyperparam, IntegerRange):
         return UniformIntegerHyperparameter(name, hyperparam.start,
                                             hyperparam.stop)
Ejemplo n.º 7
0
 def setUp(self):
     logging.basicConfig()
     self.logger = logging.getLogger(self.__module__ + "." + self.__class__.__name__)
     self.logger.setLevel(logging.DEBUG)
     self.value = 0
     self.cs = ConfigurationSpace()
     self.cs.add_hyperparameters([
         UniformFloatHyperparameter('param_a', -0.2, 1.77, 1.1),
         UniformIntegerHyperparameter('param_b', -3, 10, 1),
         Constant('param_c', 'value'),
         CategoricalHyperparameter('ambigous_categorical', choices=['True', True, 5]),  # True is ambigous here
     ])
     self.test_config = Configuration(self.cs, {'param_a': 0.5,
                                                'param_b': 1,
                                                'param_c': 'value',
                                                'ambigous_categorical': 5})
Ejemplo n.º 8
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)
Ejemplo n.º 9
0
def get_gp(n_dimensions, rs, noise=1e-3, normalize_y=True) -> GaussianProcess:
    from smac.epm.gp_kernels import ConstantKernel, Matern, WhiteKernel

    cov_amp = ConstantKernel(
        2.0,
        constant_value_bounds=(1e-10, 2),
        prior=LognormalPrior(mean=0.0, sigma=1.0, rng=rs),
    )
    exp_kernel = Matern(
        np.ones([n_dimensions]),
        [(np.exp(-10), np.exp(2)) for _ in range(n_dimensions)],
        nu=2.5,
    )
    noise_kernel = WhiteKernel(
        noise_level=noise,
        noise_level_bounds=(1e-10, 2),
        prior=HorseshoePrior(scale=0.1, rng=rs),
    )
    kernel = cov_amp * exp_kernel + noise_kernel

    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 = GaussianProcess(
        configspace=configspace,
        bounds=bounds,
        types=types,
        kernel=kernel,
        seed=rs.randint(low=1, high=10000),
        normalize_y=normalize_y,
        n_opt_restarts=2,
    )
    return model
Ejemplo n.º 10
0
    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:
                self.assertEqual(line[1], -1)
            elif line[0] == 1:
                self.assertEqual(line[2], -1)