使用 Skopt gp_minimize 时出现 TypeError 异常:objective() 获得意外的关键字参数

问题描述 投票:0回答:1

我正在尝试使用

gp_minimize
包的
Skopt
运行贝叶斯参数优化,但不断收到“TypeError:objective() 获得意外的关键字参数 'param1'”。从昨天开始我就一直在努力解决这个问题,但没有取得任何进展。我正在使用 Anaconda 环境来完成所有工作。

这是我的代码:

from skopt import gp_minimize
from skopt.space import Real
from skopt.utils import use_named_args

space = [
    Real(low=0, high=1, name='param1'),
    Real(low=0, high=0.5, name='param2'),
    Real(low=0, high=0.5, name='param3'),
    Real(low=0, high=4, name='param4'),
    Real(low=0, high=4, name='param5'),
    Real(low=0, high=0.075, name='param6'),
    Real(low=0, high=0.01, name='param7'),
    Real(low=0, high=1, name='param8'),
    Real(low=0, high=0.001, name='param9'),
    Real(low=0, high=4, name='param10')
]

# Define the objective functions

@use_named_args(space)
def objective(params1, params2, params3, params4, params5, params6, params7, params8, params9, params10):
    param_values = np.array([params1, params2, params3, params4, params5,
                            params6, params7, params8, params9, params10]).reshape(1, -1)
    # param_values = param_values.reshape(1, -1)
    conv_e_pred = least_linear_loss_model_conv_e.predict(param_values)[0]
    carb_conv_e_pred = least_linear_sigmoid_loss_model_carb_conv_e.predict(param_values)[0]
    therm_e_pred = least_linear_sigmoid_loss_model_therm_e.predict(param_values)[0]
    
    # Combine objectives (e.g., weighted sum)
    return (0.4 * therm_e_pred + 0.3 * conv_e_pred + 0.3 * carb_conv_e_pred)

)

# Print the best parameters and best score
print("Best parameters:", result.x)
print("Best predicted combined performance:", -result.fun)
# Perform Bayesian Optimization
result = gp_minimize(
    func=objective,
    dimensions=space,
    n_calls=50,  # Number of evaluations
    random_state=42,
    verbose=True

这是我收到的错误的详细信息:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[116], line 2
      1 # Perform Bayesian Optimization
----> 2 result = gp_minimize(
      3     func=objective,
      4     dimensions=space,
      5     n_calls=50,  # Number of evaluations
      6     random_state=42,
      7     verbose=True
      8 )
     10 # Print the best parameters and best score
     11 print("Best parameters:", result.x)

File ~\anaconda3\envs\tf\lib\site-packages\skopt\optimizer\gp.py:281, in gp_minimize(func, dimensions, base_estimator, n_calls, n_random_starts, n_initial_points, initial_point_generator, acq_func, acq_optimizer, x0, y0, random_state, verbose, callback, n_points, n_restarts_optimizer, xi, kappa, noise, n_jobs, model_queue_size, space_constraint)
    273 if base_estimator is None:
    274     base_estimator = cook_estimator(
    275         "GP",
    276         space=space,
    277         random_state=rng.randint(0, np.iinfo(np.int32).max),
    278         noise=noise,
    279     )
--> 281 return base_minimize(
    282     func,
    283     space,
    284     base_estimator=base_estimator,
    285     acq_func=acq_func,
    286     xi=xi,
    287     kappa=kappa,
    288     acq_optimizer=acq_optimizer,
    289     n_calls=n_calls,
    290     n_points=n_points,
    291     n_random_starts=n_random_starts,
    292     n_initial_points=n_initial_points,
    293     initial_point_generator=initial_point_generator,
    294     n_restarts_optimizer=n_restarts_optimizer,
    295     x0=x0,
    296     y0=y0,
    297     random_state=rng,
    298     verbose=verbose,
    299     space_constraint=space_constraint,
    300     callback=callback,
    301     n_jobs=n_jobs,
    302     model_queue_size=model_queue_size,
    303 )

File ~\anaconda3\envs\tf\lib\site-packages\skopt\optimizer\base.py:332, in base_minimize(func, dimensions, base_estimator, n_calls, n_random_starts, n_initial_points, initial_point_generator, acq_func, acq_optimizer, x0, y0, random_state, verbose, callback, n_points, n_restarts_optimizer, xi, kappa, n_jobs, model_queue_size, space_constraint)
    330 for _ in range(n_calls):
    331     next_x = optimizer.ask()
--> 332     next_y = func(next_x)
    333     result = optimizer.tell(next_x, next_y)
    334     result.specs = specs

File ~\anaconda3\envs\tf\lib\site-packages\skopt\utils.py:779, in use_named_args.<locals>.decorator.<locals>.wrapper(x)
    776 arg_dict = {dim.name: value for dim, value in zip(dimensions, x)}
    778 # Call the wrapped objective function with the named arguments.
--> 779 objective_value = func(**arg_dict)
    781 return objective_value

TypeError: objective() got an unexpected keyword argument 'param1

任何了解正在发生的事情的人都应该帮助我解决这个问题。

python tensorflow anaconda skopt
1个回答
0
投票

您的属性名称有拼写错误。将

param
更改为
params
。这应该可以解决问题:

space = [
    Real(low=0, high=1, name='params1'),
    Real(low=0, high=0.5, name='params2'),
    Real(low=0, high=0.5, name='params3'),
    Real(low=0, high=4, name='params4'),
    Real(low=0, high=4, name='params5'),
    Real(low=0, high=0.075, name='params6'),
    Real(low=0, high=0.01, name='params7'),
    Real(low=0, high=1, name='params8'),
    Real(low=0, high=0.001, name='params9'),
    Real(low=0, high=4, name='params10')
]

© www.soinside.com 2019 - 2024. All rights reserved.