This repository was archived by the owner on Jul 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmlr3learners_lightgbm_early_stopping.Rmd
144 lines (121 loc) · 2.89 KB
/
mlr3learners_lightgbm_early_stopping.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
---
title: "mlr3learners.lightgbm: Early Stopping Example"
date: "`r Sys.Date()`"
output:
rmarkdown::html_vignette:
keep_md: true
vignette: >
%\VignetteIndexEntry{mlr3learners_lightgbm_early_stopping}
%\VignetteEncoding{UTF-8}
%\VignetteEngine{knitr::rmarkdown}
editor_options:
chunk_output_type: console
---
```{r include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
eval = FALSE
)
```
```{r setup}
library(mlr3)
library(mlr3learners.lightgbm)
library(paradox)
library(mlbench)
```
# Load the dataset
```{r}
data("PimaIndiansDiabetes2")
dataset = data.table::as.data.table(PimaIndiansDiabetes2)
target_col = "diabetes"
dataset = backend_preprocessing(
datatable = dataset,
target_col = target_col,
task_type = "class:binary",
positive = "pos"
)
task = mlr3::TaskClassif$new(
id = "pima",
backend = dataset,
target = target_col,
positive = "1"
)
```
```{r}
set.seed(17)
split = list(
train_index = sample(seq_len(task$nrow), size = 0.7 * task$nrow)
)
split$test_index = setdiff(seq_len(task$nrow), split$train_index)
```
# Early stopping using the internal lightgbm implementation
```{r message=F, error=F, warning=F}
learner = mlr3::lrn("classif.lightgbm", objective = "binary")
# define parameters
learner$param_set$values = mlr3misc::insert_named(
learner$param_set$values,
list(
"early_stopping_round" = 10,
"learning_rate" = 0.1,
"seed" = 17L,
"metric" = "auc",
"num_iterations" = 100
)
)
system.time(
learner$train(task, row_ids = split$train_index)
)
learner$model$current_iter()
```
# Early stopping using the mlr3tuning implementation
```{r message=F, error=F, warning=F}
learner = mlr3::lrn("classif.lightgbm")
# define parameters
learner$param_set$values = mlr3misc::insert_named(
learner$param_set$values,
list(
"objective" = "binary",
"nrounds_by_cv" = FALSE,
"learning_rate" = 0.1,
"seed" = 17L,
"metric" = "auc",
"num_threads" = 1
)
)
# define num_iterations as tuning parameter
tune_ps = ParamSet$new(list(
ParamDbl$new("num_iterations", lower = 1L, upper = 100L)
))
# design_points
design = paradox::generate_design_grid(
tune_ps,
param_resolutions = c(
num_iterations = 100
))
# Create the resampling strategy and the measure
resampling = mlr3::rsmp("cv", folds = 5)
measure = mlr3::msr("classif.auc")
# Create the tuner
tuner = mlr3tuning::tnr("design_points", design = design$data, batch_size = 1)
# Create the terminator
terminator = mlr3tuning::term("stagnation", iters = 10)
# Instantiate the AutoTuner instance
at = mlr3tuning::AutoTuner$new(
learner = learner,
resampling = resampling,
measures = measure,
tune_ps = tune_ps,
terminator = terminator,
tuner = tuner
)
at
# Train the tuner
future::plan("multisession")
set.seed(17)
system.time(
at$train(task, row_ids = split$train_index)
)
future::plan("sequential")
at$learner$model$current_iter()
```