添加我自己的自定义损失函数时应该更改哪些文件?我知道我可以在 ObjectiveFunction 中添加我的目标和梯度/粗麻布计算,只是想知道是否还有其他需要做的事情,或者是否有其他替代方法可以用于自定义损失函数。
问问题
8114 次
1 回答
3
根据lightGBM 早期停止示例中的演示文件,
设定目标函数为:
# 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)
于 2017-12-16T03:23:31.087 回答