1

我可以使用 SwarmPackagePy 库绘制 Firefly 算法的 3D 动画。

Firefly 算法的 3D 动画

我想用这个算法来优化高斯过程回归(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),如下所示:

gp = GaussianProcessRegressor(kernel=kernel, alpha=1.5, optimizer=alh, n_restarts_optimizer=5)

但是,运行python代码后,我收到如下错误:

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

我做错了吗?错误的原因可能是什么?

谢谢!

4

1 回答 1

1

正如 sklearn 的文档所说,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
于 2018-04-23T08:41:09.637 回答