You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Skopt gp_minimize时遇TypeError:objective()接收到意外关键字参数

解决Skopt gp_minimize中的TypeError: objective() got an unexpected keyword argument 'param1'问题

问题场景

在Anaconda环境中使用Skopt库的gp_minimize进行贝叶斯参数优化时,持续触发如下错误:

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

相关代码及错误详情如下:

原代码

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)
    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)

# Perform Bayesian Optimization
result = gp_minimize(
    func=objective,
    dimensions=space,
    n_calls=50,  # Number of evaluations
    random_state=42,
    verbose=True
)

# Print the best parameters and best score
print("Best parameters:", result.x)
print("Best predicted combined performance:", -result.fun)

错误堆栈

---------------------------------------------------------------------------
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'

错误原因

@use_named_args(space)装饰器会自动按照space列表中每个Real对象定义的name字段(如param1)作为关键字参数传递给objective函数,但你的objective函数参数名写成了params1(多了一个s),参数名与装饰器传递的关键字参数不匹配,导致触发TypeError。

修正后的代码

将objective函数的参数名修改为与space中name完全一致的名称,同时补充缺失的numpy导入:

from skopt import gp_minimize
from skopt.space import Real
from skopt.utils import use_named_args
import numpy as np

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')
]

@use_named_args(space)
# 参数名改为param1~param10,与space中的name字段匹配
def objective(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10):
    param_values = np.array([param1, param2, param3, param4, param5,
                            param6, param7, param8, param9, param10]).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]
    
    return (0.4 * therm_e_pred + 0.3 * conv_e_pred + 0.3 * carb_conv_e_pred)

# Perform Bayesian Optimization
result = gp_minimize(
    func=objective,
    dimensions=space,
    n_calls=50,
    random_state=42,
    verbose=True
)

print("Best parameters:", result.x)
print("Best predicted combined performance:", -result.fun)

内容的提问来源于stack exchange,提问作者nomacreates

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 18:45:10