-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtest-hyperparameterTuning.R
More file actions
164 lines (135 loc) · 4.95 KB
/
test-hyperparameterTuning.R
File metadata and controls
164 lines (135 loc) · 4.95 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
context('Hyperparameter Tuning')
testthat::test_that(
"xgboost"
, {
skip_on_cran()
library("xgboost")
set.seed(0)
data(agaricus.train, package = "xgboost")
Folds <- list(
Fold1 = as.integer(seq(1,nrow(agaricus.train$data),by = 3))
, Fold2 = as.integer(seq(2,nrow(agaricus.train$data),by = 3))
, Fold3 = as.integer(seq(3,nrow(agaricus.train$data),by = 3))
)
scoringFunction <- function(max_depth, max_leaves,
min_child_weight, subsample,
colsample_bytree, gamma, lambda, alpha,
.debug = FALSE) {
# ---- Type coercion & scalarization ----
max_depth <- as.integer(max_depth)[1]
max_leaves <- as.integer(max_leaves)[1]
min_child_weight <- as.numeric(min_child_weight)[1]
subsample <- as.numeric(subsample)[1]
colsample_bytree <- as.numeric(colsample_bytree)[1]
gamma <- as.numeric(gamma)[1]
lambda <- as.numeric(lambda)[1]
alpha <- as.numeric(alpha)[1]
# ---- Data (assumes 'agaricus.train' and 'Folds' exist) ----
dtrain <- xgboost::xgb.DMatrix(
data = agaricus.train$data,
label = agaricus.train$label
)
# Base params
Pars <- list(
booster = "gbtree",
eta = 0.01,
max_depth = max_depth,
min_child_weight = min_child_weight,
subsample = subsample,
colsample_bytree = colsample_bytree,
gamma = gamma,
lambda = lambda, # L2 reg
alpha = alpha, # L1 reg
objective = "binary:logistic",
eval_metric = "auc"
)
# If max_leaves is requested, enable histogram/lossguide (so xgboost uses it)
if (!is.na(max_leaves) && max_leaves > 0L) {
Pars$tree_method <- "hist"
Pars$grow_policy <- "lossguide"
Pars$max_leaves <- max_leaves
# It's common to leave max_depth as-is; alternatively set max_depth = 0
# Pars$max_depth <- 0L
}
# ---- Safe CV wrapper ----
xgbcv <- try(
xgboost::xgb.cv(
params = Pars,
data = dtrain,
nrounds = 100,
folds = Folds,
prediction = FALSE,
showsd = TRUE,
early_stopping_rounds = 5,
maximize = TRUE,
verbose = 0
),
silent = TRUE
)
# On error: return worst score but keep scalars so bayesOpt can proceed
if (inherits(xgbcv, "try-error")) {
if (isTRUE(.debug)) message("xgb.cv error: ", as.character(xgbcv))
return(list(Score = as.numeric(-Inf), BestNrounds = as.integer(1L)))
}
# ---- Scalar Score ----
score_vec <- as.numeric(xgbcv$evaluation_log$test_auc_mean)
if (!is.null(names(score_vec))) names(score_vec) <- NULL
Score <- as.numeric(max(score_vec, na.rm = TRUE))[1]
# ---- Scalar best nrounds ----
bi <- xgbcv$best_iteration
if (is.null(bi) || length(bi) != 1L || is.na(bi)) {
bi <- which.max(score_vec)
if (length(bi) != 1L || is.na(bi)) bi <- 1L
}
BestNrounds <- as.integer(bi)[1]
if (isTRUE(.debug)) {
cat(sprintf(
"DEBUG | Score len=%d val=%.6f | BestNrounds len=%d val=%d\n",
length(Score), Score, length(BestNrounds), BestNrounds
))
}
list(
Score = Score, # must be scalar
BestNrounds = BestNrounds # must be scalar
)
}
bounds <- list(
max_depth = c(1L, 5L)
, max_leaves = c(2L,25L)
, min_child_weight = c(0, 25)
, subsample = c(0.25, 1)
, colsample_bytree = c(0.1,1)
, gamma = c(0,1)
, lambda = c(0,1)
, alpha = c(0,1)
)
initGrid <- data.table(
max_depth = c(1,1,2,2,3,3,4,4,5)
, max_leaves = c(2,3,4,5,6,7,8,9,10)
, min_child_weight = seq(bounds$min_child_weight[1],bounds$min_child_weight[2],length.out = 9)
, subsample = seq(bounds$subsample[1],bounds$subsample[2],length.out = 9)
, colsample_bytree = seq(bounds$colsample_bytree[1],bounds$colsample_bytree[2],length.out = 9)
, gamma = seq(bounds$gamma[1],bounds$gamma[2],length.out = 9)
, lambda = seq(bounds$lambda[1],bounds$lambda[2],length.out = 9)
, alpha = seq(bounds$alpha[1],bounds$alpha[2],length.out = 9)
)
optObj <- bayesOpt(
FUN = scoringFunction
, bounds = bounds
, initPoints = 9
, iters.n = 4
, iters.k = 1
, gsPoints = 10
)
expect_equal(nrow(optObj$scoreSummary),13)
optObj <- bayesOpt(
FUN = scoringFunction
, bounds = bounds
, initGrid = initGrid
, iters.n = 4
, iters.k = 1
, gsPoints = 10
)
expect_equal(nrow(optObj$scoreSummary),13)
}
)