def test_normalfloat_is_legal(self): f1 = NormalFloatHyperparameter("param", 0, 10) self.assertTrue(f1.is_legal(3.0)) self.assertTrue(f1.is_legal(2)) self.assertFalse(f1.is_legal("Hahaha")) # Test legal vector values 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(0.3)) self.assertTrue(f1.is_legal_vector(-0.1)) self.assertTrue(f1.is_legal_vector(1.1)) self.assertRaises(TypeError, f1.is_legal_vector, "Hahaha")
def test_uniformfloat_transform(self): """This checks whether a value sampled through the configuration space (it does not happend when the variable is sampled alone) stays equal when it is serialized via JSON and the deserialized again.""" cs = ConfigurationSpace() a = cs.add_hyperparameter(UniformFloatHyperparameter('a', -5, 10)) b = cs.add_hyperparameter( NormalFloatHyperparameter('b', 1, 2, log=True)) for i in range(100): config = cs.sample_configuration() value = OrderedDict(sorted(config.get_dictionary().items())) string = json.dumps(value) saved_value = json.loads(string) saved_value = OrderedDict(sorted(byteify(saved_value).items())) self.assertEqual(repr(value), repr(saved_value)) # Next, test whether the truncation also works when initializing the # Configuration with a dictionary for i in range(100): rs = np.random.RandomState(1) value_a = a.sample(rs) value_b = b.sample(rs) values_dict = {'a': value_a, 'b': value_b} config = Configuration(cs, values=values_dict) string = json.dumps(config.get_dictionary()) saved_value = json.loads(string) saved_value = byteify(saved_value) self.assertEqual(values_dict, saved_value)
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'], ) elif hp_type == 'ordinal': return OrdinalHyperparameter( name=name, sequence=hyperparameter['sequence'], default_value=hyperparameter['default'], ) else: raise ValueError(hp_type)
def test_sample_NormalFloatHyperparameter(self): hp = NormalFloatHyperparameter("nfhp", 0, 1) def actual_test(): rs = np.random.RandomState(1) counts_per_bin = [0 for i in range(11)] for i in range(100000): value = hp.sample(rs) index = min(max(int((np.round(value + 0.5)) + 5), 0), 9) counts_per_bin[index] += 1 self.assertEqual( [0, 4, 138, 2113, 13394, 34104, 34282, 13683, 2136, 146, 0], counts_per_bin) self.assertIsInstance(value, float) return counts_per_bin self.assertEqual(actual_test(), actual_test())
def test_normalfloat_to_integer(self): f1 = NormalFloatHyperparameter("param", 0, 10) f2_expected = NormalIntegerHyperparameter("param", 0, 10) f2_actual = f1.to_integer() self.assertEqual(f2_expected, f2_actual)
def test_normalfloat_to_uniformfloat(self): f1 = NormalFloatHyperparameter("param", 0, 10, q=0.1) f1_expected = UniformFloatHyperparameter("param", -30, 30, q=0.1) f1_actual = f1.to_uniform() self.assertEqual(f1_expected, f1_actual)
def test_normalfloat(self): # TODO test non-equality f1 = NormalFloatHyperparameter("param", 0.5, 10.5) f1_ = NormalFloatHyperparameter("param", 0.5, 10.5) self.assertEqual(f1, f1_) self.assertEqual( "param, Type: NormalFloat, Mu: 0.5 Sigma: 10.5, Default: 0.5", str(f1)) f2 = NormalFloatHyperparameter("param", 0, 10, q=0.1) f2_ = NormalFloatHyperparameter("param", 0, 10, q=0.1) self.assertEqual(f2, f2_) self.assertEqual( "param, Type: NormalFloat, Mu: 0.0 Sigma: 10.0, Default: 0.0, " "Q: 0.1", str(f2)) f3 = NormalFloatHyperparameter("param", 0, 10, log=True) f3_ = NormalFloatHyperparameter("param", 0, 10, log=True) self.assertEqual(f3, f3_) self.assertEqual( "param, Type: NormalFloat, Mu: 0.0 Sigma: 10.0, Default: 0.0, " "on log-scale", str(f3)) f4 = NormalFloatHyperparameter("param", 0, 10, default_value=1.0) f4_ = NormalFloatHyperparameter("param", 0, 10, default_value=1.0) self.assertEqual(f4, f4_) self.assertEqual( "param, Type: NormalFloat, Mu: 0.0 Sigma: 10.0, Default: 1.0", str(f4)) f5 = NormalFloatHyperparameter("param", 0, 10, default_value=1.0, q=0.1, log=True) f5_ = NormalFloatHyperparameter("param", 0, 10, default_value=1.0, q=0.1, log=True) self.assertEqual(f5, f5_) self.assertEqual( "param, Type: NormalFloat, Mu: 0.0 Sigma: 10.0, Default: 1.0, " "on log-scale, Q: 0.1", str(f5)) self.assertNotEqual(f1, f2) self.assertNotEqual(f1, "UniformFloat") # test that meta-data is stored correctly f_meta = NormalFloatHyperparameter("param", 0.1, 10, q=0.1, log=True, default_value=1.0, meta=dict(self.meta_data)) self.assertEqual(f_meta.meta, self.meta_data)