early_stopping.R 1.82 KB
Newer Older
1
2
library(lightgbm)
library(methods)
3
4
5
6
7

# Load in the agaricus dataset
data(agaricus.train, package = "lightgbm")
data(agaricus.test, package = "lightgbm")

Guolin Ke's avatar
Guolin Ke committed
8
dtrain <- lgb.Dataset(agaricus.train$data, label = agaricus.train$label)
9
dtest <- lgb.Dataset.create.valid(dtrain, data = agaricus.test$data, label = agaricus.test$label)
10
11
12
13

# Note: for customized objective function, we leave objective as default
# Note: what we are getting is margin value in prediction
# You must know what you are doing
14
param <- list(
15
16
  num_leaves = 4L
  , learning_rate = 1.0
17
)
Guolin Ke's avatar
Guolin Ke committed
18
valids <- list(eval = dtest)
19
num_round <- 20L
20
21
22

# User define objective function, given prediction, return gradient and second order gradient
# This is loglikelihood loss
Guolin Ke's avatar
Guolin Ke committed
23
24
logregobj <- function(preds, dtrain) {
  labels <- getinfo(dtrain, "label")
25
  preds <- 1.0 / (1.0 + exp(-preds))
Guolin Ke's avatar
Guolin Ke committed
26
  grad <- preds - labels
27
  hess <- preds * (1.0 - preds)
Guolin Ke's avatar
Guolin Ke committed
28
29
  return(list(grad = grad, hess = hess))
}
30

31
# User-defined evaluation function returns a pair (metric_name, result, higher_better)
Guolin Ke's avatar
Guolin Ke committed
32
# NOTE: when you do customized loss function, the default prediction value is margin
33
# This may make built-in evalution metric calculate wrong results
34
# For example, we are doing logistic loss, the prediction is score before logistic transformation
35
36
# The built-in evaluation error assumes input is after logistic transformation
# Keep this in mind when you use the customization, and maybe you need write customized evaluation function
Guolin Ke's avatar
Guolin Ke committed
37
38
evalerror <- function(preds, dtrain) {
  labels <- getinfo(dtrain, "label")
39
  err <- as.numeric(sum(labels != (preds > 0.5))) / length(labels)
40
  return(list(name = "error", value = err, higher_better = FALSE))
Guolin Ke's avatar
Guolin Ke committed
41
}
42
print("Start training with early Stopping setting")
Guolin Ke's avatar
Guolin Ke committed
43

44
45
46
47
48
49
50
bst <- lgb.train(
  param
  , dtrain
  , num_round
  , valids
  , objective = logregobj
  , eval = evalerror
51
  , early_stopping_round = 3L
52
)