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.

"ValueError: array must not contain infs or NaNs" happens after self.suggest().

See original GitHub issue

I met an error when executing the code shown in the Basic tour of the Bayesian Optimization package part. My code is the same as the demo, except the parameters for maximize() method.

def black_box_function(x, y):
    return -x ** 2 - (y - 1) ** 2 + 1

from bayes_opt import BayesianOptimization
pbounds = {'x': (2, 4), 'y': (-3, 3)}

optimizer = BayesianOptimization(
    f=black_box_function,
    pbounds=pbounds,
    random_state=1,
)

optimizer.maximize(
    init_points=1,
    n_iter=5,
)
print(optimizer.max)

As you can see, I set the parameters as 1 and 5 respectively, and I got the output as well as the error like this:

|   iter    |  target   |     x     |     y     |
-------------------------------------------------
|  1        | -7.135    |  2.834    |  1.322    |
Traceback (most recent call last):
  File "/home/ljy/anaconda3/envs/tt/lib/python3.6/site-packages/bayes_opt/bayesian_optimization.py", line 169, in maximize
    x_probe = next(self._queue)
  File "/home/ljy/anaconda3/envs/tt/lib/python3.6/site-packages/bayes_opt/bayesian_optimization.py", line 26, in __next__
    raise StopIteration("Queue is empty, no more objects to retrieve.")
StopIteration: Queue is empty, no more objects to retrieve.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "tmp.py", line 91, in <module>
    n_iter=5,
  File "/home/ljy/anaconda3/envs/tt/lib/python3.6/site-packages/bayes_opt/bayesian_optimization.py", line 171, in maximize
    x_probe = self.suggest(util)
  File "/home/ljy/anaconda3/envs/tt/lib/python3.6/site-packages/bayes_opt/bayesian_optimization.py", line 124, in suggest
    self._gp.fit(self._space.params, self._space.target)
  File "/home/ljy/anaconda3/envs/tt/lib/python3.6/site-packages/sklearn/gaussian_process/_gpr.py", line 234, in fit
    self.kernel_.bounds))]
  File "/home/ljy/anaconda3/envs/tt/lib/python3.6/site-packages/sklearn/gaussian_process/_gpr.py", line 503, in _constrained_optimization
    bounds=bounds)
  File "/home/ljy/anaconda3/envs/tt/lib/python3.6/site-packages/scipy/optimize/_minimize.py", line 600, in minimize
    callback=callback, **options)
  File "/home/ljy/anaconda3/envs/tt/lib/python3.6/site-packages/scipy/optimize/lbfgsb.py", line 335, in _minimize_lbfgsb
    f, g = func_and_grad(x)
  File "/home/ljy/anaconda3/envs/tt/lib/python3.6/site-packages/scipy/optimize/lbfgsb.py", line 285, in func_and_grad
    f = fun(x, *args)
  File "/home/ljy/anaconda3/envs/tt/lib/python3.6/site-packages/scipy/optimize/optimize.py", line 326, in function_wrapper
    return function(*(wrapper_args + args))
  File "/home/ljy/anaconda3/envs/tt/lib/python3.6/site-packages/scipy/optimize/optimize.py", line 64, in __call__
    fg = self.fun(x, *args)
  File "/home/ljy/anaconda3/envs/tt/lib/python3.6/site-packages/sklearn/gaussian_process/_gpr.py", line 225, in obj_func
    theta, eval_gradient=True, clone_kernel=False)
  File "/home/ljy/anaconda3/envs/tt/lib/python3.6/site-packages/sklearn/gaussian_process/_gpr.py", line 476, in log_marginal_likelihood
    alpha = cho_solve((L, True), y_train)  # Line 3
  File "/home/ljy/anaconda3/envs/tt/lib/python3.6/site-packages/scipy/linalg/decomp_cholesky.py", line 196, in cho_solve
    b1 = asarray_chkfinite(b)
  File "/home/ljy/anaconda3/envs/tt/lib/python3.6/site-packages/numpy/lib/function_base.py", line 496, in asarray_chkfinite
    "array must not contain infs or NaNs")
ValueError: array must not contain infs or NaNs

It looks something wrong with suggest() fucntion.

And of course, if I set the two hyper-parameters as 2 and 3 respectively, it works well and does optimize to a good result, the output is like this then.

|   iter    |  target   |     x     |     y     |
-------------------------------------------------
|  1        | -7.135    |  2.834    |  1.322    |
|  2        | -7.78     |  2.0      | -1.186    |
|  3        | -7.11     |  2.218    | -0.7867   |
|  4        | -12.4     |  3.66     |  0.9608   |
|  5        | -6.999    |  2.23     | -0.7392   |
=================================================

So I’m not sure why I can’t set init_points to be smaller, or there are some principles about these two parameters that I haven’t understood.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
rkerncommented, Sep 28, 2020

As a stopgap, you can downgrade scikit-learn to a 0.22.x release.

0reactions
samuelstroscheincommented, Apr 17, 2021

I encountered the same error following the example notebook provided here https://github.com/fmfn/BayesianOptimization/blob/master/examples/advanced-tour.ipynb. Cell 8 throws the ValueError.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"ValueError: array must not contain infs or NaNs" - Stack ...
I am aware of scikit-learn GitHub issue #2089, but since I use scikit-learn 0.16.1 (with Python 2.7.10 x64) this problem should be solved...
Read more >
Interpolate_bads: "ValueError: array must not contain infs or ...
When I drop bad channels and then reload the data to save the interpolated data, I get “ValueError: array must not contain infs...
Read more >
Let's Start | Kaggle
Explore and run machine learning code with Kaggle Notebooks | Using data from Predict Click through rate (CTR) for a website.
Read more >
Getting 'ValueError: array must not contain infs or NaNs' even ...
nan_to_num() on the sentiment scores and then input them into the pearsonr function. However, even after it is still creating the error. When...
Read more >
Plugin error from diversity: array must not contain infs or NaNs
Hi there, I've been trying to run diversity analyses on a dataset but keep geeting the following error: Plugin error from diversity: array...
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