XGBoost中param中设置的'objective'和train函数中的obj参数有什么区别?

In XGBoost, what's the difference between the 'objective' set in param and the obj parameter in the train function?

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

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 函数。

很奇怪,我发现如果 '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' 并且只设置火车中的 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)

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

的可调用函数

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