bayesian-optimization / BayesianOptimization

A Python implementation of global optimization with gaussian processes.
https://bayesian-optimization.github.io/BayesianOptimization/index.html
MIT License
7.88k stars 1.54k forks source link

TypeError: 'NoneType' object is not subscriptable self._space.max() #502

Closed james-imi closed 3 months ago

james-imi commented 3 months ago

I am receiving this error whenever the init_points are fulfilled.

  File "/home/workspace/xray-classifier/train_impaction_opt.py", line 538, in train_opt
    optimizer.maximize(
  File "/home/anaconda3/envs/xray_classifier/lib/python3.10/site-packages/bayes_opt/bayesian_optimization.py", line 372, in maximize
    x_probe = self.suggest(util)
  File "/home/anaconda3/envs/xray_classifier/lib/python3.10/site-packages/bayes_opt/bayesian_optimization.py", line 276, in suggest
    y_max_params=self._space.params_to_array(self._space.max()['params']))
TypeError: 'NoneType' object is not subscriptable

with these constraits


# Define the function to optimize
pbounds = {
    "lr": (1e-3, 1e-4 / 8),
    'weight_decay': (0.01, 0.0001),
    'freeze': (0, 6),
    # Focal loss gamma and alpha
    'gamma': (0.5, 5),
    'alpha': (0.1, 0.9)
}

I printed the values and it shows this

self._gp: GaussianProcessRegressor(alpha=1e-06, kernel=Matern(length_scale=1, nu=2.5),
                         n_restarts_optimizer=5, normalize_y=True,
                         random_state=RandomState(MT19937) at 0x71EBCD6B6D40)
self.constraint: None
self._space._target_max(): None
self._space.bounds: [[1.00e-01 9.00e-01]
 [0.00e+00 6.00e+00]
 [5.00e-01 5.00e+00]
 [1.00e-03 1.25e-05]
 [1.00e-02 1.00e-04]]
Traceback (most recent call last):
  File "/home/anaconda3/envs/xray_classifier/lib/python3.10/site-packages/bayes_opt/bayesian_optimization.py", line 381, in maximize
    x_probe = next(self._queue)
  File "/home/anaconda3/envs/xray_classifier/lib/python3.10/site-packages/bayes_opt/bayesian_optimization.py", line 40, in __next__
    raise StopIteration("Queue is empty, no more objects to retrieve.")
StopIteration: Queue is empty, no more objects to retrieve.
till-m commented 3 months ago

Hi @james-imi,

I suspect that you have no point registered that fulfills the constraint, hence there is no maximum found and .max() returning None. I think you should sample more random points until you find a point that fulfills the constraint. Apologies for the misleading error message, in the future this will raise a more informative error.

till-m commented 3 months ago

Hi @james-imi,

I assume your bug is fixed. If you need more help, feel free to let us know.