'Hyperopt NoneType object has no attribute 'randint'

I'm facing some problems using hyperopt


# Here we use a single parameter grid, and execute just one iteration
space = _set_space(parameters_grid)

trials = hyperopt.Trials()
best_params = hyperopt.fmin(fn=_loss_fnc,
                            space=space,
                            algo=hyperopt.tpe.suggest,
                            max_evals=1,
                            rstate=np.random.RandomState(42),
                            timeout=None,
                            trials=trials)
best_params = hyperopt.space_eval(space, best_params)
best_params_idx = np.argmin(trials.losses())

# If the default parameter used in grid is not good, we try again with a bigger grid
if abs(trials.losses()[best_params_idx] < abs(min_score):
    space = _set_space(full_parameters_grid)  # updates the space
    best_params = hyperopt.fmin(fn=_loss_fnc,
                                space=space,
                                algo=hyperopt.tpe.suggest,
                                max_evals=5,
                                rstate=np.random.RandomState(42),
                                timeout=None,
                                trials=trials,
                                loss_threshold=min_score)

best_params = hyperopt.space_eval(space, best_params)


def _set_space(parameters_grid):
    # Just some rules to define the which parameters to include in the grid
    parameters_grid = _update_grid(parameters_grid)

    # processing to hyperopt's format
    search_space = _normalize_for_hyperopt(parameters_grid)
    space = hyperopt.hp.choice('a', list(search_space.values()))
    return space

But it returns the following error:

ERROR in rec_eval
EXCEPTION <class 'AttributeError'> 'NoneType' object has no attribute 'randint'
NODE
0 randint
1   Literal{1}

================================================================================
ERROR: Error while computing regression for bundle 13380
Traceback (most recent call last):
  File "/my_file/optimizer.py", line 225, in fit
    best_params = hyperopt.space_eval(space, trials.argmin)
  File "/usr/local/lib/python3.7/site-packages/hyperopt/fmin.py", line 585, in space_eval
    rval = pyll.rec_eval(space, memo=memo)
  File "/usr/local/lib/python3.7/site-packages/hyperopt/pyll/base.py", line 911, in rec_eval
    rval = scope._impls[node.name](*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/hyperopt/pyll/stochastic.py", line 103, in randint
    return rng.randint(low, high, size)
AttributeError: 'NoneType' object has no attribute 'randint'
ERROR: 'NoneType' object has no attribute 'randint'

Do you have any ideas what the problem shoud be? The model runs without any problem, but when executing space_eval, it crashes.

Also, it is related with the second spaceclass assigned inside the second attempt, because the code runs without any problems if we ommit it.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source