2017-11-02 1 views
0

私自身のカスタム損失機能を追加する際に、どのファイルを変更する必要がありますか? ObjectiveFunctionに目的関数と勾配/ヘッセ演算を追加できることは知っています。必要なことが他にあるかどうか、またはカスタム損失関数の他の選択肢があるかどうか疑問に思っています。私のカスタムロスト機能のためにlightgbmを変更するには?

答えて

0

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) 
関連する問題