Ax icon indicating copy to clipboard operation
Ax copied to clipboard

Error: A list of 'ChoiceParameter' is not iterable

Open Runyu-Zhang opened this issue 1 year ago • 4 comments

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.

Runyu-Zhang avatar Feb 27 '24 20:02 Runyu-Zhang

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!

mgrange1998 avatar Feb 27 '24 22:02 mgrange1998

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.

Runyu-Zhang avatar Feb 28 '24 17:02 Runyu-Zhang

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 https://github.com/facebook/Ax/pull/2231?fbclid=IwAR3d4oAlxseldMHHdwDW8Jv-WxnU9n7enoG74CU13wQKxjK5ko7Q9CFTxKQ 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!

mgrange1998 avatar Feb 28 '24 19:02 mgrange1998

Nice. Thank you!

Runyu-Zhang avatar Feb 28 '24 20:02 Runyu-Zhang

Closing this out since it looks like this has been addressed. Thanks for raising this @Runyu-Zhang!

bernardbeckerman avatar Jul 10 '24 13:07 bernardbeckerman