test_custom_objective.R 2.96 KB
Newer Older
1
2
data(agaricus.train, package = "lightgbm")
data(agaricus.test, package = "lightgbm")
Guolin Ke's avatar
Guolin Ke committed
3
4
5
6
7
dtrain <- lgb.Dataset(agaricus.train$data, label = agaricus.train$label)
dtest <- lgb.Dataset(agaricus.test$data, label = agaricus.test$label)
watchlist <- list(eval = dtest, train = dtrain)

logregobj <- function(preds, dtrain) {
8
  labels <- get_field(dtrain, "label")
9
  preds <- 1.0 / (1.0 + exp(-preds))
Guolin Ke's avatar
Guolin Ke committed
10
  grad <- preds - labels
11
  hess <- preds * (1.0 - preds)
Guolin Ke's avatar
Guolin Ke committed
12
13
14
  return(list(grad = grad, hess = hess))
}

15
16
17
18
# User-defined evaluation function returns a pair (metric_name, result, higher_better)
# NOTE: when you do customized loss function, the default prediction value is margin
# This may make built-in evalution metric calculate wrong results
# Keep this in mind when you use the customization, and maybe you need write customized evaluation function
Guolin Ke's avatar
Guolin Ke committed
19
evalerror <- function(preds, dtrain) {
20
  labels <- get_field(dtrain, "label")
21
22
  preds <- 1.0 / (1.0 + exp(-preds))
  err <- as.numeric(sum(labels != (preds > 0.5))) / length(labels)
23
24
25
26
27
  return(list(
    name = "error"
    , value = err
    , higher_better = FALSE
  ))
Guolin Ke's avatar
Guolin Ke committed
28
29
}

30
param <- list(
31
32
  num_leaves = 8L
  , learning_rate = 1.0
33
34
  , objective = logregobj
  , metric = "auc"
35
  , verbose = .LGB_VERBOSITY
36
  , num_threads = .LGB_MAX_THREADS
37
)
38
num_round <- 10L
Guolin Ke's avatar
Guolin Ke committed
39
40

test_that("custom objective works", {
41
  bst <- lgb.train(param, dtrain, num_round, watchlist, eval = evalerror)
Guolin Ke's avatar
Guolin Ke committed
42
43
  expect_false(is.null(bst$record_evals))
})
44
45
46
47
48
49
50

test_that("using a custom objective, custom eval, and no other metrics works", {
  set.seed(708L)
  bst <- lgb.train(
    params = list(
      num_leaves = 8L
      , learning_rate = 1.0
51
      , verbose = .LGB_VERBOSITY
52
      , num_threads = .LGB_MAX_THREADS
53
54
55
56
57
58
59
60
61
    )
    , data = dtrain
    , nrounds = 4L
    , valids = watchlist
    , obj = logregobj
    , eval = evalerror
  )
  expect_false(is.null(bst$record_evals))
  expect_equal(bst$best_iter, 4L)
62
  expect_true(abs(bst$best_score - 0.000621) < .LGB_NUMERIC_TOLERANCE)
63
64
65

  eval_results <- bst$eval_valid(feval = evalerror)[[1L]]
  expect_true(eval_results[["data_name"]] == "eval")
66
  expect_true(abs(eval_results[["value"]] - 0.0006207325) < .LGB_NUMERIC_TOLERANCE)
67
68
69
  expect_true(eval_results[["name"]] == "error")
  expect_false(eval_results[["higher_better"]])
})
70
71
72
73
74
75
76
77

test_that("using a custom objective that returns wrong shape grad or hess raises an informative error", {
  bad_grad <- function(preds, dtrain) {
    return(list(grad = numeric(0L), hess = rep(1.0, length(preds))))
  }
  bad_hess <- function(preds, dtrain) {
    return(list(grad = rep(1.0, length(preds)), hess = numeric(0L)))
  }
78
  params <- list(num_leaves = 3L, verbose = .LGB_VERBOSITY)
79
80
81
82
83
84
85
  expect_error({
    lgb.train(params = params, data = dtrain, obj = bad_grad)
  }, sprintf("Expected custom objective function to return grad with length %d, got 0.", nrow(dtrain)))
  expect_error({
    lgb.train(params = params, data = dtrain, obj = bad_hess)
  }, sprintf("Expected custom objective function to return hess with length %d, got 0.", nrow(dtrain)))
})