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

ChoiceParameter with list of ints: TypeError: Object of type int32 is not JSON serializable #170

Closed
BCJuan opened this issue Sep 20, 2019 · 6 comments

Comments

@BCJuan
Copy link

BCJuan commented Sep 20, 2019

Hi,

First of all thank you for this awesome project.

Regarding the error: when trying to input a set of integers to a ChoiceParameter with parameter_type=ParameterType.INT the following error appears:

TypeError: Object of type int32 is not JSON serializable

The problem seems to occur at file arm.py and at function md5hash where the parameters are dumped to a JSON parameters_str = json.dumps(parameters, sort_keys=True)

To reproduce:

    range_x = ChoiceParameter(name='x', values=[1,5,10],
                              parameter_type=ParameterType.INT)
    space = SearchSpace(parameters=[range_x])
    experiment = Experiment(name="experiment_one_cell",
                            search_space=space)
    sobol = Models.SOBOL(search_space=experiment.search_space)
    generator_run = sobol.gen(100)

I have tried to make the list a set, but the problem seems to be in the parameter type definition. When changing to ParameterType.STRING and casting values to strings the problem does not exist.

I know this is not an important error, but I state it here just so it is somewhere defined.

Thank you .

UPDATE: indeed this is strange since there is already a conversion from numpy to python in function md5hash:

        for k, v in parameters.items():
            parameters[k] = numpy_type_to_python_type(v)

But it does not seem to work.

@ldworkin
Copy link
Contributor

Hi @BCJuan ! Thanks for reporting this issue. I'm having trouble reproducing though. This is what I ran:

from ax import *

    range_x = ChoiceParameter(name='x', values=[1,5,10],
                              parameter_type=ParameterType.INT)
    space = SearchSpace(parameters=[range_x])
    experiment = Experiment(name="experiment_one_cell",
                            search_space=space)
    sobol = Models.SOBOL(search_space=experiment.search_space)
    generator_run = sobol.gen(100)

However, that completed successfully. Am I missing something?

@BCJuan
Copy link
Author

BCJuan commented Sep 20, 2019

Hi,

I still havethe problem if I use your code. It is my fault, I have checked the datatype and mine are numpy.int32

The function numpy_type_to_python_type in typeutils.py only catches np.int64 and numpy.float64.

I suppose is mandatory to use 64 bit data types.

Apologies for the time spent.

Best

@ldworkin
Copy link
Contributor

Oh interesting! We can fix it to work with np.int32 and np.float32. Thanks for catching this!

@ldworkin
Copy link
Contributor

Actually I believe this was already fixed! What version of Ax are you using?

see:
7904aa2#diff-0befb5b9d1a4f64449eba6e50b2d2b0d

@BCJuan BCJuan closed this as completed Sep 20, 2019
@BCJuan
Copy link
Author

BCJuan commented Sep 20, 2019

My fault I am using 0.1.3. Closing the issue. Thanks!

@ldworkin
Copy link
Contributor

Great!

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