0

我在使用@use_named_argsScikit Optimize时遇到问题。问题是我的目标函数接受参数NamedTuple并且我无法更改它,因为这是我正在处理的项目中的要求。现在,我需要实现skopt超参数搜索,我需要用它@use_named_args来装饰我的目标函数。由于我的目标函数接受NamedTuple而不是单个参数(如skopt示例中的那个),我该怎么做?除了需要调整的可变超参数之外,我还需要传递一个固定的超参数集。

下面是我想要实现的代码,但我不能,因为我不能my_objective_function@use_named_args

from skopt.space import Real
from skopt import forest_minimize
from skopt.utils import use_named_args
from functools import partial

dim1 = Real(name='foo', low=0.0, high=1.0)
dim2 = Real(name='bar', low=0.0, high=1.0)
dim3 = Real(name='baz', low=0.0, high=1.0)

dimensions = [dim1, dim2, dim3]

class variable_params(NamedTuple):
    bar: int
    foo: int
    baz: int

class fixed_params(NamedTuple):
    bar1: int
    foo1: int
    baz1: int

# Instantiate object
variable_args = variable_params(foo=5, bar=10, baz=2)
fixed_args = fixed_params(foo1=2, bar1=3, baz1=4)


@use_named_args(dimensions=dimensions)
def my_objective_function(v_args, f_args):
    return v_args.foo ** 2 + v_args.bar ** 4 + v_args.baz ** 8 + f_args.foo1 * 2 + f_args.bar1 * 4 + f_args.baz1 * 8

#Do partial function for passing the fixed params
my_objective_function = partial(my_objective_function,f_args=fixed_args)

result = forest_minimize(
    func=my_objective_function, 
    dimensions=dimensions, 
    n_calls=20, 
    base_estimator="ET",
    random_state=4
)

谢谢!

4

1 回答 1

0

您可以创建一个新的目标函数以传递给优化器。它将接收可变参数,将其转换为命名元组,然后调用原始目标。

稍微调整你的例子,你会得到类似的东西:

from skopt.space import Real
from skopt import forest_minimize
from skopt.utils import use_named_args
from collections import namedtuple

dim1 = Real(name='foo', low=0.0, high=1.0)
dim2 = Real(name='bar', low=0.0, high=1.0)
dim3 = Real(name='baz', low=0.0, high=1.0)

dimensions = [dim1, dim2, dim3]

VariableParams = namedtuple('VariableParams', 'foo bar baz')
FixedParams = namedtuple('FixedParams', 'foo1 bar1 baz1')

# define fixed params
fixed_args = FixedParams(foo1=2, bar1=3, baz1=4)


# objective you are not allowed to change
def my_objective_function(v_args, f_args):
    return v_args.foo ** 2 + v_args.bar ** 4 + v_args.baz ** 8 + f_args.foo1 * 2 + f_args.bar1 * 4 + f_args.baz1 * 8


# new objective passed to the optimizer
@use_named_args(dimensions)
def objective(foo, bar, baz):
    variable_args = VariableParams(foo, bar, baz)
    return my_objective_function(variable_args, fixed_args)


# run search with new objective
result = forest_minimize(
    func=objective,
    dimensions=dimensions,
    n_calls=10
)

于 2021-11-03T08:01:47.297 回答