Python Hyperopt支持选项子集吗?
我有一组选项a。我想得到选项a的子集。这在Hyperopt中可能吗 输入: 输出:Python Hyperopt支持选项子集吗?,python,hyperopt,Python,Hyperopt,我有一组选项a。我想得到选项a的子集。这在Hyperopt中可能吗 输入: 输出: 没有,但是如果您特别想要该功能,有多种方法可以实现 可以定义新表达式并将其添加到现有hyperopt参数表达式中,如中所示。例如,在您的情况下,您可以执行以下操作: import hyperopt.pyll from hyperopt.pyll import scope from hyperopt.pyll.stochastic import sample # Add a new method as you w
没有,但是如果您特别想要该功能,有多种方法可以实现
import hyperopt.pyll
from hyperopt.pyll import scope
from hyperopt.pyll.stochastic import sample
# Add a new method as you want
@scope.define
def foo(choices, num_choices):
return np.random.choice(choices, size=num_choices, replace=False)
choices = [1,2,3,4]
# Define the space like below and use in fmin
space = scope.foo(choices, 2)
# Call this multiple times to see how space behaves
print(sample(space))
请参阅以了解其工作原理
注意:
方法将返回选择的子集(在numpy数组中)。因此,请确保在目标函数中使用内部多个值,如foo
等x[0]、x[1]、…
- 在代码末尾添加
,否则每次运行代码之前都必须重新启动终端/内核李>scope.undfine(foo)
- hyperopt wiki可以像上面一样定义新类型的参数搜索空间,因为这可能会影响搜索策略或执行得不理想李>
replace=False
的原因),则可以执行以下操作:
choices = [1,2,3,4]
space = [hp.choice('c1', choices),
hp.choice('c2', choices)]
然后在目标函数中,您可以访问两个值,即x[0]
,x[1]
但从您的问题来看,似乎您希望有子选项,这意味着无需替换,因此[1,1]
或[2,2]
等的子集无效。在这种情况下,您应该使用from hyperopt import fmin, tpe, hp, STATUS_OK, STATUS_FAIL, Trials
def objective(x):
# Check if the supplied choices are valid or not
x1 = x[0]
x2 = x[1]
if x1 == x2:
# If invalid, only return the status and hyperopt will understand
return {'status': STATUS_FAIL}
# Normal flow of objective
# Do your coding here
# In the end, return this
return {
'loss': actual_loss, # Fill the actual loss here
'status': STATUS_OK
}
choices = [1,2,3,4]
space = [hp.choice('c1', choices),
hp.choice('c2', choices)]
trials = Trials()
best = fmin(objective, space=space, algo=tpe.suggest, max_evals=100, trials=trials)
from hyperopt import space_eval
print(space_eval(space, best))
希望这有帮助。没有,但是如果您特别想要该功能,有多种方法可以实现
import hyperopt.pyll
from hyperopt.pyll import scope
from hyperopt.pyll.stochastic import sample
# Add a new method as you want
@scope.define
def foo(choices, num_choices):
return np.random.choice(choices, size=num_choices, replace=False)
choices = [1,2,3,4]
# Define the space like below and use in fmin
space = scope.foo(choices, 2)
# Call this multiple times to see how space behaves
print(sample(space))
请参阅以了解其工作原理
注意:
方法将返回选择的子集(在numpy数组中)。因此,请确保在目标函数中使用内部多个值,如foo
等x[0]、x[1]、…
- 在代码末尾添加
,否则每次运行代码之前都必须重新启动终端/内核李>scope.undfine(foo)
- hyperopt wiki可以像上面一样定义新类型的参数搜索空间,因为这可能会影响搜索策略或执行得不理想李>
replace=False
的原因),则可以执行以下操作:
choices = [1,2,3,4]
space = [hp.choice('c1', choices),
hp.choice('c2', choices)]
然后在目标函数中,您可以访问两个值,即x[0]
,x[1]
但从您的问题来看,似乎您希望有子选项,这意味着无需替换,因此[1,1]
或[2,2]
等的子集无效。在这种情况下,您应该使用from hyperopt import fmin, tpe, hp, STATUS_OK, STATUS_FAIL, Trials
def objective(x):
# Check if the supplied choices are valid or not
x1 = x[0]
x2 = x[1]
if x1 == x2:
# If invalid, only return the status and hyperopt will understand
return {'status': STATUS_FAIL}
# Normal flow of objective
# Do your coding here
# In the end, return this
return {
'loss': actual_loss, # Fill the actual loss here
'status': STATUS_OK
}
choices = [1,2,3,4]
space = [hp.choice('c1', choices),
hp.choice('c2', choices)]
trials = Trials()
best = fmin(objective, space=space, algo=tpe.suggest, max_evals=100, trials=trials)
from hyperopt import space_eval
print(space_eval(space, best))
希望这有帮助