passing supplementary parameters to hyperopt objective function
Asked Answered
T

1

13

I am using Python's hyperopt library to perform ML hyperparameters' optimization. In particular I am trying to find lightgbm optimal hyperparameter using this function to minimize:

def lgb_objective_map(params):
"""
objective function for lightgbm using MAP as success metric.
"""

# hyperopt casts as float
params['num_boost_round'] = int(params['num_boost_round'])
params['num_leaves'] = int(params['num_leaves'])
params['min_data_in_leaf'] = int(params['min_data_in_leaf'])

# need to be passed as parameter
params['verbose'] = -1
params['seed'] = 1

# Cross validation
cv_result = lgb.cv(
params,
lgtrain,
nfold=3,
metrics='binary_logloss',
num_boost_round=params['num_boost_round'],
early_stopping_rounds=20,
stratified=False,
)

# Update the number of trees based on the early stopping results
early_stop_dict[lgb_objective_map.i] = len(cv_result['binary_logloss-mean'])
params['num_boost_round'] = len(cv_result['binary_logloss-mean'])

# fit and predict
#model = lgb.LGBMRegressor(**params)
#model.fit(train,y_train,feature_name=all_cols,categorical_feature=cat_cols)
model= lgb.train(params=params,train_set=lgtrain)
preds = model.predict(X_test)

# add a column with predictions and rank


result = log_loss(y_test,preds)
#    actual_predicted 
actual_predicted = np.sum(y_test)/np.sum(preds)

print("INFO: iteration {} logloss {:.3f} actual on predicted ratio {:.3f}".format(lgb_objective_map.i, 
      result,actual_predicted))

lgb_objective_map.i+=1

return result

The hyperopt call is:

best = fmin(fn=lgb_objective_map,
        space=lgb_parameter_space,
        algo=tpe.suggest,
        max_evals=200,
        trials=trials)

Is is possible to modify the best call in order to pass supplementary parameter to lgb_objective_map like as lgbtrain, X_test, y_test? This would allow to generalize the call to hyperopt.

Turnbow answered 1/2, 2019 at 11:40 Comment(2)
One (ugly) way to do this would be to define some other variables and use them as cases. If params['X_test'] = 0 : X_test = X_test_0 else X_test = X_test_1.Skillful
@CoMartel, I knew that approach, but as you noticed I was looking for a nicer solution :-(Turnbow
C
19

The partial function from functools provides an eloquent solution.

Just wrap your function and add the desired arguments:

partial(yourFunction,arg_1,arg_2,...,arg_n)

Then pass that to hp's fmin function.

Here's a toy example:

from functools import partial
from hyperopt import hp,fmin, STATUS_OK

def objective(params, data):
    output = f(**params, data)
    return {'loss': output ,  'status': STATUS_OK}

fmin_objective = partial(objective, data=data)

bestParams = fmin(fn = fmin_objective ,space = params)
Celerity answered 21/6, 2019 at 20:50 Comment(1)
Just to note f in your code is the parameter and data dependent objective function. Excellent solution, thank you.Callable

© 2022 - 2024 — McMap. All rights reserved.