bayesian-optimization / BayesianOptimization

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

`FloatingPointError: underflow encountered in exp` in `optimizer.maximize()` #432

Closed JaydenTeoh closed 1 year ago

JaydenTeoh commented 1 year ago

Describe the bug A clear and concise description of what the bug is. Using bayesian-optimization==1.4.3 and scipy==1.10.1 results in FloatingPointError: underflow encountered in exp

Here is the code snippet I am testing on:

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

pbounds = {'x': (2, 4), 'y': (-3, 3)}

optimizer = BayesianOptimization(f=black_box_function, pbounds=pbounds, verbose=2, random_state=7)

optimizer.maximize(init_points=45, n_iter=20)

This is the error log, seems like when Queue is empty, the self.suggest(util) function gets called which calls the kernel in gaussian process and that's where the exponential underflow occurs:

Traceback (most recent call last):
  File "/common/home/users/j/jxteoh.2023/.conda/envs/dcd/lib/python3.8/site-packages/bayes_opt/bayesian_optimization.py", line 305, in maximize
    x_probe = next(self._queue)
  File "/common/home/users/j/jxteoh.2023/.conda/envs/dcd/lib/python3.8/site-packages/bayes_opt/bayesian_optimization.py", line 27, 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 "/common/home/users/j/jxteoh.2023/.conda/envs/dcd/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/common/home/users/j/jxteoh.2023/.conda/envs/dcd/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/common/home/users/j/jxteoh.2023/algos/bw/train.py", line 196, in <module>
    stats = train_runner.run(j)
  File "/common/home/users/j/jxteoh.2023/algos/bw/envs/runners/adversarial_runner.py", line 815, in run
    agent_info = self.agent_rollout(
  File "/common/home/users/j/jxteoh.2023/algos/bw/envs/runners/adversarial_runner.py", line 719, in agent_rollout
    level_sampler.update_with_rollouts(agent.storage)
  File "/common/home/users/j/jxteoh.2023/algos/bw/level_replay/level_sampler.py", line 271, in update_with_rollouts
    self._update_with_rollouts(rollouts, score_function)
  File "/common/home/users/j/jxteoh.2023/algos/bw/level_replay/level_sampler.py", line 651, in _update_with_rollouts
    _, seed_idx = self.update_seed_score(actor_index, seed_t, score, max_score, diversity_t, num_steps)
  File "/common/home/users/j/jxteoh.2023/algos/bw/level_replay/level_sampler.py", line 276, in update_seed_score
    score, seed_idx = self._partial_update_seed_score_buffer(actor_index, seed, score, diversity, num_steps, done=True)
  File "/common/home/users/j/jxteoh.2023/algos/bw/level_replay/level_sampler.py", line 327, in _partial_update_seed_score_buffer
    seed_idx = self._next_buffer_index                  # retrieve the seed_idx with min support
  File "/common/home/users/j/jxteoh.2023/algos/bw/level_replay/level_sampler.py", line 372, in _next_buffer_index
    return self.sample_balanced_weights_w_diversity().argmin()     
  File "/common/home/users/j/jxteoh.2023/algos/bw/level_replay/level_sampler.py", line 906, in sample_balanced_weights_w_diversity
    self.balanced_diversity_coef_support = self.get_balanced_diversity_weightage(np.mean(weights), np.mean(diversity_weights))
  File "/common/home/users/j/jxteoh.2023/algos/bw/level_replay/level_sampler.py", line 196, in get_balanced_diversity_weightage
    self.balanced_weight_optimizer.maximize(init_points=45, n_iter=20)
  File "/common/home/users/j/jxteoh.2023/.conda/envs/dcd/lib/python3.8/site-packages/bayes_opt/bayesian_optimization.py", line 281, in maximize
    x_probe = self.suggest(util)
  File "/common/home/users/j/jxteoh.2023/.conda/envs/dcd/lib/python3.8/site-packages/bayes_opt/bayesian_optimization.py", line 190, in suggest
    self._gp.fit(self._space.params, self._space.target)
  File "/common/home/users/j/jxteoh.2023/.conda/envs/dcd/lib/python3.8/site-packages/sklearn/gaussian_process/_gpr.py", line 304, in fit
    self._constrained_optimization(obj_func, theta_initial, bounds)
  File "/common/home/users/j/jxteoh.2023/.conda/envs/dcd/lib/python3.8/site-packages/sklearn/gaussian_process/_gpr.py", line 622, in _constrained_optimization
    opt_res = scipy.optimize.minimize(
  File "/common/home/users/j/jxteoh.2023/.conda/envs/dcd/lib/python3.8/site-packages/scipy/optimize/_minimize.py", line 696, in minimize
    res = _minimize_lbfgsb(fun, x0, args, jac, bounds,
  File "/common/home/users/j/jxteoh.2023/.conda/envs/dcd/lib/python3.8/site-packages/scipy/optimize/_lbfgsb_py.py", line 305, in _minimize_lbfgsb
    sf = _prepare_scalar_function(fun, x0, jac=jac, args=args, epsilon=eps,
  File "/common/home/users/j/jxteoh.2023/.conda/envs/dcd/lib/python3.8/site-packages/scipy/optimize/_optimize.py", line 332, in _prepare_scalar_function
    sf = ScalarFunction(fun, x0, args, grad, hess,
  File "/common/home/users/j/jxteoh.2023/.conda/envs/dcd/lib/python3.8/site-packages/scipy/optimize/_differentiable_functions.py", line 158, in __init__
    self._update_fun()
  File "/common/home/users/j/jxteoh.2023/.conda/envs/dcd/lib/python3.8/site-packages/scipy/optimize/_differentiable_functions.py", line 251, in _update_fun
    self._update_fun_impl()
  File "/common/home/users/j/jxteoh.2023/.conda/envs/dcd/lib/python3.8/site-packages/scipy/optimize/_differentiable_functions.py", line 155, in update_fun
    self.f = fun_wrapped(self.x)
  File "/common/home/users/j/jxteoh.2023/.conda/envs/dcd/lib/python3.8/site-packages/scipy/optimize/_differentiable_functions.py", line 137, in fun_wrapped
    fx = fun(np.copy(x), *args)
  File "/common/home/users/j/jxteoh.2023/.conda/envs/dcd/lib/python3.8/site-packages/scipy/optimize/_optimize.py", line 76, in __call__
    self._compute_if_needed(x, *args)
  File "/common/home/users/j/jxteoh.2023/.conda/envs/dcd/lib/python3.8/site-packages/scipy/optimize/_optimize.py", line 70, in _compute_if_needed
    fg = self.fun(x, *args)
  File "/common/home/users/j/jxteoh.2023/.conda/envs/dcd/lib/python3.8/site-packages/sklearn/gaussian_process/_gpr.py", line 276, in obj_func
    lml, grad = self.log_marginal_likelihood(
  File "/common/home/users/j/jxteoh.2023/.conda/envs/dcd/lib/python3.8/site-packages/sklearn/gaussian_process/_gpr.py", line 546, in log_marginal_likelihood
    K, K_gradient = kernel(self.X_train_, eval_gradient=True)
  File "/common/home/users/j/jxteoh.2023/.conda/envs/dcd/lib/python3.8/site-packages/sklearn/gaussian_process/kernels.py", line 1704, in __call__
    K = (1.0 + K + K**2 / 3.0) * np.exp(-K)
FloatingPointError: underflow encountered in exp
till-m commented 1 year ago

Running on bayesian-optimization==1.4.3, scikit-learn==1.2.2 and scipy==1.10.0 I do not get this error. What's your scikit-learn version?

JaydenTeoh commented 1 year ago

@till-m I am using scikit-learn==1.2.2 as well. Not sure if it matters but I am running it on a nvidia GPU cluster

till-m commented 1 year ago

Not sure if it matters but I am running it on a nvidia GPU cluster

I guess it could, though this would be above my paygrade. Can you check if the limits of numerical precision are somehow lower on your system then they would usually be?

till-m commented 1 year ago

Closing this for now -- feel free to reopen if the issue persists and you think it is due to this package.