xgb.wrap.loss: xgboost loss function wrapper

Description Usage Arguments Value Examples

View source: R/xgb.wrap.loss.R

Description

The wrapper works only if both the wrapper and the original loss metric are existing. Requires Matrix and xgboost packages.

Usage

1

Arguments

f

Type: function. The function to wrap from xgboost. Requires the following order of arguments for the function to work: preds, labels, and returns a vector of the same length of both the inputs.

Value

The wrapping function.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Note: this example unexpectedly fails when using pkgdown.

library(xgboost)
library(Matrix)
data(agaricus.train, package = "xgboost")
data(agaricus.test, package = "xgboost")

dtrain <- xgboost::xgb.DMatrix(agaricus.train$data, label = agaricus.train$label)
dtest <- xgboost::xgb.DMatrix(agaricus.test$data, label = agaricus.test$label)
watchlist <- list(train = dtrain, eval = dtest)

cross_entropy <- function(preds, labels) {
  preds <- 1 / (1 + exp(-preds))
  grad <- preds - labels
  hess <- preds * (1 - preds)
  return(list(grad = grad, hess = hess))
}
cross_entropy_wrap <- xgb.wrap.loss(f = cross_entropy)

param <- list(max_depth = 2, eta = 1, silent = 1, nthread = 1, 
              objective = cross_entropy_wrap, eval_metric = "auc")
bst <- xgboost::xgb.train(param, dtrain, nrounds = 2, watchlist)

# Note: this example unexpectedly fails when using pkgdown.

Laurae2/LauraeDS documentation built on May 29, 2019, 2:25 p.m.