Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tune] OptunaSearch: check compatibility of search space with evaluated_rewards #18625

Merged
merged 8 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion python/ray/tune/suggest/optuna.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ class OptunaSearch(Searcher):
needing to re-compute the trial. Must be the same length as
points_to_evaluate.

..warning::
When using ``evaluated_rewards``, the search space ``space``
must be provided as a :class:`dict` with parameter names as
keys and ``optuna.distributions`` instances as values. The
define-by-run search space definition is not yet supported with
this functionality.

Tune automatically converts search spaces to Optuna's format:

.. code-block:: python
Expand All @@ -139,7 +146,7 @@ class OptunaSearch(Searcher):
from ray.tune.suggest.optuna import OptunaSearch
import optuna

config = {
space = {
"a": optuna.distributions.UniformDistribution(6, 8),
"b": optuna.distributions.LogUniformDistribution(1e-4, 1e-2),
}
Expand All @@ -166,6 +173,49 @@ def define_search_space(trial: optuna.Trial):

tune.run(trainable, search_alg=optuna_search)

You can pass configs that will be evaluated first using
Yard1 marked this conversation as resolved.
Show resolved Hide resolved
``points_to_evaluate``:

.. code-block:: python

from ray.tune.suggest.optuna import OptunaSearch
import optuna

space = {
"a": optuna.distributions.UniformDistribution(6, 8),
"b": optuna.distributions.LogUniformDistribution(1e-4, 1e-2),
}

optuna_search = OptunaSearch(
space,
points_to_evaluate=[{"a": 6.5, "b": 5e-4}, {"a": 7.5, "b": 1e-3}]
metric="loss",
mode="min")

tune.run(trainable, search_alg=optuna_search)

Avoid re-running evaluated trials by passing the rewards together with
`points_to_evaluate`:

.. code-block:: python

from ray.tune.suggest.optuna import OptunaSearch
import optuna

space = {
"a": optuna.distributions.UniformDistribution(6, 8),
"b": optuna.distributions.LogUniformDistribution(1e-4, 1e-2),
}

optuna_search = OptunaSearch(
space,
points_to_evaluate=[{"a": 6.5, "b": 5e-4}, {"a": 7.5, "b": 1e-3}]
evaluated_rewards=[0.89, 0.42]
metric="loss",
mode="min")

tune.run(trainable, search_alg=optuna_search)

.. versionadded:: 0.8.8

"""
Expand Down Expand Up @@ -367,6 +417,12 @@ def add_evaluated_point(self,
cls=self.__class__.__name__,
metric=self._metric,
mode=self._mode))
if callable(self._space):
raise TypeError(
"Define-by-run function passed in `space` argument is not "
"yet supported when using `evaluated_rewards`. Please provide "
"an `OptunaDistribution` dict or pass a Ray Tune "
"search space to `tune.run()`.")

ot_trial_state = OptunaTrialState.COMPLETE
if error:
Expand Down
13 changes: 13 additions & 0 deletions python/ray/tune/tests/test_searchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,19 @@ def testOptuna(self):
self.assertTrue(
searcher._ot_study.trials[-1].state == TrialState.PRUNED)

def dbr_space(trial):
return {
self.param_name: trial.suggest_float(self.param_name, 0.0, 5.0)
}

dbr_searcher = OptunaSearch(
space=dbr_space,
metric="metric",
mode="max",
)
with self.assertRaises(TypeError):
dbr_searcher.add_evaluated_point(point, 1.0)

def testHEBO(self):
from ray.tune.suggest.hebo import HEBOSearch

Expand Down