Skip to content

Commit

Permalink
[tune] hyperopt searcher to support tune.choice([[1,2],[3,4]]). (#24181)
Browse files Browse the repository at this point in the history
Have Hyperopt Searcher to support tune.choice([1,2],[3,4]) type search space.
  • Loading branch information
xwjiang2010 authored Apr 28, 2022
1 parent 629424f commit 576addf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python/ray/tune/suggest/hyperopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,8 @@ def resolve_value(par: str, domain: Domain) -> Any:
dict(enumerate(category)), prefix=f"{par}/{i}"
)
if isinstance(category, list)
and len(category) > 0
and isinstance(category[0], Domain)
else resolve_value(f"{par}/{i}", category)
if isinstance(category, Domain)
else category
Expand Down
32 changes: 32 additions & 0 deletions python/ray/tune/tests/test_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,38 @@ def testConvertHyperOpt(self):
self.assertTrue(5 <= config["a"] <= 6)
self.assertTrue(8 <= config["b"] <= 9)

def testConvertHyperOptChooseFromListOfList(self):
from ray.tune.suggest.hyperopt import HyperOptSearch
from hyperopt import hp

config = {
"a": tune.choice([[1, 2], [3, 4]]),
}
converted_config = HyperOptSearch.convert_search_space(config)
hyperopt_config = {
"a": hp.choice("a", [[1, 2], [3, 4]]),
}

searcher1 = HyperOptSearch(
space=converted_config, random_state_seed=1234, metric="a", mode="max"
)
searcher2 = HyperOptSearch(
space=hyperopt_config, random_state_seed=1234, metric="a", mode="max"
)

config1 = searcher1.suggest("0")
config2 = searcher2.suggest("0")

self.assertEqual(config1, config2)

# Hyperopt natively converts list to tuple.
# Try out the following script:
# ```
# a = HyperOptSearch.convert_search_space({"a": tune.choice([[1,2], [3,4]])})
# print(hyperopt.pyll.stochastic.sample(a))
# ```
self.assertTrue(config1.get("a") in [(1, 2), (3, 4)])

def testConvertHyperOptNested(self):
from ray.tune.suggest.hyperopt import HyperOptSearch

Expand Down

0 comments on commit 576addf

Please sign in to comment.