【问题标题】:In XGBoost, what's the difference between the 'objective' set in param and the obj parameter in the train function?在 XGBoost 中,param 中设置的“目标”与 train 函数中的 obj 参数有什么区别?
【发布时间】:2018-03-15 13:47:36
【问题描述】:

使用 XGBoost 包时,我们可以在 param dict 中设置 'objective'(例如:'objective': 'binary:logistic' )并将 dict 传递给 train 函数。 同时,train函数中有一个obj参数。据我所知,它们都是目标函数。那么他们有什么区别呢?如果两者都设置了,哪一个会生效?

param = {'max_depth': 3, 'eta': 1, 'silent': 1, 'objective': 'binary:logistic'} 
bst = xgb.train(param, data_train, num_boost_round=n_round, obj=g_h, feval=error_rate)

其中 g_h 是自定义的目标函数。

很奇怪,我发现如果 'objective': 'binary:logistic' 和 obj 都设置了,y_hat 是

y_hat: [6.0993789e-06 9.8472750e-01 6.0993789e-06 ... 9.9993265e-01 4.4560062e-07
 9.9993265e-01]

如果我跳过 'objective': 'binary:logistic' 并且只设置了 train 中的 obj,则 y_hat 是

y_hat: [-5.6174016  5.2989674 -5.6174016 ...  7.6525593 -6.4794073  6.7979865]

所以 train 函数中的 obj 不会覆盖 'objective': 'binary:logistic'!

这是代码:

import xgboost as xgb

def g_h(y_hat, y):
    p = 1.0 / (1.0 + np.exp(-y_hat))
    g = p - y.get_label()
    h = p * (1.0-p)
    return g, h

# read in data
dtrain = xgb.DMatrix('demo/data/agaricus.txt.train')
dtest = xgb.DMatrix('demo/data/agaricus.txt.test')
# specify parameters via map
param = {'max_depth':3, 'eta':1, 'silent':1, 'objective':'binary:logistic' }
num_round = 7
bst = xgb.train(param, dtrain, num_round)
# make prediction
y_hat = bst.predict(dtest)
print(y_hat)

【问题讨论】:

    标签: python machine-learning xgboost


    【解决方案1】:

    最初objective 构造函数参数仅支持定义已知目标的string 值,例如您的示例中的目标。另一方面,obj 参数需要一个带有签名 objective(y_true, y_pred) -> grad, hess 的可调用对象

    现在(至少在 v0.7 中)两者都可以是自定义的可调用对象。对于用户来说,这意味着您可以使用任何一种方式来定义目标,您传递给 train 的方式将覆盖构造函数中定义的方式。我最好的猜测是,为了向后兼容,两种定义它的方式仍然存在。

    【讨论】:

    • 我做了一些实验,发现 train 函数中的 obj 并没有覆盖 'objective': 'binary:logistic'。多么奇怪!
    猜你喜欢
    • 2018-12-22
    • 2017-03-07
    • 2015-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    • 2017-03-02
    • 1970-01-01
    相关资源
    最近更新 更多