我有一组选择A,我想拿回一组选择A的子集,这能用Hyperopt吗?
投入:
{
'A': hp.choice('A', [0, 1, 2, 3])
}产出:
{
'A': [0, 2]
}发布于 2018-12-19 11:27:04
不是,但是如果您特别想要这个功能,有多种方法可以做到这一点。
- The `foo` method will return a subset of choices (in a numpy array). So make sure that in your objective function, you are using the internal multiple values like `x[0], x[1], ...` etc.
- Add `scope.undefine(foo)` in the end of your code, or else you will have to restart your terminal / kernel each time before running the code.
- The hyperopt wiki [specifically prohibits](https://github.com/hyperopt/hyperopt/wiki/FMin#24-adding-new-kinds-of-hyperparameter) to define new types of parameter search spaces just as we did above because that may affect the search strategy or perform non-optimally.
replace=False的原因,然后可以执行以下操作:
选择= 1,2,3,4空格= hp.choice('c1',选择),hp.choice('c2',选择)
然后,在目标函数中,可以访问两个值,如x[0]、x[1]。
但是从你的问题上看,你似乎想要有子选择,这意味着没有替换,所以一个子集的[1,1]或[2,2]等是无效的。在这种情况下,您应该使用status。第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))希望这能有所帮助。
发布于 2021-08-12 20:18:11
是的,虽然代码有点麻烦,但还是有。将A的每个元素分别定义为包含和排除之间的选择。例如:
space = {
"A0": hp.choice("A0", [False, True]),
"A1": hp.choice("A1", [False, True]),
"A2": hp.choice("A2", [False, True]),
...
}在目标函数中解释这一点的代码也非常容易:
A = [i for i in range(num_choices) if space["A"+str(i)]]
# A = [0, 2] for example这将返回A的真正随机子集(从空集到整个A的任何地方)。
https://stackoverflow.com/questions/51367504
复制相似问题