【问题标题】:hyperopt result exceeds my hp.choice restriction, why? (XGBoost)hyperopt 结果超出了我的 hp.choice 限制,为什么? (XGBoost)
【发布时间】:2019-03-04 07:14:49
【问题描述】:

我遇到了一个奇怪的问题:
我通过 hyperopt 定义了我的 XGB 超参数 'max_depth'

hp.choice('max_depth',range(2,20))

但我得到了'max_depth' = 01 的结果,这不在[2,20) 的限制范围内。为什么? 我错过了什么? 谢谢。

错误结果:

{'colsample_bytree': 0.18, 'learning_rate': 0.05, 'max_depth': 1, 'reg_alpha': 3.44, 'reg_lambda': 0.92}

{'colsample_bytree': 0.41, 'learning_rate': 0.09, 'max_depth': 0, 'reg_alpha': 0.14, 'reg_lambda': 3.53}

{'colsample_bytree': 0.71, 'learning_rate': 0.17, 'max_depth': 0, 'reg_alpha': 2.21, 'reg_lambda': 2.82}
def xgb_classifier_tune(params):
    obj='binary:logistic' if class_nums==2 else 'multi:softmax'
    random.seed(time.time())
    xgb_model=xgb.XGBClassifier(
            max_depth=params['max_depth'],
            colsample_bytree=params['colsample_bytree'],
            learning_rate=params['learning_rate'],
            reg_alpha=params['reg_alpha'],
            reg_lambda=params['reg_lambda'],
            objective=obj,
            n_estimators=100000,
            random_state=random.randint(0,99999),
            n_jobs=-1)

    if params['max_depth']<2:
        return {'loss':999.999,'status': STATUS_FAIL,'info':[0,0,0,{}]}
    xgb_model.fit(tune_train_x,tune_train_y,eval_set=[(tune_valid_x,tune_valid_y)],verbose=1,early_stopping_rounds=100) #verbose: 0 (silent), 1 (warning), 2 (info), 3 (debug)
    predict_y=xgb_model.predict(tune_valid_x)
    f1,mcc,roc_auc,table=get_score(tune_valid_y[y_feature].values,predict_y)
    return 'loss':-mcc,'status': STATUS_OK

def xgb_hyper_tune():
    mdep=list(range(2,20))
    space={'max_depth':hp.choice('max_depth',mdep),
        'colsample_bytree':hp.uniform('colsample_bytree',0.1,0.9),
        'learning_rate':hp.quniform('learning_rate',0.01,0.2,0.01),
        'reg_alpha':hp.uniform('reg_alpha',0.1,6.0),
        'reg_lambda':hp.uniform('reg_lambda',0.1,6.0)}

    trials=Trials()
    best_param=fmin(xgb_classifier_tune,space,algo=tpe.suggest,max_evals=100, trials=trials)
    return best_param

【问题讨论】:

    标签: xgboost hyperopt


    【解决方案1】:

    因为hp.choice 将返回索引而不是限制项的值。例如0表示max_depth的值为2。

    【讨论】:

      【解决方案2】:

      我也遇到了同样的问题。这不是错误。

      hyperoptofficial document来看,是不能理解的。但是 zilin xiang 的 the answerthis ncalik 的非常好的解释帮助我理解了出现问题的原因。 This帮我解决了问题。

      出现问题的原因

      这是由于使用了hp.choice() 而不是在fmin() 中设置return_argmin=False。由于您设置了max_depth [2,20),因此获取索引 0 或 1 意味着它正在使用 max_depth 2(用于索引 0)或 3(用于索引 1)。

      解决问题

      如果要获取实际参数,而不是索引,则可以使用两种方法。

      1. 使用hyperopt.space_eval():hyperopt.space_eval(space, best_param)
      2. fmin() 中设置return_argmin=Falsebest_param=fmin(xgb_classifier_tune,space,algo=tpe.suggest,max_evals=100, trials=trials, return_argmin=False)。它会返回参数的实际值,而不是索引。

      想了解更多,可以查看this

      【讨论】:

        猜你喜欢
        • 2020-05-09
        • 2022-01-06
        • 2022-01-14
        • 2020-09-10
        • 1970-01-01
        • 2021-09-24
        • 1970-01-01
        • 2016-06-27
        • 1970-01-01
        相关资源
        最近更新 更多