首页 > 解决方案 > HyperOptSearch 和 ray.tune

问题描述

我正在尝试使用 HyperOptSearch 和 ray.tune 进行参数优化。该代码适用于 hyperopt(无调整),但我希望它更快,因此使用调整。不幸的是我找不到很多例子,所以我不确定代码。我使用带有 XGboost 的管道,但不仅要优化 XGboost 中的参数,还要优化管道中用于编码的另一个参数。这可能与音调有关吗?我的代码如下。

from hyperopt import hp
from ray import tune
from ray.tune.suggest.hyperopt import HyperOptSearch

def train_model(space, reporter):
    #target encoding
    columns_te=no_of_classes[no_of_classes.counts>space['enc_threshold']].feature.values.tolist()
    #one hot encoding
    columns_ohe=categorical.columns[~categorical.columns.isin(cols_te)].tolist()

    #model
    pipe1 = SKPipeline([('ohe',
                          OneHotEncoder(cols=columns_ohe, return_df=True,handle_unknown='ignore', use_cat_names=True)), 
                          ('te',
                          TargetEncoder(cols= columns_te, min_samples_leaf=space['min_samples_leaf']))])

    pipe2 = IMBPipeline([
         ('sampling',RandomUnderSampler()),
         ('clf', xgb.XGBClassifier(**space, n_jobs = -1))
        ])

    model = SKPipeline([('pipe1', pipe1), ('pipe2', pipe2)])

    optimizer = SGD()
    dataset = xx
    accuracy = model.fit(dataset.drop(['yy']), dataset.yy)
    reporter(mean_accuracy=roc_auc)

if __name__ == '__main__':
    ray.init()

    space = {'eta':hp.uniform('eta',0.001,0.1),
            'max_depth':scope.int(hp.quniform('max_depth', 1,5,1)),
            'min_child_weight': hp.uniform('min_child_weight', 0.1, 1.5),
            'n_estimators': scope.int(hp.quniform('n_estimators',20,200,10)),
            'subsample': hp.uniform('subsample',0.5,0.9),
            'colsample_bytree': hp.uniform('colsample_bytree',0.5,0.9),
            'gamma': hp.uniform('gamma',0,5),
            'min_samples_leaf':scope.int(hp.quniform('min_samples_leaf',10,200,20)),
            'nrounds':scope.int(hp.quniform('nrounds',100,1500,50))
             }



    algo = HyperOptSearch(space, max_concurrent=5,  metric='roc_auc', mode="max")
    tune.run(train_model, num_samples=10, search_alg=algo)

标签: parallel-processingxgboostrayhyperopt

解决方案


推荐阅读