3

I am using bayesian optimization to optimize the hyper parameters of a NN using python library "bayes_opt", but an error has raised

~\Miniconda3\envs\tensorflow\lib\site-packages\bayes_opt\bayesian_optimization.py in next(self) 25 if self.empty: ---> 26 raise StopIteration("Queue is empty, no more objects to retrieve.") 27 obj = self._queue[0]

StopIteration: Queue is empty, no more objects to retrieve.

My code is as following:

from bayes_opt import BayesianOptimization
import time

# Supress NaN warnings, see: https://stackoverflow.com/questions/34955158/what-might-be-the-cause-of-invalid-value-encountered-in-less-equal-in-numpy
import warnings
warnings.filterwarnings("ignore",category =RuntimeWarning)


# Bounded region of parameter space
pbounds = {'dropout': (0.0, 0.499),
           'lr': (0.0, 0.1),
           'neuronPct': (0.01, 1),
           'neuronShrink': (0.01, 1)
          } 

optimizer = BayesianOptimization(
    f=evaluate_network,
    pbounds=pbounds,
    verbose=2,  # verbose = 1 prints only when a maximum is observed, verbose = 0 is silent
    random_state=1,
)

start_time = time.time()
optimizer.maximize(init_points=10, n_iter=100,)
time_took = time.time() - start_time

print(optimizer.max)

and the output is as following:

|   iter    |  target   |  dropout  |    lr     | neuronPct | neuron... |
-------------------------------------------------------------------------
|  1        | -0.5718   |  0.2081   |  0.07203  |  0.01011  |  0.3093   |
|  2        | -0.5906   |  0.07323  |  0.009234 |  0.1944   |  0.3521   |
|  3        | -2.475    |  0.198    |  0.05388  |  0.425    |  0.6884   |
|  4        | -0.5708   |  0.102    |  0.08781  |  0.03711  |  0.6738   |
|  5        | -0.5749   |  0.2082   |  0.05587  |  0.149    |  0.2061   |
|  6        | -2.487    |  0.3996   |  0.09683  |  0.3203   |  0.6954   |
|  7        | -0.5669   |  0.4373   |  0.08946  |  0.09419  |  0.04866  |
|  8        | -0.5701   |  0.08475  |  0.08781  |  0.1074   |  0.4269   |
|  9        | -0.607    |  0.478    |  0.05332  |  0.695    |  0.3224   |
|  10       | -0.581    |  0.3426   |  0.08346  |  0.02811  |  0.7526   |
|  11       | -0.7295   |  0.1606   |  0.0      |  1.0      |  0.01     |
|  12       | -0.7131   |  0.499    |  0.0      |  0.607    |  0.01     |
|  13       | -0.7044   |  0.0      |  0.0      |  0.01     |  0.01     |
|  14       | -0.5872   |  0.04935  |  0.01347  |  0.4728   |  0.4246   |
|  15       | -0.5665   |  0.0      |  0.1      |  0.5566   |  0.01     |
|  16       | -0.9658   |  0.0      |  0.0      |  1.0      |  0.4938   |
|  17       | -0.9074   |  0.0      |  0.0      |  0.01     |  1.0      |
|  18       | -2.464    |  0.499    |  0.1      |  1.0      |  0.1975   |
|  19       | -0.9505   |  0.499    |  0.0      |  1.0      |  1.0      |
|  20       |  nan      |  0.0      |  0.1      |  1.0      |  1.0      |  

NOTE the "nan" at the last row of the output table
Thanks in advance.

Programmer
  • 73
  • 1
  • 9
  • It is related to your results. As you could see, your target in the row is np.nan, and your error says that "Queue is empty, no more objects to retrieve", I guess the function runs out of your data points. But nothing is sure, I suppose you read the source codes of this package. – Memphis Meng Oct 22 '20 at 18:35

5 Answers5

2

Same thing just happened to me. I see that this is an old post, so no hope for a solution soon.

I managed to narrow it down to the following: if the pbounds interval is too small, this happens. I made the pbound interval slightly bigger and it stopped happening.

This worked for me, but I hope sometime in the future somebody fixes this. The error seems to be in sklearn.

Luis
  • 21
  • 2
0

I recently had a similar issue using bayes_opt with the facebook prophet package.

To resolve the "StopIteration: Queue is empty ..." error I had to modify my pbounds parameter from:

pbounds = {'daily_prior': (2, -2), 'yearly_prior': (2, -2)}

to

pbounds = {'daily_prior': (-2, 2), 'yearly_prior': (-2, 2)}

I've check most, if not all, of the bayes_opt examples and so far they all have this {'param': (min, max)} form.

I've added this answer to this GitHub issue on the bayes_opt repository.

This will not fix the error in the original question but I hope it helps someone.


ps. Here is the full error message I was getting:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/bayes_opt/bayesian_optimization.py", line 181, in maximize
    x_probe = next(self._queue)
  File "/usr/local/lib/python3.8/site-packages/bayes_opt/bayesian_optimization.py", line 25, 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 "bayes_opt_prophet_bounds_test.py", line 43, in <module>
    optimizer.maximize(
  File "/usr/local/lib/python3.8/site-packages/bayes_opt/bayesian_optimization.py", line 184, in maximize
    x_probe = self.suggest(util)
  File "/usr/local/lib/python3.8/site-packages/bayes_opt/bayesian_optimization.py", line 133, in suggest
    suggestion = acq_max(
  File "/usr/local/lib/python3.8/site-packages/bayes_opt/util.py", line 58, in acq_max
    res = minimize(lambda x: -ac(x.reshape(1, -1), gp=gp, y_max=y_max),
  File "/usr/local/lib/python3.8/site-packages/scipy/optimize/_minimize.py", line 617, in minimize
    return _minimize_lbfgsb(fun, x0, args, jac, bounds,
  File "/usr/local/lib/python3.8/site-packages/scipy/optimize/lbfgsb.py", line 294, in _minimize_lbfgsb
    raise ValueError("LBFGSB - one of the lower bounds is greater than an upper bound.")
ValueError: LBFGSB - one of the lower bounds is greater than an upper bound.
makeyourownmaker
  • 1,558
  • 2
  • 13
  • 33
0

The init_points argument seemed to be the problem in my case, I started with 10 and went up to 45 to fix it:

optimizer.maximize(init_points=45, n_iter=20)
CreepyRaccoon
  • 826
  • 1
  • 9
  • 19
0

A few days aog, I had this exception.

I found a cause that x range of pbounds={'x':(0, 1)} is too narrow.

so I solved that firstly changed pbounds={'x':(0, 100000)} and then added x[1]= x[1]/100000 in black_box_function.

0

I solved the problem by increasing the init_points argument.

CreepyRaccoon
  • 826
  • 1
  • 9
  • 19