[英]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.