How to use the optuna.distributions.IntUniformDistribution function in optuna

To help you get started, we’ve selected a few optuna examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github optuna / optuna / tests / integration_tests / test_skopt.py View on Github external
})
    assert optimizer._is_compatible(trial)

    # Incompatible ('p0' doesn't exist).
    trial = _create_frozen_trial({'p1': 5},
                                 {'p1': distributions.UniformDistribution(low=0, high=10)})
    assert not optimizer._is_compatible(trial)

    # Incompatible (the value of 'p0' is out of range).
    trial = _create_frozen_trial({'p0': 20},
                                 {'p0': distributions.UniformDistribution(low=0, high=100)})
    assert not optimizer._is_compatible(trial)

    # Error (different distribution class).
    trial = _create_frozen_trial({'p0': 5},
                                 {'p0': distributions.IntUniformDistribution(low=0, high=10)})
    with pytest.raises(ValueError):
        optimizer._is_compatible(trial)
github optuna / optuna / tests / test_trial.py View on Github external
distribution0 = distributions.UniformDistribution(low=0, high=100)
    assert trial0._suggest('x', distribution0) == 5.5

    # Not suggested from `relative_params` (due to unknown parameter name).
    trial1 = create_trial()
    distribution1 = distribution0
    assert trial1._suggest('w', distribution1) != 5.5

    # Not suggested from `relative_params` (due to incompatible value range).
    trial2 = create_trial()
    distribution2 = distributions.UniformDistribution(low=0, high=5)
    assert trial2._suggest('x', distribution2) != 5.5

    # Error (due to incompatible distribution class).
    trial3 = create_trial()
    distribution3 = distributions.IntUniformDistribution(low=1, high=100)
    with pytest.raises(ValueError):
        trial3._suggest('y', distribution3)

    # Error ('z' is included in `relative_params` but not in `relative_search_space`).
    trial4 = create_trial()
    distribution4 = distributions.UniformDistribution(low=0, high=10)
    with pytest.raises(ValueError):
        trial4._suggest('z', distribution4)
github optuna / optuna / optuna / testing / sampler.py View on Github external
def sample_independent(self, study, trial, param_name, param_distribution):
        # type: (Study, FrozenTrial, str, BaseDistribution) -> Any

        if isinstance(param_distribution, distributions.UniformDistribution):
            param_value = param_distribution.low  # type: Any
        elif isinstance(param_distribution, distributions.LogUniformDistribution):
            param_value = param_distribution.low
        elif isinstance(param_distribution, distributions.DiscreteUniformDistribution):
            param_value = param_distribution.low
        elif isinstance(param_distribution, distributions.IntUniformDistribution):
            param_value = param_distribution.low
        elif isinstance(param_distribution, distributions.CategoricalDistribution):
            param_value = param_distribution.choices[0]
        else:
            raise NotImplementedError

        return param_value
github optuna / optuna / tests / integration_tests / test_cma.py View on Github external
sampler = optuna.integration.CmaEsSampler(
            x0={'x': 0, 'y': 0},
            sigma0=0.1,
            cma_stds={'x': 1, 'y': 1},
            seed=1,
            cma_opts={'popsize': 5},
            independent_sampler=DeterministicRelativeSampler({}, {}))
        study = optuna.create_study(sampler=sampler)

        with patch('optuna.integration.cma._Optimizer') as mock_obj:
            mock_obj.ask.return_value = {'x': -1, 'y': -1}
            study.optimize(
                lambda t: t.suggest_int('x', -1, 1) + t.suggest_int('y', -1, 1), n_trials=2)
            assert mock_obj.mock_calls[0] == call({
                'x': IntUniformDistribution(low=-1, high=1),
                'y': IntUniformDistribution(low=-1, high=1)
            }, {
                'x': 0,
                'y': 0
            }, 0.1, {
                'x': 1,
                'y': 1
            }, {
                'popsize': 5,
                'seed': 1,
                'verbose': -2
            })
github optuna / optuna / tests / test_distributions.py View on Github external
single_distributions = [
        distributions.UniformDistribution(low=1.0, high=1.0),
        distributions.LogUniformDistribution(low=7.3, high=7.3),
        distributions.DiscreteUniformDistribution(low=2.22, high=2.22, q=0.1),
        distributions.IntUniformDistribution(low=-123, high=-123),
        distributions.CategoricalDistribution(choices=('foo', ))
    ]  # type: List[distributions.BaseDistribution]
    for distribution in single_distributions:
        assert distribution.single()

    nonsingle_distributions = [
        distributions.UniformDistribution(low=1.0, high=1.001),
        distributions.LogUniformDistribution(low=7.3, high=10),
        distributions.DiscreteUniformDistribution(low=-30, high=-20, q=2),
        distributions.IntUniformDistribution(low=-123, high=0),
        distributions.CategoricalDistribution(choices=('foo', 'bar'))
    ]  # type: List[distributions.BaseDistribution]
    for distribution in nonsingle_distributions:
        assert not distribution.single()
github optuna / optuna / optuna / integration / cma.py View on Github external
def _to_optuna_params(search_space, param_name, cma_param_value):
        # type: (Dict[str, BaseDistribution], str, float) -> Any

        dist = search_space[param_name]
        if isinstance(dist, LogUniformDistribution):
            return math.exp(cma_param_value)
        if isinstance(dist, DiscreteUniformDistribution):
            v = numpy.round(cma_param_value / dist.q) * dist.q + dist.low
            # v may slightly exceed range due to round-off errors.
            return float(min(max(v, dist.low), dist.high))
        if isinstance(dist, IntUniformDistribution):
            return int(numpy.round(cma_param_value))
        if isinstance(dist, CategoricalDistribution):
            v = int(numpy.round(cma_param_value))
            return dist.choices[v]
        return cma_param_value
github optuna / optuna / optuna / trial.py View on Github external
>>>     clf = sklearn.ensemble.RandomForestClassifier(n_estimators=n_estimators)
                >>>     ...

        Args:
            name:
                A parameter name.
            low:
                Lower endpoint of the range of suggested values. ``low`` is included in the range.
            high:
                Upper endpoint of the range of suggested values. ``high`` is included in the range.

        Returns:
            A suggested integer value.
        """

        distribution = distributions.IntUniformDistribution(low=low, high=high)
        if low == high:
            return self._set_new_param_or_get_existing(name, low, distribution)

        return int(self._suggest(name, distribution))
github optuna / optuna / optuna / integration / cma.py View on Github external
def _initialize_sigma0(search_space):
        # type: (Dict[str, BaseDistribution]) -> float

        sigma0s = []
        for name, distribution in search_space.items():
            if isinstance(distribution, UniformDistribution):
                sigma0s.append((distribution.high - distribution.low) / 6)
            elif isinstance(distribution, DiscreteUniformDistribution):
                sigma0s.append((distribution.high - distribution.low) / 6)
            elif isinstance(distribution, IntUniformDistribution):
                sigma0s.append((distribution.high - distribution.low) / 6)
            elif isinstance(distribution, LogUniformDistribution):
                log_high = math.log(distribution.high)
                log_low = math.log(distribution.low)
                sigma0s.append((log_high - log_low) / 6)
            elif isinstance(distribution, CategoricalDistribution):
                sigma0s.append((len(distribution.choices) - 1) / 6)
            else:
                raise NotImplementedError('The distribution {} is not implemented.'.format(
                    distribution))
        return min(sigma0s)
github optuna / optuna / optuna / trial.py View on Github external
def suggest_int(self, name, low, high):
        # type: (str, int, int) -> int

        return int(self._suggest(name, distributions.IntUniformDistribution(low=low, high=high)))
github optuna / optuna / optuna / trial.py View on Github external
def suggest_int(self, name, low, high):
        # type: (str, int, int) -> int

        return int(self._suggest(name, distributions.IntUniformDistribution(low=low, high=high)))