如何为自定义损耗函数修改lightgbm?
添加自定义丢失功能时应更改哪些文件?我知道我可以在ObjectiveFunction中添加我的objective和gradient/hessian计算,只是想知道我是否还需要做其他事情,或者是否有其他自定义损失函数的替代方案。根据中的演示文件 将目标函数设置为:如何为自定义损耗函数修改lightgbm?,lightgbm,Lightgbm,添加自定义丢失功能时应更改哪些文件?我知道我可以在ObjectiveFunction中添加我的objective和gradient/hessian计算,只是想知道我是否还需要做其他事情,或者是否有其他自定义损失函数的替代方案。根据中的演示文件 将目标函数设置为: # User define objective function, given prediction, return gradient and second order gradient # This is loglikelihood
# 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))
}
#用户定义目标函数,给定预测、回报梯度和二阶梯度
#这是损失的可能性
logregobj
bst <- lgb.train(param,
dtrain,
num_round,
valids,
objective = logregobj,
eval = evalerror,
early_stopping_round = 3)