2

在我更新了几个包(包括 scikit-learn)之前,它运行得很好。现在,下面的代码给了我一个 TypeError。

from hyperopt import fmin, tpe, hp, STATUS_OK, Trials

def para_space():

    space_paras = {'model_type': hp.choice('model_type', ['f1', 'f2', 'f3', 'f4']),
                    'output_units': hp.uniform('output_units', 1, 10)}
    return space_paras

if __name__=='__main__':

    params = para_space()

    if params['model_type'] == 'f1':
            include_hours = True
            include_features = False
    else:   
            include_hours = True
            include_features = True

    out = int(params['output_units'])

我正在使用 python 2.7.12、0.1hyperopt版和sklearn0.18.1 版。完整追溯:

Traceback (most recent call last):
  File "testJan25.py", line 26, in <module>
    out = int(params['output_units'])
TypeError: int() argument must be a string or a number, not 'Apply'

知道如何将结果转换hp.uniform为整数吗?

编辑:

假设我hp.randint改用:

def para_space():

        space_paras = {'model_type': hp.choice('model_type', ['f1', 'f2', 'f3', 'f4']),
                        'output_units': hp.randint('output_units', 10)}
        return space_paras

然后:

    print params['output_units']

然后这是输出:

0 hyperopt_param
1   Literal{output_units}
2   randint
3     Literal{10}

但 hyperopt 的重点是为您提供超参数优化的随机值。当然有一种方法可以从中提取价值吗?

4

1 回答 1

0

hyperopt包允许您定义参数空间。要对该参数空间的值进行采样以在模型中使用,您需要一个 Trials() 对象。

def model_1(params):
        #model definition here....
    return 0

params = para_space()
#model_1(params) #THIS IS A PROBLEM! YOU CAN'T CALL THIS. YOU NEED A TRIALS() OBJECT.

trials = Trials()
best = fmin(model_1, params, algo=tpe.suggest, max_evals=1, trials=trials)
于 2018-01-25T21:32:10.657 回答