我正在通过包PyGPGO研究贝叶斯优化,它在引擎盖下使用 PyMC3,将高斯过程拟合到底层函数并提出运动以最大化奖励。
到目前为止,根据标量值定义函数似乎真的很容易,尤其是在参数有限的情况下。但是,我不确定是否有办法提供向量/数组参数。
from pyGPGO.covfunc import squaredExponential
from pyGPGO.acquisition import Acquisition
from pyGPGO.surrogates.GaussianProcess import GaussianProcess
from pyGPGO.GPGO import GPGO
def f(x):
return (np.sin(x))
sexp = squaredExponential()
gp = GaussianProcess(sexp)
acq = Acquisition(mode='ExpectedImprovement')
param = {'x': ('cont', [0, 2 * np.pi])}
np.random.seed(23)
gpgo = GPGO(gp, acq, f, param)
gpgo.run(max_iter=20)
我想要的是类似的东西:
# var: type , range
param = {'x': (np.array(3,10), [0, 2 * np.pi])}
如果这不可能,我可能会以 的形式设置多个变量,{x1: ..., x2: ...,}
然后通过函数定义本身相应地访问它们,尽管当参数空间很大时这似乎不是最佳的。
编辑:这是我的方法,这是行不通的......
import numpy as np
from pyGPGO.covfunc import squaredExponential
from pyGPGO.acquisition import Acquisition
from pyGPGO.surrogates.GaussianProcess import GaussianProcess
from pyGPGO.GPGO import GPGO
param = {f'x_{i}': ('cont', [0, 1]) for i in range(3)}
param.update({f'y_{i}': ('cont', [0, 1]) for i in range(3)})
def f(p):
v1 = np.array([p[f'x_{i}'] for i in range(3)])
v2 = np.array([p[f'y_{i}'] for i in range(3)])
return np.dot(v1,v2)
sexp = squaredExponential()
gp = GaussianProcess(sexp)
acq = Acquisition(mode='ExpectedImprovement')
np.random.seed(23)
gpgo = GPGO(gp, acq, f, param)
gpgo.run(max_iter=20)
和错误:
Evaluation Proposed point Current eval. Best eval.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-49-f58f602dca8c> in <module>()
26 np.random.seed(23)
27 gpgo = GPGO(gp, acq, f, param)
---> 28 gpgo.run(max_iter=20)
1 frames
/usr/local/lib/python3.7/dist-packages/pyGPGO/GPGO.py in _firstRun(self, n_eval)
86 s_param_val = list(s_param.values())
87 self.X[i] = s_param_val
---> 88 self.y[i] = self.f(**s_param)
89 self.GP.fit(self.X, self.y)
90 self.tau = np.max(self.y)
TypeError: f() got an unexpected keyword argument 'x_1'
因此,将参数包装到字典中似乎比我预期的要复杂一些,因为对象中的参数param
必须由函数显式调用f
,而不是作为字典接收,p
。
以前有没有人解决过这个问题?