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

Error: A list of 'ChoiceParameter' is not iterable #2224

Open
Runyu-Zhang opened this issue Feb 27, 2024 · 4 comments
Open

Error: A list of 'ChoiceParameter' is not iterable #2224

Runyu-Zhang opened this issue Feb 27, 2024 · 4 comments

Comments

@Runyu-Zhang
Copy link

While creating a list of choice parameters using dictionary:
parameters_list = [dict(name = parameter,
type = "choice",
values = [x,...]
) for parameter in all_parameters
]
it has warning UserWarning: sort_values is not specified for ChoiceParameter "xxx". Defaulting to True for parameters of ParameterType FLOAT. To override this behavior (or avoid this warning), specify sort_values during ChoiceParameter construction.

So if I want to avoid those warnings by creating my parameters using ChoiceParameter:

from ax import ChoiceParameter, ParameterType

[ChoiceParameter(name = parameter,
parameter_type = ParameterType.FLOAT,
values =[x,...],
is_ordered = True,
sort_values = False) for parameter in all_parameters]
it becomes an error
experiment = self.make_experiment(
File "/ax/lib/python3.9/site-packages/ax/service/utils/instantiation.py", line 908, in make_experiment
search_space=cls.make_search_space(parameters, parameter_constraints),
File "/ax/lib/python3.9/site-packages/ax/service/utils/instantiation.py", line 687, in make_search_space
typed_parameters = [cls.parameter_from_json(p) for p in parameters]
File "/ax/lib/python3.9/site-packages/ax/service/utils/instantiation.py", line 687, in
typed_parameters = [cls.parameter_from_json(p) for p in parameters]
File "/ax/lib/python3.9/site-packages/ax/service/utils/instantiation.py", line 280, in parameter_from_json
if "parameter_type" in representation:
TypeError: argument of type 'ChoiceParameter' is not iterable

Although it's not critical, still better to have a way to pass "sort_values" through dict(), or having ChoiceParameter() iterable.

@mgrange1998
Copy link
Contributor

Yes, looking through the ChoiceParameter construction code it looks like there isn't yet a way to pass "sort_values" with a dict, thanks for bringing this to our attention.

If I am understanding correctly, you are passing a "ChoiceParameter" object into "parameter_from_json", and line 280 is erroring because ChoiceParameter is not an iterable object. This is expected, as parameter_from_json is meant to convert JSON objects to Parameter objects.
Since you are now constructing the ChoiceParameter you do not need to pass it into parameter_from_json- you can instead use the ChoiceParameter directly. Let me know if this helps!

@Runyu-Zhang
Copy link
Author

Thanks for the tips mgrange1998. But I guess my issue was that when I construct a list of "ChoiceParameter" using for loop, like:
list_choice_parameters = []
for parameter in all_parameters:
list_choice_parameters.append(ChoiceParameter(name = parameter,
parameter_type = ParameterType.FLOAT,
values =[x1, x2, ...],
is_ordered = True,
sort_values = False)
)
and then pass list_choice_parameters to ax_client.create_experiment(name = name,
parameters = list_choice_parameters,
objectives = objectives
)
this is throwing an error TypeError: argument of type 'ChoiceParameter' is not iterable

Instead, if I append dictionaries to "list_parameters=[]", and then pass to ax_client.create_experiment(), it is fine, but then the warning comes: sort_values is not specified for ChoiceParameter "xxx". Defaulting to True for parameters of ParameterType FLOAT. To override this behavior (or avoid this warning), specify sort_values during ChoiceParameter construction.
Hopefully it makes sense.

@mgrange1998
Copy link
Contributor

mgrange1998 commented Feb 28, 2024

Hello again- that makes sense to me. I have a pull request up which will fix your issue by allowing you to specify "sort_values" in the input dictionary #2231
Once this lands, you'll be able to specify "sort_values" explicitly to stop the UserWarning from appearing. Let me know if you have any other questions!

@Runyu-Zhang
Copy link
Author

Nice. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants