我目前正在尝试通过使用GPyOpt模块的高斯优化来找到某个函数f(arg1, arg2, arg3, ...)
的最小值。虽然f(...)
有许多输入参数,但我只想优化其中的一个。你怎么做到的?在
我当前的“解决方案”是将f(...)
放入一个伪类中,并在初始化时指定不需要优化的参数。虽然这可以说是解决这个问题的最具Python式的方法,但它也比它有任何权利要复杂得多。在
优化x
时,函数f(x, y, method)
(数字)和method
(字符串)固定的函数f(x, y, method)
:
import GPyOpt
import numpy as np
# dummy class
class TarFun(object):
# fix y while initializing the object
def __init__(self, y, method):
self.y = y
self.method = method
# actual function to be minimized
def f(self, x):
if self.method == 'sin':
return np.sin(x-self.y)
elif self.method == 'cos':
return np.cos(x-self.y)
# create TarFun object with y fixed to 2 and use 'sin' method
tarFunObj = TarFun(y=2, method='sin')
# describe properties of x
space = [{'name':'x', 'type': 'continuous', 'domain': (-5,5)}]
# create GPyOpt object that will only optimize x
optObj = GPyOpt.methods.BayesianOptimization(tarFunObj.f, space)
肯定有更简单的方法。但是我发现的所有示例都优化了所有参数,我无法在github上读取代码(我想我可以在GPyOpt.core.task.space中找到信息,但是没有运气)。在
GPyOpt本机支持上下文。您可以描述函数的整个域,然后在调用优化例程时使用上下文字典来修复某些变量的值。API是这样的:
有关上下文优化的详细信息,请参阅tutorial notebook。在
我将从functools标准库中检查
partial
函数。它允许您部分指定一个函数,例如:相关问题 更多 >
编程相关推荐