Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中实现负二项式损失函数以用于轻型GBM?_Python_Machine Learning_Gradient_Lightgbm_Hessian Matrix - Fatal编程技术网

如何在python中实现负二项式损失函数以用于轻型GBM?

如何在python中实现负二项式损失函数以用于轻型GBM?,python,machine-learning,gradient,lightgbm,hessian-matrix,Python,Machine Learning,Gradient,Lightgbm,Hessian Matrix,我有一个机器学习的问题,我相信负二项损失函数会很好地适合,但是light gbm软件包没有它作为标准,我正在尝试实现它,但我不知道如何获得梯度和Hessian,有人知道我如何做到这一点吗?我设法得到了损失函数,但我不能得到梯度和hessian import math def custom_asymmetric_valid(y_pred,y_true): y_true = y_true.get_label() p = 0.5 n = y_pred loss = m

我有一个机器学习的问题,我相信负二项损失函数会很好地适合,但是light gbm软件包没有它作为标准,我正在尝试实现它,但我不知道如何获得梯度和Hessian,有人知道我如何做到这一点吗?我设法得到了损失函数,但我不能得到梯度和hessian

import math

def custom_asymmetric_valid(y_pred,y_true):
    y_true = y_true.get_label()
    p = 0.5
    n = y_pred
    loss = math.gamma(n) + math.gamma(y_true + 1) - math.gamma(n + y_true) - n * math.log(p) - y_true * math.log(1 - p)
    return "custom_asymmetric_eval", np.mean(loss), False

现在如何得到梯度和Hessian

def custom_asymmetric_train(y_pred,y_true):
    residual = (y_true.get_label() - y_pred).astype("float")

    grad = ?
    hess = ?

    return grad, hess


任何人都可以提供帮助?

这可以通过scipy自动实现:

from scipy.misc import derivative
from scipy.special import gamma

def custom_asymmetric_train(y_pred, dtrain):

    y_true = dtrain.label
    p = 0.5

    def loss(x,t):
        loss = gamma(x) + gamma(t+1) - gamma(x+t) - x*np.log(p) - t*np.log(1-p)
        return loss

    partial_d = lambda x: loss(x, y_true)
    grad = derivative(partial_d, y_pred, n=1, dx=1e-6)
    hess = derivative(partial_d, y_pred, n=2, dx=1e-6)

    return grad, hess