简体   繁体   English

使用来自 Scikit Optimize 的 @use_named_args

[英]Using @use_named_args from Scikit Optimize

I'm having a problem on using @use_named_args from Scikit Optimize .我在使用来自Scikit Optimize@use_named_args时遇到问题。 The problem is that my objective function accepts the arguments NamedTuple and I can't change this because this is the requirement in the project I'm working on.问题是我的目标 function 接受 arguments NamedTuple我无法更改它,因为这是我正在处理的项目中的要求。 Now, I need to implement skopt for hyperparameters search and I need to use @use_named_args to decorate my objective function.现在,我需要为超参数搜索实现skopt ,我需要使用@use_named_args来装饰我的目标 function。 How can I do it since my objective function accepting NamedTuple instead of single arguments (like the one on skopt example)?既然我的目标 function 接受NamedTuple而不是单个 arguments (如skopt示例中的那个),我该怎么做? I also need to pass a fixed hyperparameters set in addition to the variable hyperparameters that I need to tune.除了需要调整的可变超参数之外,我还需要传递一个固定的超参数集。

Below is the code I want to achieve, but I can't because I can't decorate my_objective_function with @use_named_args下面是我想要实现的代码,但我不能,因为我不能用@use_named_args装饰my_objective_function

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
)

Thank you!谢谢!

You can just create a new objective function to be passed to the optimizer.您可以创建一个新目标 function 以传递给优化器。 It will receive the variable parameters, convert those to a named tuple and then call the original objective.它将接收可变参数,将其转换为命名元组,然后调用原始目标。

Slightly adjusting your example you get something like:稍微调整你的例子,你会得到类似的东西:

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
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在类中使用 scikit-learn 优化(尤其是 use_named_args 装饰器)? - How to use scikit-learn optimize in a class (especially the use_named_args decorator)? 使用scipy优化将args从实例方法传递给函数 - Passing args from Instance Method to function using scipy optimize 没有使用anaconda的名为“ scikit”的模块 - No module named 'scikit' using anaconda 在 scikit-optimize 中使用 KerasRegressor 的示例 - Example of using a KerasRegressor in scikit-optimize 如何在 scipy.optimize.minimize 中使用有趣的 output args 作为 jac 的输入 args? - How to use the output args of fun as the input args of jac in scipy.optimize.minimize? Scikit 优化和离散变量 - Scikit optimize and discrete variables 当某些输入被命名而其他输入未被命名时,如何在 python 中使用(或打开)args? - How to use (or open) args in python when some inputs are named and others are not? 确定 *args 是否提供来自装饰器的特定命名参数 - Determine whether *args is providing a specific named argument from a decorator scipy.optimize中的可选args - optional args in scipy.optimize 如何在 Scikit SGDClassifier 中使用 partial_fit 增加迭代次数以优化每一步的成本函数? - How to increase the number of iterations to optimize my cost function at each step using partial_fit at Scikit SGDClassifier?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM