python - 损失的不对称函数
问题描述
我正在使用 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)
我该如何修改它以达到我的目的?
解决方案
我相信这个功能是你想要做出改变的地方。
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)
推荐阅读
- angular - 当我展开一个标签部分时,所有克拉箭头都朝上。只应显示选定的箭头
- ios - 如何在swift中添加长文本阴影?
- python-3.x - 无法使用熊猫键入 timedelta?
- r - #R - 使用 R 将季度数据拆分为月度数据
- django - Django - 请求解析
- django - 一个特定路由的 uwsgi 分段错误
- codenameone - 文本与 textArea 中的提示重叠 - cn1
- android - 互联网丢失后 Firestore Android 重新连接时间太长(约 10 分钟)
- node.js - nodeJS中的条件http获取请求
- java - 单击时消失的 Java 文本字段背景文本