def test_ordinal_check_order(self):
     f1 = OrdinalHyperparameter("temp",
                                ["freezing", "cold", "warm", "hot"])
     self.assertTrue(f1.check_order("freezing", "cold"))
     self.assertTrue(f1.check_order("freezing", "hot"))
     self.assertFalse(f1.check_order("hot", "cold"))
     self.assertFalse(f1.check_order("hot", "warm"))
 def test_ordinal_check_order(self):
     f1 = OrdinalHyperparameter("temp",
                                ["freezing", "cold", "warm", "hot"])
     self.assertTrue(f1.check_order("freezing", "cold"))
     self.assertTrue(f1.check_order("freezing", "hot"))
     self.assertFalse(f1.check_order("hot", "cold"))
     self.assertFalse(f1.check_order("hot", "warm"))
 def test_ordinal_get_neighbors(self):
     f1 = OrdinalHyperparameter("temp",
                                ["freezing", "cold", "warm", "hot"])
     self.assertEqual(f1.get_neighbors(0, rs=None), [1])
     self.assertEqual(f1.get_neighbors(1, rs=None), [0, 2])
     self.assertEqual(f1.get_neighbors(3, rs=None), [2])
     self.assertEqual(f1.get_neighbors("hot", transform=True, rs=None), ["warm"])
     self.assertEqual(f1.get_neighbors("cold", transform=True, rs=None), ["freezing", "warm"])
Example #4
0
    def test_ordinal_is_legal(self):
        f1 = OrdinalHyperparameter("temp", ["freezing", "cold", "warm", "hot"])
        self.assertTrue(f1.is_legal("warm"))
        self.assertTrue(f1.is_legal("freezing"))
        self.assertFalse(f1.is_legal("chill"))
        self.assertFalse(f1.is_legal(2.5))
        self.assertFalse(f1.is_legal("3"))

        # Test is legal vector
        self.assertTrue(f1.is_legal_vector(1.0))
        self.assertTrue(f1.is_legal_vector(0.0))
        self.assertTrue(f1.is_legal_vector(0))
        self.assertTrue(f1.is_legal_vector(3))
        self.assertFalse(f1.is_legal_vector(-0.1))
        self.assertFalse(f1.is_legal_vector("Hahaha"))
Example #5
0
 def test_hyperparam_representation(self):
     # Float
     f1 = UniformFloatHyperparameter("param", 1, 100, log=True)
     self.assertEqual(
         "param, Type: UniformFloat, Range: [1.0, 100.0], Default: 10.0, on log-scale",
         repr(f1)
     )
     f2 = NormalFloatHyperparameter("param", 8, 99.1, log=False)
     self.assertEqual(
         "param, Type: NormalFloat, Mu: 8.0 Sigma: 99.1, Default: 8.0",
         repr(f2)
     )
     i1 = UniformIntegerHyperparameter("param", 0, 100)
     self.assertEqual(
         "param, Type: UniformInteger, Range: [0, 100], Default: 50",
         repr(i1)
     )
     i2 = NormalIntegerHyperparameter("param", 5, 8)
     self.assertEqual(
         "param, Type: NormalInteger, Mu: 5 Sigma: 8, Default: 5",
         repr(i2)
     )
     o1 = OrdinalHyperparameter("temp", ["freezing", "cold", "warm", "hot"])
     self.assertEqual(
         "temp, Type: Ordinal, Sequence: {freezing, cold, warm, hot}, Default: freezing",
         repr(o1)
     )
     c1 = CategoricalHyperparameter("param", [True, False])
     self.assertEqual(
         "param, Type: Categorical, Choices: {True, False}, Default: True",
         repr(c1)
     )
Example #6
0
def _construct_hyperparameter(hyperparameter: Dict) -> Hyperparameter:
    hp_type = hyperparameter['type']
    name = hyperparameter['name']
    if hp_type == 'constant':
        return Constant(
            name=name,
            value=hyperparameter['value'],
        )
    elif hp_type == 'unparametrized':
        return UnParametrizedHyperparameter(
            name=name,
            value=hyperparameter['value'],
        )
    elif hp_type == 'uniform_float':
        return UniformFloatHyperparameter(
            name=name,
            log=hyperparameter['log'],
            lower=hyperparameter['lower'],
            upper=hyperparameter['upper'],
            default_value=hyperparameter['default'],
        )
    elif hp_type == 'normal_float':
        return NormalFloatHyperparameter(
            name=name,
            log=hyperparameter['log'],
            mu=hyperparameter['mu'],
            sigma=hyperparameter['sigma'],
            default_value=hyperparameter['default'],
        )
    elif hp_type == 'uniform_int':
        return UniformIntegerHyperparameter(
            name=name,
            log=hyperparameter['log'],
            lower=hyperparameter['lower'],
            upper=hyperparameter['upper'],
            default_value=hyperparameter['default'],
        )
    elif hp_type == 'normal_int':
        return NormalIntegerHyperparameter(
            name=name,
            log=hyperparameter['log'],
            lower=hyperparameter['lower'],
            upper=hyperparameter['upper'],
            default_value=hyperparameter['default'],
        )
    elif hp_type == 'categorical':
        return CategoricalHyperparameter(
            name=name,
            choices=hyperparameter['choices'],
            default_value=hyperparameter['default'],
            weights=hyperparameter.get('probabilities'),
        )
    elif hp_type == 'ordinal':
        return OrdinalHyperparameter(
            name=name,
            sequence=hyperparameter['sequence'],
            default_value=hyperparameter['default'],
        )
    else:
        raise ValueError(hp_type)
Example #7
0
    def test_read_new_configuration_space_forbidden(self):
        cs_with_forbidden = ConfigurationSpace()
        int_hp = UniformIntegerHyperparameter('int_hp', 0, 50, 30)
        float_hp = UniformFloatHyperparameter('float_hp', 0., 50., 30.)
        cat_hp_str = CategoricalHyperparameter('cat_hp_str', ['a', 'b', 'c'], 'b')
        ord_hp_str = OrdinalHyperparameter('ord_hp_str', ['a', 'b', 'c'], 'b')

        cs_with_forbidden.add_hyperparameters([int_hp, float_hp, cat_hp_str, ord_hp_str])

        int_hp_forbidden = ForbiddenAndConjunction(ForbiddenEqualsClause(int_hp, 1))

        float_hp_forbidden_1 = ForbiddenEqualsClause(float_hp, 1.0)
        float_hp_forbidden_2 = ForbiddenEqualsClause(float_hp, 2.0)
        float_hp_forbidden = ForbiddenAndConjunction(float_hp_forbidden_1, float_hp_forbidden_2)

        cat_hp_str_forbidden = ForbiddenAndConjunction(ForbiddenEqualsClause(cat_hp_str, 'a'))
        ord_hp_float_forbidden = ForbiddenAndConjunction(ForbiddenEqualsClause(ord_hp_str, 'a'))
        cs_with_forbidden.add_forbidden_clauses([int_hp_forbidden, float_hp_forbidden,
                                                 cat_hp_str_forbidden, ord_hp_float_forbidden])

        complex_cs = list()
        complex_cs.append("int_hp integer [0,50] [30]")
        complex_cs.append("float_hp real [0.0, 50.0] [30.0]")
        complex_cs.append("cat_hp_str categorical {a, b, c} [b]")
        complex_cs.append("ord_hp_str ordinal {a, b, c} [b]")
        complex_cs.append("# Forbiddens:")
        complex_cs.append("{int_hp=1}")
        complex_cs.append("{float_hp=1.0, float_hp=2.0}")
        complex_cs.append("{cat_hp_str=a}")
        complex_cs.append("{ord_hp_str=a}")
        cs_new = pcs_new.read(complex_cs)

        self.assertEqual(cs_new, cs_with_forbidden)
 def test_write_ordinal(self):
     expected = "ord_a '--ord_a ' o {a,b,3}\n"
     cs = ConfigurationSpace()
     cs.add_hyperparameter(
         OrdinalHyperparameter("ord_a", ["a", "b", 3]))
     value = irace.write(cs)
     self.assertEqual(expected, value)
 def test_ordinal_attributes_accessible(self):
     f1 = OrdinalHyperparameter("temp", ["freezing", "cold", "warm", "hot"])
     self.assertEqual(f1.name, "temp")
     self.assertTupleEqual(f1.sequence, ("freezing", "cold", "warm", "hot"))
     self.assertEqual(f1.num_elements, 4)
     self.assertEqual(f1.default_value, "freezing")
     self.assertEqual(f1.normalized_default_value, 0)
Example #10
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
Example #11
0
    def test_greater_and_less_condition(self):
        child = Constant('child', 'child')
        hp1 = UniformFloatHyperparameter("float", 0, 5)
        hp2 = UniformIntegerHyperparameter("int", 0, 5)
        hp3 = OrdinalHyperparameter("ord", list(range(6)))

        for hp in [hp1, hp2, hp3]:
            gt = GreaterThanCondition(child, hp, 1)
            self.assertFalse(gt.evaluate({hp.name: 0}))
            self.assertTrue(gt.evaluate({hp.name: 2}))
            self.assertFalse(gt.evaluate({hp.name: None}))

            lt = LessThanCondition(child, hp, 1)
            self.assertTrue(lt.evaluate({hp.name: 0}))
            self.assertFalse(lt.evaluate({hp.name: 2}))
            self.assertFalse(lt.evaluate({hp.name: None}))

        hp4 = CategoricalHyperparameter("cat", list(range(6)))
        self.assertRaisesRegexp(
            ValueError, "Parent hyperparameter in a > or < "
            "condition must be a subclass of "
            "NumericalHyperparameter or "
            "OrdinalHyperparameter, but is "
            "<class 'ConfigSpace.hyperparameters.CategoricalHyperparameter'>",
            GreaterThanCondition, child, hp4, 1)
        self.assertRaisesRegexp(
            ValueError, "Parent hyperparameter in a > or < "
            "condition must be a subclass of "
            "NumericalHyperparameter or "
            "OrdinalHyperparameter, but is "
            "<class 'ConfigSpace.hyperparameters.CategoricalHyperparameter'>",
            LessThanCondition, child, hp4, 1)

        hp5 = OrdinalHyperparameter("ord",
                                    ['cold', 'luke warm', 'warm', 'hot'])

        gt = GreaterThanCondition(child, hp5, 'warm')
        self.assertTrue(gt.evaluate({hp5.name: 'hot'}))
        self.assertFalse(gt.evaluate({hp5.name: 'cold'}))

        lt = LessThanCondition(child, hp5, 'warm')
        self.assertTrue(lt.evaluate({hp5.name: 'luke warm'}))
        self.assertFalse(lt.evaluate({hp5.name: 'warm'}))
    def test_ordinal_is_legal(self):
        f1 = OrdinalHyperparameter("temp", 
                                   ["freezing", "cold", "warm", "hot"])
        self.assertTrue(f1.is_legal("warm"))
        self.assertTrue(f1.is_legal(u"freezing"))
        self.assertFalse(f1.is_legal("chill"))
        self.assertFalse(f1.is_legal(2.5))
        self.assertFalse(f1.is_legal("3"))

        # Test is legal vector
        self.assertTrue(f1.is_legal_vector(1.0))
        self.assertTrue(f1.is_legal_vector(0.0))
        self.assertTrue(f1.is_legal_vector(0))
        self.assertTrue(f1.is_legal_vector(3))
        self.assertFalse(f1.is_legal_vector(-0.1))
        self.assertFalse(f1.is_legal_vector("Hahaha"))
Example #13
0
 class TruncatedSVD(object, metaclass=Metaclass):
     # static variables
     _name = "TruncatedSVD"
     _model = decomposition.TruncatedSVD
     _params = [
         OrdinalHyperparameter("n_components", sequence=list(range(2, 10)), default_value=2),
         CategoricalHyperparameter("algorithm", ['arpack','randomized'], default_value='randomized'),
         # OrdinalHyperparameter("random_state", sequence=list(range(10)), default_value=1)
     ]
     _params_names = set([p.name for p in _params])
     _conditions = []
     _forbidden_clauses = []
Example #14
0
 class LatentDirichletAllocation(object, metaclass=Metaclass):
     # static variables
     _name = "LatentDirichletAllocation"
     _model = decomposition.LatentDirichletAllocation
     _params = [
         OrdinalHyperparameter("n_components", sequence=list(range(2, 10)), default_value=2),
         CategoricalHyperparameter("learning_method", ['batch','online'], default_value='batch'),
         # OrdinalHyperparameter("random_state", sequence=list(range(10)), default_value=1)
     ]
     _params_names = set([p.name for p in _params])
     _conditions = []
     _forbidden_clauses = []
Example #15
0
 class IncrementalPCA(object, metaclass=Metaclass):
     # static variables
     _name = "IncrementalPCA"
     _model = decomposition.IncrementalPCA
     _params = [
         OrdinalHyperparameter("n_components", sequence=list(range(2, 4)), default_value=2),
         CategoricalHyperparameter("whiten", [True, False], default_value=False),
         UniformIntegerHyperparameter("batch_size", 10, 1000, default_value=100)
     ]
     _params_names = set([p.name for p in _params]) 
     _conditions = []
     _forbidden_clauses = []
Example #16
0
 class KernelPCA(object, metaclass=Metaclass):
     # static variables
     _name = "KernelPCA"
     _model = decomposition.KernelPCA
     _params = [
         OrdinalHyperparameter("n_components", sequence=list(range(2, 10)), default_value=2),
         CategoricalHyperparameter("kernel", ['linear','poly','rbf','sigmoid','cosine'], default_value='linear'),
         # OrdinalHyperparameter("random_state", sequence=list(range(10)), default_value=1)
     ]
     _params_names = set([p.name for p in _params])
     _conditions = []
     _forbidden_clauses = []
 def test_ordinal_is_legal(self):
     f1 = OrdinalHyperparameter("temp", ["freezing", "cold", "warm", "hot"])
     self.assertTrue(f1.is_legal("warm"))
     self.assertTrue(f1.is_legal(u"freezing"))
     self.assertFalse(f1.is_legal("chill"))
     self.assertFalse(f1.is_legal(2.5))
     self.assertFalse(f1.is_legal("3"))
Example #18
0
 def test_ordinal_get_neighbors(self):
     f1 = OrdinalHyperparameter("temp", 
                                ["freezing", "cold", "warm", "hot"])
     self.assertEqual(f1.get_neighbors("freezing", rs=None), [2])
     self.assertEqual(f1.get_neighbors("cold", transform=True, rs=None), ["freezing", "warm"])
     self.assertEqual(f1.get_neighbors("hot", rs=None), [3])
     self.assertEqual(f1.get_neighbors("hot", transform =True, rs=None), ["warm"])
Example #19
0
 class TSNE(object, metaclass=Metaclass):
     # static variables
     _name = "TSNE"
     _model = manifold.TSNE
     _params = [
         OrdinalHyperparameter("n_components", sequence=list(range(2,3)), default_value=2),
         UniformFloatHyperparameter("perplexity", 1, 300, default_value=30),
         UniformFloatHyperparameter("early_exaggeration", 5.0, 20.0, default_value=12.0),
         # OrdinalHyperparameter("random_state", sequence=list(range(10)), default_value=0)
     ]
     _params_names = set([p.name for p in _params]) 
     _conditions = []
     _forbidden_clauses = []
Example #20
0
 class FastICA(object, metaclass=Metaclass):
     # static variables
     _name = "FastICA"
     _model = decomposition.FastICA
     _params = [
         OrdinalHyperparameter("n_components", sequence=list(range(2, 4)), default_value=2),
         CategoricalHyperparameter("algorithm", ['parallel', 'deflation'], default_value='parallel'),
         CategoricalHyperparameter("fun", ['logcosh', 'exp','cube'], default_value='logcosh'),
         CategoricalHyperparameter("whiten", [True,False], default_value=True),
         # OrdinalHyperparameter("random_state", sequence=list(range(10)), default_value=1)
     ]
     _params_names = set([p.name for p in _params])
     _conditions = []
     _forbidden_clauses = []
Example #21
0
 def test_meta_field(self):
     cs = ConfigurationSpace()
     cs.add_hyperparameter(
         UniformIntegerHyperparameter("uihp",
                                      lower=1,
                                      upper=10,
                                      meta=dict(uihp=True)))
     cs.add_hyperparameter(
         NormalIntegerHyperparameter("nihp",
                                     mu=0,
                                     sigma=1,
                                     meta=dict(nihp=True)))
     cs.add_hyperparameter(
         UniformFloatHyperparameter("ufhp",
                                    lower=1,
                                    upper=10,
                                    meta=dict(ufhp=True)))
     cs.add_hyperparameter(
         NormalFloatHyperparameter("nfhp",
                                   mu=0,
                                   sigma=1,
                                   meta=dict(nfhp=True)))
     cs.add_hyperparameter(
         CategoricalHyperparameter("chp",
                                   choices=['1', '2', '3'],
                                   meta=dict(chp=True)))
     cs.add_hyperparameter(
         OrdinalHyperparameter("ohp",
                               sequence=['1', '2', '3'],
                               meta=dict(ohp=True)))
     cs.add_hyperparameter(Constant("const", value=1,
                                    meta=dict(const=True)))
     parent = ConfigurationSpace()
     parent.add_configuration_space("sub", cs, delimiter=':')
     self.assertEqual(
         parent.get_hyperparameter("sub:uihp").meta, dict(uihp=True))
     self.assertEqual(
         parent.get_hyperparameter("sub:nihp").meta, dict(nihp=True))
     self.assertEqual(
         parent.get_hyperparameter("sub:ufhp").meta, dict(ufhp=True))
     self.assertEqual(
         parent.get_hyperparameter("sub:nfhp").meta, dict(nfhp=True))
     self.assertEqual(
         parent.get_hyperparameter("sub:chp").meta, dict(chp=True))
     self.assertEqual(
         parent.get_hyperparameter("sub:ohp").meta, dict(ohp=True))
     self.assertEqual(
         parent.get_hyperparameter("sub:const").meta, dict(const=True))
Example #22
0
    def test_with_ordinal(self):
        cs = smac.configspace.ConfigurationSpace()
        a = cs.add_hyperparameter(
            CategoricalHyperparameter('a', [0, 1], default_value=0))
        b = cs.add_hyperparameter(
            OrdinalHyperparameter('b', [0, 1], default_value=1))
        b = cs.add_hyperparameter(
            UniformFloatHyperparameter('c',
                                       lower=0.,
                                       upper=1.,
                                       default_value=1))
        b = cs.add_hyperparameter(
            UniformIntegerHyperparameter('d',
                                         lower=0,
                                         upper=10,
                                         default_value=1))
        cs.seed(1)

        feat_array = np.array([0, 0, 0]).reshape(1, -1)
        types, bounds = get_types(cs, feat_array)
        model = RandomForestWithInstances(types=types,
                                          bounds=bounds,
                                          instance_features=feat_array,
                                          seed=1,
                                          ratio_features=1.0,
                                          pca_components=9)
        self.assertEqual(bounds[0][0], 2)
        self.assertTrue(bounds[0][1] is np.nan)
        self.assertEqual(bounds[1][0], 0)
        self.assertEqual(bounds[1][1], 1)
        self.assertEqual(bounds[2][0], 0.)
        self.assertEqual(bounds[2][1], 1.)
        self.assertEqual(bounds[3][0], 0.)
        self.assertEqual(bounds[3][1], 1.)
        X = np.array(
            [[0., 0., 0., 0., 0., 0., 0.], [0., 0., 1., 0., 0., 0., 0.],
             [0., 1., 0., 9., 0., 0., 0.], [0., 1., 1., 4., 0., 0., 0.]],
            dtype=np.float64)
        y = np.array([0, 1, 2, 3], dtype=np.float64)

        X_train = np.vstack((X, X, X, X, X, X, X, X, X, X))
        y_train = np.vstack((y, y, y, y, y, y, y, y, y, y))

        model.train(X_train, y_train.reshape((-1, 1)))
        mean, _ = model.predict(X)
        for idx, m in enumerate(mean):
            self.assertAlmostEqual(y[idx], m, 0.05)
Example #23
0
    class PCA(object, metaclass=Metaclass):
		# static variables
        _name = "PCA"
        _model = decomposition.PCA
        _params = [
            OrdinalHyperparameter("n_components", sequence=list(range(2, 4)), default_value=2),
            CategoricalHyperparameter("svd_solver", ['auto', 'full', 'arpack', 'randomized'], default_value='auto'),
            CategoricalHyperparameter("whiten", [True, False], default_value=False),
            
            # "random_state" was included, used only when "svd_solver" = 'arpack', or 'randomized'
            # OrdinalHyperparameter("random_state", sequence=list(range(10)), default_value=0)
        ]
        _params_names = set([p.name for p in _params])
        _conditions = [
            # InCondition(child=_params[3], parent=_params[1], values=['arpack', 'randomized'])
        ]
        _forbidden_clauses = []
Example #24
0
 def test_repr_roundtrip(self):
     cs = ConfigurationSpace()
     cs.add_hyperparameter(
         UniformIntegerHyperparameter("uihp", lower=1, upper=10))
     cs.add_hyperparameter(
         NormalIntegerHyperparameter("nihp", mu=0, sigma=1))
     cs.add_hyperparameter(
         UniformFloatHyperparameter("ufhp", lower=1, upper=10))
     cs.add_hyperparameter(NormalFloatHyperparameter("nfhp", mu=0, sigma=1))
     cs.add_hyperparameter(
         CategoricalHyperparameter("chp", choices=['1', '2', '3']))
     cs.add_hyperparameter(
         OrdinalHyperparameter("ohp", sequence=['1', '2', '3']))
     cs.add_hyperparameter(Constant("const", value=1))
     default = cs.get_default_configuration()
     repr = default.__repr__()
     repr = repr.replace('})', '}, configuration_space=cs)')
     config = eval(repr)
     self.assertEqual(default, config)
Example #25
0
 def test_get_types(self):
     cs = ConfigurationSpace()
     cs.add_hyperparameter(CategoricalHyperparameter('a', ['a', 'b']))
     cs.add_hyperparameter(UniformFloatHyperparameter('b', 1, 5))
     cs.add_hyperparameter(UniformIntegerHyperparameter('c', 3, 7))
     cs.add_hyperparameter(Constant('d', -5))
     cs.add_hyperparameter(OrdinalHyperparameter('e', ['cold', 'hot']))
     cs.add_hyperparameter(CategoricalHyperparameter('f', ['x', 'y']))
     types, bounds = get_types(cs, None)
     np.testing.assert_array_equal(types, [2, 0, 0, 0, 0, 2])
     self.assertEqual(bounds[0][0], 2)
     self.assertFalse(np.isfinite(bounds[0][1]))
     np.testing.assert_array_equal(bounds[1], [0, 1])
     np.testing.assert_array_equal(bounds[2], [0, 1])
     self.assertEqual(bounds[3][0], 0)
     self.assertFalse(np.isfinite(bounds[3][1]))
     np.testing.assert_array_equal(bounds[4], [0, 1])
     self.assertEqual(bounds[5][0], 2)
     self.assertFalse(np.isfinite(bounds[5][1]))
Example #26
0
 class NullModel(object, metaclass=Metaclass):
     # fake model class
     class model(object):
         def __init__(self, random_state=1):
             pass
         
         def fit_transform(self, data):
             return data
         
         def transform(self, data):
             return data
     
     # static variables
     # this is a dummy class, if user chooses this algorithm, then no dimension reduction is done
     _name = "NullModel"
     _model = model
     _params = [
         OrdinalHyperparameter("random_state", sequence=list(range(3)), default_value=1)
     ]
     _params_names = set([p.name for p in _params])
     _conditions = []
     _forbidden_clauses = []
Example #27
0
def build_config_space(data):
    # Build Configuration Space which defines all parameters and their ranges
    cs = ConfigurationSpace()

    params = {}

    for i in data["parameters"]:
        obj = data["parameters"][i]
        if obj["type"] == "integer":
            if min(obj["values"]) < max(obj["values"]):
                params[i] = UniformIntegerHyperparameter(
                    i,
                    min(obj["values"]),
                    max(obj["values"]),
                    default_value=obj["default"])
                cs.add_hyperparameter(params[i])
            else:
                logging.warning(
                    "Parameter {} can take only one value. It will be ignored".
                    format(i))
        elif obj["type"] == "ordinal":
            if len(obj["values"]) > 1:
                params[i] = OrdinalHyperparameter(i,
                                                  obj["values"],
                                                  default_value=obj["default"])
                cs.add_hyperparameter(params[i])
            else:
                logging.warning(
                    "Parameter {} can take only one value. It will be ignored".
                    format(i))
        else:
            logging.critical(
                "Parameter type {} for parameter {} not supported. Exiting".
                format(obj["type"], i))
            sys.exit(1)
    return cs
Example #28
0
 def test_get_types_with_inactive(self):
     cs = ConfigurationSpace()
     a = cs.add_hyperparameter(CategoricalHyperparameter('a', ['a', 'b']))
     b = cs.add_hyperparameter(UniformFloatHyperparameter('b', 1, 5))
     c = cs.add_hyperparameter(UniformIntegerHyperparameter('c', 3, 7))
     d = cs.add_hyperparameter(Constant('d', -5))
     e = cs.add_hyperparameter(OrdinalHyperparameter('e', ['cold', 'hot']))
     f = cs.add_hyperparameter(CategoricalHyperparameter('f', ['x', 'y']))
     cs.add_condition(EqualsCondition(b, a, 'a'))
     cs.add_condition(EqualsCondition(c, a, 'a'))
     cs.add_condition(EqualsCondition(d, a, 'a'))
     cs.add_condition(EqualsCondition(e, a, 'a'))
     cs.add_condition(EqualsCondition(f, a, 'a'))
     types, bounds = get_types(cs, None)
     np.testing.assert_array_equal(types, [2, 0, 0, 2, 0, 3])
     self.assertEqual(bounds[0][0], 2)
     self.assertFalse(np.isfinite(bounds[0][1]))
     np.testing.assert_array_equal(bounds[1], [-1, 1])
     np.testing.assert_array_equal(bounds[2], [-1, 1])
     self.assertEqual(bounds[3][0], 2)
     self.assertFalse(np.isfinite(bounds[3][1]))
     np.testing.assert_array_equal(bounds[4], [0, 2])
     self.assertEqual(bounds[5][0], 3)
     self.assertFalse(np.isfinite(bounds[5][1]))
 def test_ordinal_get_value(self):
     f1 = OrdinalHyperparameter("temp",
                                ["freezing", "cold", "warm", "hot"])
     self.assertEqual(f1.get_value(3), "hot")
     self.assertNotEqual(f1.get_value(1), "warm")
Example #30
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)
 def test_get_num_neighbors(self):
     f1 = OrdinalHyperparameter("temp",
                                ["freezing", "cold", "warm", "hot"])
     self.assertEqual(f1.get_num_neighbors("freezing"), 1)
     self.assertEqual(f1.get_num_neighbors("hot"), 1)
     self.assertEqual(f1.get_num_neighbors("cold"), 2)
 def test_ordinal_get_seq_order(self):
     f1 = OrdinalHyperparameter("temp",
                                ["freezing", "cold", "warm", "hot"])
     self.assertEqual(tuple(f1.get_seq_order()), tuple([0, 1, 2, 3]))
 def test_ordinal_get_order(self):
     f1 = OrdinalHyperparameter("temp",
                                ["freezing", "cold", "warm", "hot"])
     self.assertEqual(f1.get_order("warm"), 2)
     self.assertNotEqual(f1.get_order("freezing"), 3)
 def test_get_num_neighbors(self):
     f1 = OrdinalHyperparameter("temp",
                                ["freezing", "cold", "warm", "hot"])
     self.assertEqual(f1.get_num_neighbors("freezing"), 1)
     self.assertEqual(f1.get_num_neighbors("hot"), 1)
     self.assertEqual(f1.get_num_neighbors("cold"), 2)
 def test_ordinal_get_seq_order(self):
     f1 = OrdinalHyperparameter("temp",
                                ["freezing", "cold", "warm", "hot"])
     self.assertEqual(tuple(f1.get_seq_order()), tuple([0, 1, 2, 3]))
Example #36
0
def get_cs_dimensions(api_config):
    """
	Help routine to setup ConfigurationSpace search space in constructor.
	Take api_config as argument so this can be static.
	Parameters
	----------
	api_config: Dict
		api dictionary to construct
	Returns
	-------
	cs: ConfigurationSpace
		ConfigurationSpace that contains the same hyperparameter as api_config
	"""
    # TODO 2 options to transform the real and int hyperaparameters in different scales
    #  option 1: similar to example_submission.skopt.optimizer, merge 'logit' into 'log' and 'bilog' into 'linear'
    #  option 2: use the api bayesmark.space.space to warp and unwarp the samples
    cs = ConfigurationSpace()
    param_list = sorted(api_config.keys())

    # hp_list = []
    for param_name in param_list:
        param_config = api_config[param_name]

        param_type = param_config["type"]
        param_space = param_config.get("space", None)
        param_values = param_config.get("values", None)
        param_range = param_config.get("range", None)

        if param_type == "cat":
            assert param_space is None
            assert param_range is None
            hp = CategoricalHyperparameter(name=param_name,
                                           choices=param_values)
        elif param_type == "bool":
            assert param_space is None
            assert param_values is None
            assert param_range is None
            hp = CategoricalHyperparameter(name=param_name,
                                           choices=[True, False])
        elif param_type == "ordinal":
            # appear in example_submission.skopt.optimizer but not in README
            assert param_space is None
            assert param_range is None
            hp = OrdinalHyperparameter(name=param_name, sequence=param_values)
        elif param_type in ("int", "real"):
            if param_values is not None:
                # TODO: decide whether we treat these parameters as discrete values
                #  or step function (example see example_submission.skopt.optimizer, line 71-77)
                # sort the values to store them in OrdinalHyperparameter
                param_values_sorted = np.sort(param_values)
                hp = OrdinalHyperparameter(name=param_name,
                                           sequence=param_values_sorted)
            else:
                log = True if param_space in ("log", "logit") else False
                if param_type == "int":
                    hp = UniformIntegerHyperparameter(name=param_name,
                                                      lower=param_range[0],
                                                      upper=param_range[-1],
                                                      log=log)
                else:
                    hp = UniformFloatHyperparameter(name=param_name,
                                                    lower=param_range[0],
                                                    upper=param_range[-1],
                                                    log=log)
        else:
            assert False, "type %s not handled in API" % param_type
        cs.add_hyperparameter(hp)

    return cs
 def test_ordinal_get_order(self):
     f1 = OrdinalHyperparameter("temp",
                                ["freezing", "cold", "warm", "hot"])
     self.assertEqual(f1.get_order("warm"), 2)
     self.assertNotEqual(f1.get_order("freezing"), 3)