I am trying to run a Bayesian Parameter Optimization Using the gp_minimize
of the Skopt
package but kept getting "TypeError: objective() got an unexpected keyword argument 'param1'". I have been trying to resolve this since yesterday but have made no headway. I am using the Anaconda environment for all the work.
Here is the my code:
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)
# 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)
And this is the detail of the error I am getting:
---------------------------------------------------------------------------
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
Anyone with an understanding of what is happening should help me resolve this, please.
You have a typo in attributes names. Change param
to params
. This should fix the issue:
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')
]