def test_bayesian_oracle_with_zero_y(tmp_dir): hp_list = [hp_module.Choice('a', [1, 2], default=1), hp_module.Int('b', 3, 10, default=3), hp_module.Float('c', 0, 1, 0.1, default=0), hp_module.Fixed('d', 7), hp_module.Choice('e', [9, 0], default=9)] oracle = bo_module.BayesianOptimizationOracle() for i in range(100): oracle.populate_space(str(i), hp_list) oracle.result(str(i), 0)
def test_save_before_result(tmp_dir): hp_list = [hp_module.Choice('a', [1, 2], default=1), hp_module.Int('b', 3, 10, default=3), hp_module.Float('c', 0, 1, 0.1, default=0), hp_module.Fixed('d', 7), hp_module.Choice('e', [9, 0], default=9)] oracle = bo_module.BayesianOptimizationOracle() oracle.populate_space(str(1), hp_list) oracle.save(os.path.join(tmp_dir, 'temp_oracle')) oracle.result(str(1), 0)
def test_Fixed(): fixed = hp_module.Fixed('fixed', 'value') fixed = hp_module.Fixed.from_config(fixed.get_config()) assert fixed.default == 'value' assert fixed.random_sample() == 'value' fixed = hp_module.Fixed('fixed', True) assert fixed.default is True assert fixed.random_sample() is True fixed = hp_module.Fixed('fixed', False) fixed = hp_module.Fixed.from_config(fixed.get_config()) assert fixed.default is False assert fixed.random_sample() is False fixed = hp_module.Fixed('fixed', 1) assert fixed.value == 1 assert fixed.random_sample() == 1 fixed = hp_module.Fixed('fixed', 8.2) assert fixed.value == 8.2 assert fixed.random_sample() == 8.2 with pytest.raises(ValueError, match='value must be an'): hp_module.Fixed('fixed', None)
def test_Fixed(): fixed = hp_module.Fixed("fixed", "value") fixed = hp_module.Fixed.from_config(fixed.get_config()) assert fixed.default == "value" assert fixed.random_sample() == "value" fixed = hp_module.Fixed("fixed", True) assert fixed.default is True assert fixed.random_sample() is True fixed = hp_module.Fixed("fixed", False) fixed = hp_module.Fixed.from_config(fixed.get_config()) assert fixed.default is False assert fixed.random_sample() is False fixed = hp_module.Fixed("fixed", 1) assert fixed.value == 1 assert fixed.random_sample() == 1 fixed = hp_module.Fixed("fixed", 8.2) assert fixed.value == 8.2 assert fixed.random_sample() == 8.2 with pytest.raises(ValueError, match="value must be an"): hp_module.Fixed("fixed", None)
def test_bayesian_dynamic_space(tmp_dir): hp_list = [hp_module.Choice('a', [1, 2], default=1)] oracle = bo_module.BayesianOptimizationOracle() for i in range(10): oracle.populate_space(str(i), hp_list) oracle.result(str(i), i) hp_list.append(hp_module.Int('b', 3, 10, default=3)) assert 'b' in oracle.populate_space('1_0', hp_list)['values'] hp_list.append(hp_module.Float('c', 0, 1, 0.1, default=0)) assert 'c' in oracle.populate_space('1_1', hp_list)['values'] hp_list.append(hp_module.Fixed('d', 7)) assert 'd' in oracle.populate_space('1_2', hp_list)['values'] hp_list.append(hp_module.Choice('e', [9, 0], default=9)) assert 'e' in oracle.populate_space('1_3', hp_list)['values']
def test_Fixed(): fixed = hp_module.Fixed('fixed', 'value') fixed = hp_module.Fixed.from_config(fixed.get_config()) assert fixed.default == 'value' assert fixed.random_sample() == 'value'
def get_hyperparameter(value, hp, dtype): if value is None: return hp elif isinstance(value, dtype): return hyperparameters.Fixed(hp.name, value) return value