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

[Enhance] Add support for dict input in Choice #138

Merged
merged 10 commits into from
Jan 18, 2023
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
13 changes: 8 additions & 5 deletions siatune/tune/spaces/choice.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (c) SI-Analytics. All rights reserved.
from typing import Callable, Optional, Sequence
from typing import Callable, Optional, Sequence, Union

import ray.tune as tune
from ray.tune.search.sample import Domain
Expand All @@ -14,16 +14,19 @@ class Choice(BaseSpace):
"""Sample a categorical value.

Args:
categories (Sequence): The categories.
alias (Sequence, optional): A alias to be expressed.
Defaults to None.
categories (Sequence | dict): The categorical search space to choose
one. If categories is dict, keys of dict will overwrite the alias.
alias (Sequence, optional): A alias to be expressed. Defaults to None.
KKIEEK marked this conversation as resolved.
Show resolved Hide resolved
"""

sample: Callable = tune.choice

def __init__(self,
categories: Sequence,
categories: Union[Sequence, dict],
alias: Optional[Sequence] = None) -> None:
if isinstance(categories, dict):
alias, categories = zip(*[(k, v) for k, v in categories.items()])

if alias is not None:
assert isinstance(alias, Sequence)
assert len(categories) == len(alias)
Expand Down
1 change: 1 addition & 0 deletions tests/test_tune/test_spaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def is_immutable(config):
with pytest.raises(AssertionError):
choice = Choice(categories=[True, False], alias=['TF'])
choice = Choice(categories=[True, False], alias=['T', 'F'])
choice = Choice(categories=dict(T=True, F=False))
tune.run(is_immutable, config=dict(test=choice.space))


Expand Down