首页 > 解决方案 > 客观论点在 python 的 xgboost 中是如何工作的?

问题描述

梯度提升树可以采用自定义目标函数。这很棒,因为对于我的特定任务,我有一个非常具体的损失函数,我可以计算关于预测的一阶和二阶导数。

需要明确的是,通常机器学习任务的目标函数通常定义为 obj = 损失 + 复杂度。

但是,我想保持由 给出的标准复杂性惩罚Omega(f) = gamma*T + (1/2)*lambda * \sum_{j=1}^T(w^2_j)不变。这里,T,gamma,lambda,w分别是树中叶子的数量、在树的叶子节点上进行进一步分区所需的最小损失减少、权重的 L2 正则化项和每个叶子的权重向量。

我的问题是,使用xgboost 的标准 python 实现,我可以只定义损失函数作为objective参数的输入,并让 xgboost 库处理复杂性项吗?这会很棒,因为我不想更改复杂度项或计算它的导数。

我的猜测是,不幸的是,我们还必须定义复杂度项,因为根据定义,目标函数是损失和复杂度的线性组合。

注意:如果我们查看 XGBClassifier 的注释

from xgboost import XGBClassifier
help(XGBClassifier)
...
|  objective : string or callable
|      Specify the learning task and the corresponding learning objective or
|      a custom objective function to be used (see note below).
...
...
|  Note
|  ----
|  A custom objective function can be provided for the ``objective``
|  parameter. In this case, it should have the signature
|  ``objective(y_true, y_pred) -> grad, hess``:
|  
|  y_true: array_like of shape [n_samples]
|      The target values
|  y_pred: array_like of shape [n_samples]
|      The predicted values
|  
|  grad: array_like of shape [n_samples]
|      The value of the gradient for each sample point.
|  hess: array_like of shape [n_samples]
|      The value of the second derivative for each sample point

我们看到自定义objective函数的唯一输入应该是y_predand y_true。所以我不明白,如果我们确实需要定义复杂性术语,复杂性参数 ( T,gamma,lambda,w) 将如何在自定义目标函数中定义。

标签: pythonmachine-learningxgboost

解决方案


推荐阅读