首页 > 解决方案 > 损失的不对称函数

问题描述

我正在使用 LightGBM,我需要实现一个损失函数,当预测低于目标值时,它会在训练期间给予惩罚。换句话说,我认为低估比高估要糟糕得多。我发现这个建议正好相反:

def custom_asymmetric_train(y_true, y_pred):
    residual = (y_true - y_pred).astype("float")
    grad = np.where(residual<0, -2*10.0*residual, -2*residual)
    hess = np.where(residual<0, 2*10.0, 2.0)
    return grad, hess
def custom_asymmetric_valid(y_true, y_pred):
    residual = (y_true - y_pred).astype("float")
    loss = np.where(residual < 0, (residual**2)*10.0, residual**2) 
    return "custom_asymmetric_eval", np.mean(loss), False

https://towardsdatascience.com/custom-loss-functions-for-gradient-boosting-f79c1b40466d

我该如何修改它以达到我的目的?

标签: pythonmachine-learningxgboostloss-functionlightgbm

解决方案


我相信这个功能是你想要做出改变的地方。

def custom_asymmetric_valid(y_true, y_pred):
     residual = (y_true - y_pred).astype("float")
     loss = np.where(residual < 0, (residual**2)*10.0, residual**2) 
     return "custom_asymmetric_eval", np.mean(loss), False

计算损失的线有一个比较。

loss = np.where(residual < 0, (residual**2)*10.0, residual**2) 

当残差小于 0 时,损失为残差^2 * 10,其中大约为 0 时,损失只是重新分配^2。

因此,如果我们将这个小于改为大于。这将翻转倾斜。

loss = np.where(residual > 0, (residual**2)*10.0, residual**2) 

推荐阅读