xgboost的github仓库中自定义目标函数示例脚本中如何计算对数损失的梯度和黑塞矩阵?

我想了解xgboost示例脚本中对数损失函数的梯度和黑塞矩阵是如何计算的,该脚本可以在这里找到。

我已经简化了函数以接受numpy数组,并生成了y_haty_true,它们是脚本中使用的值的样本。

这是一个简化的示例:

import numpy as npdef loglikelihoodloss(y_hat, y_true):    prob = 1.0 / (1.0 + np.exp(-y_hat))    grad = prob - y_true    hess = prob * (1.0 - prob)    return grad, hessy_hat = np.array([1.80087972, -1.82414818, -1.82414818,  1.80087972, -2.08465433,                  -1.82414818, -1.82414818,  1.80087972, -1.82414818, -1.82414818])y_true = np.array([1.,  0.,  0.,  1.,  0.,  0.,  0.,  1.,  0.,  0.])loglikelihoodloss(y_hat, y_true)

对数损失函数是enter image description here的总和,其中enter image description here

梯度(相对于p)是enter image description here,然而在代码中是enter image description here

同样,二阶导数(相对于p)是enter image description here,然而在代码中是enter image description here

这些方程是如何相等的?


回答:

对数损失函数给出如下:

enter image description here

其中

enter image description here

通过求偏导数,我们得到梯度为

enter image description here

因此我们得到梯度的负值为p-y

可以进行类似的计算来获得黑塞矩阵。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注