Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hyperopt parameter space: TypeError: int() argument must be a string or a number, not 'Apply'

This ran perfectly before I updated several packages, including scikit-learn. Now, the code below gives me a 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'])

I am using python 2.7.12, hyperopt version 0.1, and sklearn version 0.18.1. Full traceback:

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'

Any idea how I can cast the result from hp.uniform as an integer?

EDIT:

Suppose I use hp.randint instead:

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

and later:

    print params['output_units']

Then this is the output:

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

but the whole point of hyperopt is to give you random values for hyperparameter optimization. Surely there's a way to extract a value from this?

like image 716
StatsSorceress Avatar asked May 16 '26 08:05

StatsSorceress


1 Answers

The hyperopt package allows you to define a parameter space. To sample values of that parameter space to use in a model, you need a Trials() object.

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)
like image 56
StatsSorceress Avatar answered May 17 '26 22:05

StatsSorceress