Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为自定义损耗函数修改lightgbm?_Lightgbm - Fatal编程技术网

如何为自定义损耗函数修改lightgbm?

如何为自定义损耗函数修改lightgbm?,lightgbm,Lightgbm,添加自定义丢失功能时应更改哪些文件?我知道我可以在ObjectiveFunction中添加我的objective和gradient/hessian计算,只是想知道我是否还需要做其他事情,或者是否有其他自定义损失函数的替代方案。根据中的演示文件 将目标函数设置为: # User define objective function, given prediction, return gradient and second order gradient # This is loglikelihood

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

根据中的演示文件

将目标函数设置为:

# 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)