gpt4 book ai didi

lightgbm - 如何为我的自定义损失函数修改 lightgbm?

转载 作者:行者123 更新时间:2023-12-04 14:25:14 84 4
gpt4 key购买 nike

添加我自己的自定义损失函数时应该更改哪些文件?我知道我可以在 ObjectiveFunction 中添加我的目标和梯度/hessian 计算,只是想知道是否还有其他我需要做的事情,或者自定义损失函数是否有其他替代方案。

最佳答案

根据 lightGBM early stopping example 中的演示文件,

将目标函数设置为:

# User define objective function, given prediction, return gradient and second order gradient
# This is loglikelihood loss
logregobj <- function(preds, dtrain) {
labels <- getinfo(dtrain, "label")
preds <- 1 / (1 + exp(-preds))
grad <- preds - labels
hess <- preds * (1 - preds)
return(list(grad = grad, hess = hess))
}

设置误差函数为:

# User defined evaluation function, return a pair metric_name, result, higher_better
# NOTE: when you do customized loss function, the default prediction value is margin
# This may make buildin evalution metric not function properly
# For example, we are doing logistic loss, the prediction is score before logistic transformation
# The buildin evaluation error assumes input is after logistic transformation
# Take this in mind when you use the customization, and maybe you need write customized evaluation function
evalerror <- function(preds, dtrain) {
labels <- getinfo(dtrain, "label")
err <- as.numeric(sum(labels != (preds > 0.5))) / length(labels)
return(list(name = "error", value = err, higher_better = FALSE))
}

然后你可以运行 lightgbm 为:

bst <- lgb.train(param,
dtrain,
num_round,
valids,
objective = logregobj,
eval = evalerror,
early_stopping_round = 3)

关于lightgbm - 如何为我的自定义损失函数修改 lightgbm?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47067431/

84 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com