gpt4 book ai didi

Python/GPyOpt : Optimizing only one argument

转载 作者:行者123 更新时间:2023-11-30 08:58:04 27 4
gpt4 key购买 nike

我目前正在尝试使用 GPyOpt 通过高斯优化找到某些函数 f(arg1, arg2, arg3, ...) 的最小值模块。虽然 f(...) 需要许多输入参数,但我只想优化其中一个。你是怎么做到的?

我当前的“解决方案”是将 f(...) 放入虚拟类中,并在初始化时指定不可优化的参数。虽然这可以说是解决这个问题的最Python式的方法,但它也比它应有的复杂得多。

函数f(x, y, method)的简短工作示例,具有固定的y(数字)和method(字符串)同时优化x:

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 看起来像这样:

myBopt.run_optimization(..., context={'var1': .3, 'var2': 0.4})

更多详细信息可以在这个tutorial notebook中找到关于上下文优化。

关于Python/GPyOpt : Optimizing only one argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51596088/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com