HyperOptSearch と ray.tune でパラメーターの最適化を試みています。コードは hyperopt (tune なし) で動作しますが、もっと高速にしたいので tune を使用します。残念ながら、多くの例を見つけることができなかったので、コードについてはよくわかりません。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)