Exemple #1
0
 def objective(trial: Trial) -> float:
     x0 = trial.suggest_float("x0", 0, 1)
     x1 = trial.suggest_float("x1", 0.1, 1, log=True)
     x2 = trial.suggest_float("x2", 0, 1, step=0.1)
     x3 = trial.suggest_int("x3", 0, 2)
     x4 = trial.suggest_int("x4", 2, 4, log=True)
     x5 = trial.suggest_int("x5", 0, 4, step=2)
     x6 = cast(float, trial.suggest_categorical("x6", [0.1, 0.2, 0.3]))
     return x0 + x1 + x2 + x3 + x4 + x5 + x6
Exemple #2
0
def objective_func_branched_search_space(trial: Trial) -> float:

    c = trial.suggest_categorical("c", ("A", "B"))
    if c == "A":
        x = trial.suggest_float("x", -10, 10)
        return (x + 5) ** 2
    else:
        y = trial.suggest_float("y", -10, 10)
        return (y + 5) ** 2
Exemple #3
0
 def objective(trial: Trial) -> float:
     a = trial.suggest_float("a", 1, 9)
     b = trial.suggest_float("b", 1, 9, log=True)
     c = trial.suggest_float("c", 1, 9, step=1)
     d = trial.suggest_int("d", 1, 9)
     e = trial.suggest_int("e", 1, 9, log=True)
     f = trial.suggest_int("f", 1, 9, step=2)
     g = cast(int, trial.suggest_categorical("g", range(1, 10)))
     return a + b + c + d + e + f + g
Exemple #4
0
def test_check_distribution_suggest_categorical(storage_mode: str) -> None:

    sampler = samplers.RandomSampler()
    with StorageSupplier(storage_mode) as storage:
        study = create_study(storage=storage, sampler=sampler)
        trial = Trial(study, study._storage.create_new_trial(study._study_id))

        trial.suggest_categorical("x", [10, 20, 30])

        with pytest.raises(ValueError):
            trial.suggest_categorical("x", [10, 20])

        with pytest.raises(ValueError):
            trial.suggest_int("x", 10, 20)

        trial = Trial(study, study._storage.create_new_trial(study._study_id))
        with pytest.raises(ValueError):
            trial.suggest_int("x", 10, 20)
Exemple #5
0
def test_check_distribution_suggest_categorical(
        storage_init_func: Callable[[], storages.BaseStorage]) -> None:

    sampler = samplers.RandomSampler()
    study = create_study(storage_init_func(), sampler=sampler)
    trial = Trial(study, study._storage.create_new_trial(study._study_id))

    trial.suggest_categorical("x", [10, 20, 30])

    with pytest.raises(ValueError):
        trial.suggest_categorical("x", [10, 20])

    with pytest.raises(ValueError):
        trial.suggest_int("x", 10, 20)

    trial = Trial(study, study._storage.create_new_trial(study._study_id))
    with pytest.raises(ValueError):
        trial.suggest_int("x", 10, 20)
Exemple #6
0
    def objective(trial: Trial) -> float:

        a = trial.suggest_int("a", 0, 100)
        b = trial.suggest_uniform("b", -0.1, 0.1)
        c = trial.suggest_categorical("c", ("x", "y"))
        d = trial.suggest_discrete_uniform("d", -5, 5, 1)
        e = trial.suggest_loguniform("e", 0.0001, 1)

        if c == "x":
            return a * d
        else:
            return b * e
Exemple #7
0
    def objective(trial: Trial) -> float:

        a = trial.suggest_int("a", 0, 100)
        b = trial.suggest_float("b", -0.1, 0.1)
        c = trial.suggest_categorical("c", ("x", "y", None, 1, 2.0))
        d = trial.suggest_float("d", -5, 5, step=1)
        e = trial.suggest_float("e", 0.0001, 1, log=True)

        if c == "x":
            return a * d
        else:
            return b * e
Exemple #8
0
    def objective(trial: Trial) -> float:

        # Predefined parameters are sampled by `sample_relative()` method.
        assert trial.suggest_uniform("a", 0, 5) == 3.2
        assert trial.suggest_categorical("b", ["foo", "bar", "baz"]) == "baz"

        # Other parameters are sampled by `sample_independent()` method.
        assert trial.suggest_int("c", 20, 50) == unknown_param_value
        assert trial.suggest_loguniform("d", 1, 100) == unknown_param_value
        assert trial.suggest_uniform("e", 20, 40) == unknown_param_value

        return 0.0
Exemple #9
0
    def sample_params_values(self, trial: Trial, suggested_params: Dict,
                             estimated_n_trials: int) -> Dict:
        """Sample hyperparameters from suggested.

        Args:
            trial: Optuna trial object.
            suggested_params: Dict with parameters.
            estimated_n_trials: Maximum number of hyperparameter estimation.

        Returns:
            Dict with sampled hyperparameters.

        """

        trial_values = copy(suggested_params)

        try:
            nan_rate = getattr(self, '_nan_rate')
        except AttributeError:
            nan_rate = 0

        trial_values['max_depth'] = trial.suggest_int(name='max_depth',
                                                      low=3,
                                                      high=7)

        if nan_rate > 0:
            trial_values['nan_mode'] = trial.suggest_categorical(
                name='nan_mode', choices=['Max', 'Min'])

        if estimated_n_trials > 20:
            trial_values['l2_leaf_reg'] = trial.suggest_loguniform(
                name='l2_leaf_reg',
                low=1e-8,
                high=10.0,
            )

            # trial_values['bagging_temperature'] = trial.suggest_loguniform(
            #     name='bagging_temperature',
            #     low=0.01,
            #     high=10.0,
            # )

        if estimated_n_trials > 50:
            trial_values['min_data_in_leaf'] = trial.suggest_int(
                name='min_data_in_leaf', low=1, high=20)

            # the only case when used this parameter is when categorical columns more than 0
            if len(self._le_cat_features) > 0:
                trial_values['one_hot_max_size'] = trial.suggest_int(
                    name='one_hot_max_size', low=3, high=10)

        return trial_values
Exemple #10
0
 def objective(trial: Trial) -> Tuple[float, float]:
     p0 = trial.suggest_float("p0", -10, 10)
     p1 = trial.suggest_float("p1", 3, 5)
     p2 = trial.suggest_float("p2", 0.00001, 0.1, log=True)
     p3 = trial.suggest_float("p3", 100, 200, step=5)
     p4 = trial.suggest_int("p4", -20, -15)
     p5 = cast(int, trial.suggest_categorical("p5", [7, 1, 100]))
     p6 = trial.suggest_float("p6", -10, 10, step=1.0)
     p7 = trial.suggest_int("p7", 1, 7, log=True)
     return (
         p0 + p1 + p2,
         p3 + p4 + p5 + p6 + p7,
     )
def define_hyperparameters(trial: Trial) -> Pipeline:
    ngram_range = trial.suggest_categorical("vectorizer__ngram_range",
                                            ["11", "12"])
    vectorizer = TfidfVectorizer(
        stop_words="english",
        min_df=2,
        ngram_range=string_to_tuple(ngram_range),
    )

    clf = trial.suggest_categorical(
        "clf", ["SVC", "RandomForestClassifier", "MultinomialNB"])
    if clf == "SVC":
        C = trial.suggest_uniform("clf__C", 0.1, 0.2)
        classifier = SVC(C=C)
    elif clf == "RandomForestClassifier":
        max_depth = trial.suggest_int("clf__max_depth", 2, 4)
        classifier = RandomForestClassifier(max_depth=max_depth)
    else:
        alpha = trial.suggest_loguniform("clf__alpha", 1e-2, 1e-1)
        classifier = MultinomialNB(alpha=alpha)

    return Pipeline([("vectorizer", vectorizer), ("clf", classifier)])
Exemple #12
0
    def objective(trial: Trial) -> float:
        x1 = trial.suggest_uniform("x1", 0.1, 3)
        x2 = trial.suggest_loguniform("x2", 0.1, 3)
        x3 = trial.suggest_discrete_uniform("x3", 0, 3, 1)
        x4 = trial.suggest_int("x4", -3, 3)
        x5 = trial.suggest_categorical("x5", [1.0, 1.1, 1.2])
        if trial.number % 2 == 0:
            # Conditional parameters are ignored unless `params` is specified and is not `None`.
            x6 = trial.suggest_uniform("x6", 0.1, 3)

        assert isinstance(x5, float)
        value = x1**4 + x2 + x3 - x4**2 - x5
        if trial.number % 2 == 0:
            value += x6
        return value
Exemple #13
0
    def objective(trial: Trial) -> float:
        x1 = trial.suggest_float("x1", 0.1, 3)
        x2 = trial.suggest_float("x2", 0.1, 3, log=True)
        x3 = trial.suggest_float("x3", 0, 3, step=1)
        x4 = trial.suggest_int("x4", -3, 3)
        x5 = trial.suggest_int("x5", 1, 5, log=True)
        x6 = trial.suggest_categorical("x6", [1.0, 1.1, 1.2])
        if trial.number % 2 == 0:
            # Conditional parameters are ignored unless `params` is specified and is not `None`.
            x7 = trial.suggest_float("x7", 0.1, 3)

        assert isinstance(x6, float)
        value = x1**4 + x2 + x3 - x4**2 - x5 + x6
        if trial.number % 2 == 0:
            value += x7
        return value
Exemple #14
0
    def objective(trial: Trial) -> float:

        x = trial.suggest_int("x", 5, 5)
        z = trial.suggest_categorical("z", [None])
        if trial.number == 0:
            return x * int(z is None)
        elif trial.number == 1:
            trial.report(1, 4)
            trial.report(2, 7)
            raise TrialPruned()
        elif trial.number == 2:
            trial.report(float("nan"), 3)
            raise TrialPruned()
        elif trial.number == 3:
            raise TrialPruned()
        else:
            raise RuntimeError()
Exemple #15
0
 def objective(trial: Trial) -> float:
     x = trial.suggest_categorical("x", [True, False])
     if x:
         return trial.suggest_float("y", 0, 1)
     return trial.suggest_float("z", 0, 1)
 def objective(trial: Trial) -> float:
     x = trial.suggest_int("x", -1, 1)
     y = trial.suggest_categorical("y", [-1, 0, 1])
     y = cast(int, y)
     return x**2 + y**2
Exemple #17
0
 def objective(trial: Trial) -> float:
     x = trial.suggest_categorical("x", ["a", "b"])
     if x == "a":
         return trial.suggest_float("y", 0, 1)
     else:
         return trial.suggest_float("z", 0, 1)
Exemple #18
0
 def __call__(self, name: str, trial: Trial):
     return trial.suggest_categorical(name, self.choices)