Пример #1
0
def test_infer_relative_search_space() -> None:
    sampler = TPESampler()
    search_space = {
        "a": distributions.UniformDistribution(1.0, 100.0),
        "b": distributions.LogUniformDistribution(1.0, 100.0),
        "c": distributions.DiscreteUniformDistribution(1.0, 100.0, 3.0),
        "d": distributions.IntUniformDistribution(1, 100),
        "e": distributions.IntUniformDistribution(0, 100, step=2),
        "f": distributions.IntLogUniformDistribution(1, 100),
        "g": distributions.CategoricalDistribution(["x", "y", "z"]),
    }

    def obj(t: Trial) -> float:
        t.suggest_uniform("a", 1.0, 100.0)
        t.suggest_loguniform("b", 1.0, 100.0)
        t.suggest_discrete_uniform("c", 1.0, 100.0, 3.0)
        t.suggest_int("d", 1, 100)
        t.suggest_int("e", 0, 100, step=2)
        t.suggest_int("f", 1, 100, log=True)
        t.suggest_categorical("g", ["x", "y", "z"])
        return 0.0

    # Study and frozen-trial are not supposed to be accessed.
    study1 = Mock(spec=[])
    frozen_trial = Mock(spec=[])
    assert sampler.infer_relative_search_space(study1, frozen_trial) == {}

    study2 = optuna.create_study(sampler=sampler)
    study2.optimize(obj, n_trials=1)
    assert sampler.infer_relative_search_space(study2, study2.best_trial) == {}

    with warnings.catch_warnings():
        warnings.simplefilter("ignore", optuna.exceptions.ExperimentalWarning)
        sampler = TPESampler(multivariate=True)
    study3 = optuna.create_study(sampler=sampler)
    study3.optimize(obj, n_trials=1)
    assert sampler.infer_relative_search_space(
        study3, study3.best_trial) == search_space
Пример #2
0
def test_infer_relative_search_space() -> None:
    sampler = TPESampler()
    # Study and frozen-trial are not supposed to be accessed.
    study = Mock(spec=[])
    frozen_trial = Mock(spec=[])
    assert sampler.infer_relative_search_space(study, frozen_trial) == {}