12

我正在使用 Python 的 hyperopt 库来执行 ML 超参数的优化。特别是我正在尝试使用此函数找到 lightgbm 最佳超参数以最小化:

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

hyperopt 调用是:

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

是否可以修改best调用以将补充参数传递给lgb_objective_maplike as lgbtrain, X_test, y_test?这将允许概括对 hyperopt 的调用。

4

1 回答 1

15

partial函数 fromfunctools提供了一个雄辩的解决方案。

只需包装您的函数并添加所需的参数:

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

然后将其传递给 hp 的fmin函数。

这是一个玩具示例:

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)
于 2019-06-21T20:50:44.327 回答