def test_not_equals_condition(self):
        hp1 = CategoricalHyperparameter("parent", [0, 1])
        hp2 = UniformIntegerHyperparameter("child", 0, 10)
        cond = NotEqualsCondition(hp2, hp1, 0)
        cond_ = NotEqualsCondition(hp2, hp1, 0)
        self.assertEqual(cond, cond_)

        cond_reverse = NotEqualsCondition(hp1, hp2, 0)
        self.assertNotEqual(cond, cond_reverse)

        self.assertNotEqual(cond, dict())

        self.assertEqual("child | parent != 0", str(cond))
    def test_not_equals_condition(self):
        hp1 = CategoricalHyperparameter("parent", [0, 1])
        hp2 = UniformIntegerHyperparameter("child", 0, 10)
        cond = NotEqualsCondition(hp2, hp1, 0)
        cond_ = NotEqualsCondition(hp2, hp1, 0)
        self.assertEqual(cond, cond_)

        # Test vector value:
        self.assertEqual(cond.vector_value, hp1._inverse_transform(0))
        self.assertEqual(cond.vector_value, cond_.vector_value)

        cond_reverse = NotEqualsCondition(hp1, hp2, 0)
        self.assertNotEqual(cond, cond_reverse)

        self.assertNotEqual(cond, dict())

        self.assertEqual("child | parent != 0", str(cond))
Exemple #3
0
def get_complete_configspace():
    """Creates a configspace that includes all kinds of parameters with
    complicated values. The idea is to provide a configspace that can be
    used to check modules using ConfigSpace as a dependency to check
    compatibility with e.g. Constants, log-scale, etc.

    Returns
    -------
    cs: ConfigurationSpace
        cs containing all kinds of parameters
    """
    cs = ConfigurationSpace()

    hp = {}
    # Add Constants for all allowed types ('int', 'float', 'string')
    hp['alpha'] = Constant("alpha", 0.0001)
    hp['tol'] = Constant("tol", '0.0001')
    hp['verbose'] = Constant("verbose", 1)
    # Add numericals
    # Add Floats
    hp['beta1'] = UniformFloatHyperparameter("beta1", 0.85, 0.95, log=False)
    hp['power_t'] = NormalFloatHyperparameter("power_t", mu=0.5, sigma=0.1, log=False)
    # Add Ints
    hp['momentum'] = UniformIntegerHyperparameter("momentum", 0, 100, False)
    hp['beta2'] = NormalIntegerHyperparameter("beta2", mu=1, sigma=0.001, log=False)
    # Add Floats (log)
    hp['learning_rate_init'] = UniformFloatHyperparameter("learning_rate_init", 0.0001, 0.1, log=True)
    hp['random1'] = NormalFloatHyperparameter("NormalFloat", mu=0, sigma=1, default_value=1, log=True)
    # Add Ints (log)
    hp['random2'] = UniformIntegerHyperparameter("UniformInt", 2, 100, log=True)
    hp['random3'] = NormalIntegerHyperparameter("NormalInt", mu=0, sigma=1, default_value=1, log=True)
    # Add Categorical for allowed types
    hp['activation'] = CategoricalHyperparameter('activation', choices=['identity', 'logistic', 'tanh', 'relu'])
    hp['solver'] = CategoricalHyperparameter('solver', choices=[-2, 0, 2])  # corrresponds to: ‘lbfgs’, ‘sgd’, ‘adam’
    hp['batch_size_auto'] = CategoricalHyperparameter('batch_size_auto', choices=[True, False])
    hp['learning_rate'] = CategoricalHyperparameter('learning_rate', choices=[-0.5, 0.0, 0.5])  # corresponds to {‘constant’, ‘invscaling’, ‘adaptive’}
    # Add Ordinal
    hp['batch_size'] = OrdinalHyperparameter('batch_size', sequence=[32, 64.0, '128'])

    for k, v in hp.items():
        cs.add_hyperparameter(v)

    # learning_rate only with sgd
    c = InCondition(hp['learning_rate'], hp['solver'], [0])
    c = EqualsCondition(hp['momentum'], hp['solver'], 0)
    # learning_rate_init only with sgd or adam
    cs.add_condition(OrConjunction(EqualsCondition(hp['learning_rate'], hp['solver'], 0),  # sgd
                                EqualsCondition(hp['learning_rate'], hp['solver'], 2)))  # adam
    # batch_size only with not batch_size_auto
    cs.add_condition(NotEqualsCondition(hp['batch_size'], hp['batch_size_auto'], True))
    # complicated way for solver == sgd
    #cs.add_condition(AndConjunction(LessThanCondition(hp['power_t'], hp['solver'], 1),
    #                                GreaterThanCondition(hp['power_t'], hp['solver'], -1)))
    # betas with adam
    cs.add_condition(EqualsCondition(hp['beta1'], hp['solver'], 2))
    cs.add_condition(EqualsCondition(hp['beta2'], hp['solver'], 2))

    return cs
Exemple #4
0
def _construct_neq_condition(
    condition: Dict,
    cs: ConfigurationSpace,
) -> NotEqualsCondition:
    return NotEqualsCondition(
        child=cs.get_hyperparameter(condition['child']),
        parent=cs.get_hyperparameter(condition['parent']),
        value=condition['value'],
    )
Exemple #5
0
def condition_specification(child_name, condition, configuration_space):
    # specifies the condition type
    child = configuration_space.get_hyperparameter(child_name)
    parent_name = condition[0]
    parent = configuration_space.get_hyperparameter(parent_name)
    operation = condition[1]
    if operation == 'in':
        restrictions = condition[3:-1:2]
        for i in range(len(restrictions)):
            if isinstance(parent, FloatHyperparameter):
                restrictions[i] = float(restrictions[i])
            elif isinstance(parent, IntegerHyperparameter):
                restrictions[i] = int(restrictions[i])

        if len(restrictions) == 1:
            condition = EqualsCondition(child, parent, restrictions[0])
        else:
            condition = InCondition(child, parent, values=restrictions)
        return condition
    else:
        restrictions = condition[2]
        if isinstance(parent, FloatHyperparameter):
            restrictions = float(restrictions)
        elif isinstance(parent, IntegerHyperparameter):
            restrictions = int(restrictions)

        if operation == '==':
            condition = EqualsCondition(child, parent, restrictions)
        elif operation == '!=':
            condition = NotEqualsCondition(child, parent, restrictions)
        else:
            if isinstance(parent, FloatHyperparameter):
                restrictions = float(restrictions)
            elif isinstance(parent, IntegerHyperparameter):
                restrictions = int(restrictions)
            elif isinstance(parent, OrdinalHyperparameter):
                pass
            else:
                raise ValueError('The parent of a conditional hyperparameter '
                                 'must be either a float, int or ordinal '
                                 'hyperparameter, but is %s.' % type(parent))

            if operation == '<':
                condition = LessThanCondition(child, parent, restrictions)
            elif operation == '>':
                condition = GreaterThanCondition(child, parent, restrictions)
        return condition
Exemple #6
0
    def test_read_new_configuration_space_complex_conditionals(self):
        classi = OrdinalHyperparameter(
            "classi",
            [
                "random_forest", "extra_trees", "k_nearest_neighbors",
                "something"
            ],
        )
        knn_weights = CategoricalHyperparameter("knn_weights",
                                                ["uniform", "distance"])
        weather = OrdinalHyperparameter(
            "weather", ["sunny", "rainy", "cloudy", "snowing"])
        temperature = CategoricalHyperparameter("temperature", ["high", "low"])
        rain = CategoricalHyperparameter("rain", ["yes", "no"])
        gloves = OrdinalHyperparameter("gloves",
                                       ["none", "yarn", "leather", "gortex"])
        heur1 = CategoricalHyperparameter("heur1", ["off", "on"])
        heur2 = CategoricalHyperparameter("heur2", ["off", "on"])
        heur_order = CategoricalHyperparameter("heur_order",
                                               ["heur1then2", "heur2then1"])
        gloves_condition = OrConjunction(
            EqualsCondition(gloves, rain, "yes"),
            EqualsCondition(gloves, temperature, "low"))
        heur_condition = AndConjunction(
            EqualsCondition(heur_order, heur1, "on"),
            EqualsCondition(heur_order, heur2, "on"))
        and_conjunction = AndConjunction(
            NotEqualsCondition(knn_weights, classi, "extra_trees"),
            EqualsCondition(knn_weights, classi, "random_forest"))
        Cl_condition = OrConjunction(
            EqualsCondition(knn_weights, classi, "k_nearest_neighbors"),
            and_conjunction, EqualsCondition(knn_weights, classi, "something"))

        and1 = AndConjunction(EqualsCondition(temperature, weather, "rainy"),
                              EqualsCondition(temperature, weather, "cloudy"))
        and2 = AndConjunction(
            EqualsCondition(temperature, weather, "sunny"),
            NotEqualsCondition(temperature, weather, "snowing"))
        another_condition = OrConjunction(and1, and2)

        complex_conditional_space = ConfigurationSpace()
        complex_conditional_space.add_hyperparameter(classi)
        complex_conditional_space.add_hyperparameter(knn_weights)
        complex_conditional_space.add_hyperparameter(weather)
        complex_conditional_space.add_hyperparameter(temperature)
        complex_conditional_space.add_hyperparameter(rain)
        complex_conditional_space.add_hyperparameter(gloves)
        complex_conditional_space.add_hyperparameter(heur1)
        complex_conditional_space.add_hyperparameter(heur2)
        complex_conditional_space.add_hyperparameter(heur_order)

        complex_conditional_space.add_condition(gloves_condition)
        complex_conditional_space.add_condition(heur_condition)
        complex_conditional_space.add_condition(Cl_condition)
        complex_conditional_space.add_condition(another_condition)

        complex_cs = list()
        complex_cs.append(
            "classi ordinal {random_forest,extra_trees,k_nearest_neighbors, something} "
            "[random_forest]")
        complex_cs.append(
            "knn_weights categorical {uniform, distance} [uniform]")
        complex_cs.append(
            "weather ordinal {sunny, rainy, cloudy, snowing} [sunny]")
        complex_cs.append("temperature categorical {high, low} [high]")
        complex_cs.append("rain categorical { yes, no } [yes]")
        complex_cs.append(
            "gloves ordinal { none, yarn, leather, gortex } [none]")
        complex_cs.append("heur1 categorical { off, on } [off]")
        complex_cs.append("heur2 categorical { off, on } [off]")
        complex_cs.append(
            "heur_order categorical { heur1then2, heur2then1 } [heur1then2]")
        complex_cs.append("gloves | rain == yes || temperature == low")
        complex_cs.append("heur_order | heur1 == on && heur2 == on")
        complex_cs.append(
            "knn_weights | classi == k_nearest_neighbors || "
            "classi != extra_trees && classi == random_forest || classi == something"
        )
        complex_cs.append(
            "temperature | weather == rainy && weather == cloudy || "
            "weather == sunny && weather != snowing")
        cs_new = pcs_new.read(complex_cs)
        self.assertEqual(cs_new, complex_conditional_space)