question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optimize doesn't work with evaluation function that returns value of some NumPy types

See original GitHub issue

I noticed that when the evaluation_function returns value of some NumPy type(I was able to reproduce with np.int64, np.int32, np.float32), optimize method crashes with the following exception:

File “/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/ax/service/managed_loop.py”, line 206, in optimize loop.full_run() File “/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/ax/service/managed_loop.py”, line 150, in full_run self.run_trial() File “/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/ax/service/managed_loop.py”, line 141, in run_trial trial.fetch_data() File “/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/ax/core/base_trial.py”, line 259, in fetch_data trial_index=self.index, metrics=metrics, **kwargs File “/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/ax/core/simple_experiment.py”, line 205, in _fetch_trial_data return self.eval_trial(self.trials[trial_index]) File “/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/ax/core/simple_experiment.py”, line 119, in eval_trial not_none(trial.arm).parameters, None File “/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/ax/core/simple_experiment.py”, line 192, in evaluation_function_outer "Evaluation function returned an invalid type. The function must " Exception: Evaluation function returned an invalid type. The function must either return a dictionary of metric names to mean, sem tuples or a single mean, sem tuple, or a single mean.

My environment:

numpy==1.16.1
ax-platform==0.1.2

Script to reproduce:

import numpy as np
from ax import optimize
from ax.utils.measurement.synthetic_functions import branin

best_parameters, values, experiment, model = optimize(
    parameters=[
        {
            "name": "x1",
            "type": "range",
            "bounds": [-5.0, 10.0],
        },
        {
            "name": "x2",
            "type": "range",
            "bounds": [0.0, 10.0],
        },
    ],
    evaluation_function=lambda p: np.float32(branin(p["x1"], p["x2"])),
    minimize=True,
)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Iryneicommented, May 21, 2019

@lena-kashtelyan, thanks for your explanation. I was expecting the evaluation function to work with numpy types, so for me, that was a surprising behavior. No, It’s not hard to convert them for now, but thanks for the suggestion.

2reactions
lena-kashtelyancommented, May 21, 2019

@Irynei, this is intentional behavior –– the evaluation function is strictly typed, and it’s return type is a union of a float, a tuple of two floats, and a dictionary of a string to such tuples, as documented in SimpleExperiment. We use python builtins for primitive types and convert from numpy primitives where necessary. However, you’re right that for the evaluation function, we should allow numpy types –– will add that support shortly.

Is it much trouble to convert back from numpy to python primitive in the evaluation function, for now? You could use our utility ax.utils.common.typeutils.numpy_type_to_python_type, if you’d like.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optimization (scipy.optimize) — SciPy v1.9.3 Manual
It requires only function evaluations and is a good choice for simple minimization problems. However, because it does not use any gradient evaluations, ......
Read more >
2.7. Mathematical optimization: finding minima of functions
Mathematical optimization deals with the problem of finding numerically minimums (or maximums or zeros) of a function. In this context, the function is ......
Read more >
Numerical Methods using Python (scipy)
The return value[6] of fsolve is a numpy array of length n for a root finding problem with n variables. In the example...
Read more >
“Vectorized” Operations: Optimized Computations on NumPy ...
We will now take some time to survey the various types of vectorized mathematical functions that NumPy supplies, and how these mathematical operations, ......
Read more >
NumPy 1.20.0 Release Notes
Much work has been done in introducing universal functions that will ease use of ... In particularly, if dtype="S" is not provided any...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found