3

我有一个机器学习问题,我相信负二项式损失函数很适合,但是轻量级 gbm 包没有它作为标准,我正在尝试实现它,但我不知道如何得到 Gradient 和 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

任何人都可以帮忙吗?

4

1 回答 1

2

这可以通过 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
于 2020-04-25T15:18:52.393 回答