在sklearn的python代码中如何在回归中使用SwarmPackagePy?

2024-10-16 20:43:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以用SwarmPackagePy库绘制萤火虫算法的三维动画。在

3D animation of Firefly algoirthm

我想用这个算法来优化高斯过程回归(GPR)中的超参数。为此,我将GPR的优化器定义为:

alh = SwarmPackagePy.fa(50, tf.easom_function, 0, 16, 2, 10, 1, 1, 1, 0.1, 0, 0.1)

animation3D(alh.get_agents(),tf.easom_function, 10,-10)

然后我在GPR中使用了这个优化器(alh),如下所示:

^{pr2}$

但是,在运行python代码之后,我得到一个错误,如下所示:

ValueError: Unknown optimizer <SwarmPackagePy.fa.fa object at 0x0982A3B0>.

我做错事了吗?错误的原因是什么?在

谢谢你!在


Tags: 算法参数定义过程tf错误绘制function
1条回答
网友
1楼 · 发布于 2024-10-16 20:43:21

正如sklearn的documentation所说,optimizer参数需要一个可调用的。但是,SwarmPackagePy.fa不是可调用的。因为它既不是方法,也不是实现__call__方法的类,从这里可以看出:

https://github.com/SISDevelop/SwarmPackagePy/blob/master/SwarmPackagePy/fa.py

您可以在该文件中编写您自己的__call__方法,方法的签名与:

def __call__(obj_func, initial_theta, bounds):
# you need to write
# * 'obj_func' is the objective function to be maximized, which
#   takes the hyperparameters theta as parameter and an
#   optional flag eval_gradient, which determines if the
#   gradient is returned additionally to the function value
# * 'initial_theta': the initial value for theta, which can be
#   used by local optimizers
# * 'bounds': the bounds on the values of theta
....
# Returned are the best found hyperparameters theta and
# the corresponding value of the target function.
    return theta_opt, func_min

相关问题 更多 >