【问题标题】:Issue with Trials() when using Hyperopt?使用 Hyperopt 时出现 Trials() 问题?
【发布时间】:2019-07-22 16:31:19
【问题描述】:

我第一次尝试在 Python 中使用 Hyperopt 进行超参数调整。我已阅读文档并想在 XgBoost 分类器上尝试此操作。 "X_train" 和 "y_train" 是拆分成测试集和训练集后的数据框。到目前为止,这是我的代码:

#Hyperopt Parameter Tuning
from hyperopt import hp, STATUS_OK, Trials, fmin, tpe
from sklearn.model_selection import cross_val_score


def objective(space):
  print(space)
  clf = xgb.XGBClassifier(objective = space[objective],
                     max_depth = int(space[max_depth]),
                     learning_rate = space[learning_rate],
                     n_estimators = space[n_estimators])


  #eval_set = [(X_train, y_train), (Xcv, Ycv)]
  clf.fit(X_train, y_train, eval_metric='auc',
         early_stopping_rounds=10, verbose=False)

  #pred = clf.predict(X_test)
  auc = cross_val_score(clf, X_train, y_train, cv=3)
  return{'auc':auc, 'status': STATUS_OK }




space = {'booster': 'gbtree',
         'objective': 'binary:logistic',
         'eval': 'auc',
         'max_depth': hp.quniform('max_depth', 1, 100, 5),
         'learning_rate': hp.loguniform('learning_rate', 0.2, 0.3),
         'n_estimators': hp.quniform('n_esimators', 5, 500, 10)}


trials = Trials()
best = fmin(fn=objective,
            space=space,
            algo=tpe.suggest,
            max_evals=3, # change
            trials=trials)

print(best)

我收到以下突出显示“trails=trails”的错误:

TypeError: ap_loguniform_sampler() got multiple values for argument 'size'

我进行了一些研究,但未能找到解决此错误的方法。任何帮助都会很棒!

【问题讨论】:

    标签: python machine-learning hyperopt


    【解决方案1】:

    根据文档,hp.loguniform 只能接受 3 个参数,如下所示。

    hp.loguniform(label, low, high)
    

    这可能是错误的原因。请检查。

    【讨论】:

    • 感谢您的回复。试过这个,新的失败消息是: KeyError: 并且它突出显示了代码的相同部分。我在描述中对上述更改进行了编辑。
    • @rmahesh 您对上述问题有任何解决方案吗?
    猜你喜欢
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 2022-11-10
    • 2011-06-14
    • 2019-08-11
    • 2021-10-30
    • 1970-01-01
    相关资源
    最近更新 更多