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

TypeError: HiPlot generation failing when optimizing TransitionChoice #1466

Open
akorb opened this issue Aug 19, 2022 · 0 comments
Open

TypeError: HiPlot generation failing when optimizing TransitionChoice #1466

akorb opened this issue Aug 19, 2022 · 0 comments

Comments

@akorb
Copy link

akorb commented Aug 19, 2022

Steps to reproduce

Execute the script below.

Observed Results

  • TypeError: Object of type int64 is not JSON serializable

Expected Results

  • plot.html which shows the HiPlot of the optimization.

Relevant Code

from hiplot import Experiment
import nevergrad as ng
from nevergrad.optimization.callbacks import ParametersLogger
from math import cos


parametrization = ng.p.Instrumentation(
    ng.p.TransitionChoice(10)
)

optimizer = ng.optimizers.NGOpt(parametrization=parametrization, budget=10)

logger = ParametersLogger("log.json")
optimizer.register_callback("tell",  logger)

recommendation = optimizer.minimize(cos)
print("Recommendation: ", recommendation.value)

exp: Experiment = logger.to_hiplot_experiment()
exp.to_html("plot.html")

Notes

The reason that it crashes is that exp.datapoints[0].values['0.indices#0'] is a np.int64 instead of a simple native int.

TransitionChoice has the layer _datalayers.Int. And its _layered_get_value function returns an np.int64.

def _layered_get_value(self) -> np.ndarray:
if self._cache is not None:
return self._cache
bounds = self._layers[0].bounds # type: ignore
out = super()._layered_get_value()
if not self.deterministic:
out += self.random_state.rand(*out.shape) - 0.5
out = np.round(out).astype(int)
# make sure rounding does not reach beyond the bounds
eps = 1e-12
if bounds[0] is not None:
out = np.maximum(_to_int(bounds[0] + 0.5 - eps), out)
if bounds[1] is not None:
out = np.minimum(_to_int(bounds[1] - 0.5 + eps), out)
# return out
self._cache = out
return self._cache

Not sure if this is the source of the problem, I'm not familiar with the guts of nevergrad

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

1 participant