"AttributeError: 'bayes_opt' Module Lacks 'UtilityFunction' When Using Ray Tune's BayesOptSearch"

When I attempted to run the Ray Tune example of Bayes algorithm that I found on the web manual, I encountered the following issues.

I got a AttributeError which stated that the bayes_opt module has has no attribute UtilityFunction. The version of my ray tune is 2.40.0 and bayes_opt is 2.0.3. The error message pointed to a specific line in the code where the BayesOptSearch was being initialized.

Here is the relevant part of the code snippet:

from ray import tune
from bayes_opt import BayesianOptimization
from ray.tune.search import ConcurrencyLimiter
from ray.tune.search.bayesopt import BayesOptSearch
import time

def evaluate(step, width, height):
    time.sleep(0.1)
    return (0.1 + width * step / 100) ** (-1) + height * 0.1

def objective(config):
    for step in range(config["steps"]):
        score = evaluate(step, config["width"], config["height"])
        tune.report({"iterations": step, "mean_loss": score})

algo = BayesOptSearch(utility_kwargs={"kind": "ucb", "kappa": 2.5, "xi": 0.0})
algo = ConcurrencyLimiter(algo, max_concurrent=4)
num_samples = 1000
search_space = {
    "steps": 100,
    "width": tune.uniform(0, 20),
    "height": tune.uniform(-100, 100),
}
tuner = tune.Tuner(
    objective,
    tune_config=tune.TuneConfig(
        metric="mean_loss",
        mode="min",
        search_alg=algo,
        num_samples=num_samples,
    ),
    param_space=search_space,
)
results = tuner.fit()

The error occurred precisely at this line: algo = BayesOptSearch(utility_kwargs={"kind": "ucb", "kappa": 2.5, "xi": 0.0}).

The full error traceback looked like this:

AttributeError                            Traceback (most recent call last)
Cell In[4], line 15
     13         score = evaluate(step, config["width"], config["height"])
     14         tune.report({"iterations": step, "mean_loss": score})
---> 15 algo = BayesOptSearch(utility_kwargs={"kind": "ucb", "kappa": 2.5, "xi": 0.0})
     16 algo = ConcurrencyLimiter(algo, max_concurrent=4)
     17 num_samples = 1000

File ~/miniconda3/envs/myenv/lib/python3.10/site-packages/ray/tune/search/bayesopt/bayesopt_search.py:194, in BayesOptSearch.__init__(self, space, metric, mode, points_to_evaluate, utility_kwargs, random_state, random_search_steps, verbose, patience, skip_duplicate, analysis)
    191 self.random_search_trials = random_search_steps
    192 self._total_random_search_trials = 0
--> 194 self.utility = byo.UtilityFunction(**utility_kwargs)
    196 self._analysis = analysis
    198 if isinstance(space, dict) and space:

AttributeError: module 'bayes_opt' has no attribute 'UtilityFunction'
1 Like

Also having this issue. Were you able to find a fix, @gyd-cmd?

I think ray.tune does not yet support the newest version of bayes_opt.
Downgrade bayes_opt to 1.5.1 worked for me.