首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Hyperopt是否支持选择的子集?

Hyperopt是否支持选择的子集?
EN

Stack Overflow用户
提问于 2018-07-16 17:49:35
回答 2查看 1K关注 0票数 0

我有一组选择A,我想拿回一组选择A的子集,这能用Hyperopt吗?

投入:

代码语言:javascript
复制
{
    'A': hp.choice('A', [0, 1, 2, 3])
}

产出:

代码语言:javascript
复制
{
    'A': [0, 2]
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-12-19 11:27:04

不是,但是如果您特别想要这个功能,有多种方法可以做到这一点。

  1. 您可以定义一个新表达式并将其添加到文献资料中给定的现有超选择参数表达式中。例如,在您的情况下,您可以: 从hyperopt.pyll导入示例#从hyperopt.pyll导入作用域导入一个新方法,以满足您的需要:返回np.random.choice(选择、size=num_choices、replace=False)选择= 1,2,3,4 #定义如下所示的空格,并在fmin space =scope.foo(选择,2) #多次调用此方法以查看空间如何运行--打印(示例(空间))--查看numpy.random.choice文档化以了解如何工作。 Note
代码语言:javascript
复制
- 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. 

  1. 如果允许使用替换来选择两个值(这意味着子集中的两个值有时是相同的)。这就是我们在第1点中使用replace=False的原因,然后可以执行以下操作: 选择= 1,2,3,4空格= hp.choice('c1',选择),hp.choice('c2',选择) 然后,在目标函数中,可以访问两个值,如x[0]x[1]。 但是从你的问题上看,你似乎想要有子选择,这意味着没有替换,所以一个子集的[1,1][2,2]等是无效的。在这种情况下,您应该使用status

第2点的示例程序如下:

代码语言:javascript
复制
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))

希望这能有所帮助。

票数 4
EN

Stack Overflow用户

发布于 2021-08-12 20:18:11

是的,虽然代码有点麻烦,但还是有。将A的每个元素分别定义为包含和排除之间的选择。例如:

代码语言:javascript
复制
space = {
    "A0": hp.choice("A0", [False, True]),
    "A1": hp.choice("A1", [False, True]),
    "A2": hp.choice("A2", [False, True]),
    ...
}

在目标函数中解释这一点的代码也非常容易:

代码语言:javascript
复制
A = [i for i in range(num_choices) if space["A"+str(i)]]
# A = [0, 2] for example

这将返回A的真正随机子集(从空集到整个A的任何地方)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51367504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档