| 1 |
context('Test models with custom objective') |
| 2 |
|
| 3 |
set.seed(1994) |
| 4 |
|
| 5 |
n_threads <- 2 |
| 6 |
|
| 7 |
data(agaricus.train, package = 'xgboost') |
| 8 |
data(agaricus.test, package = 'xgboost') |
| 9 |
dtrain <- xgb.DMatrix( |
| 10 |
agaricus.train$data, label = agaricus.train$label, nthread = n_threads |
| 11 |
) |
| 12 |
dtest <- xgb.DMatrix( |
| 13 |
agaricus.test$data, label = agaricus.test$label, nthread = n_threads |
| 14 |
) |
| 15 |
evals <- list(eval = dtest, train = dtrain) |
| 16 |
|
| 17 |
logregobj <- function(preds, dtrain) { |
| 18 |
labels <- getinfo(dtrain, "label") |
| 19 |
preds <- 1 / (1 + exp(-preds)) |
| 20 |
grad <- preds - labels |
| 21 |
hess <- preds * (1 - preds) |
| 22 |
return(list(grad = grad, hess = hess)) |
| 23 |
} |
| 24 |
|
| 25 |
evalerror <- function(preds, dtrain) { |
| 26 |
labels <- getinfo(dtrain, "label") |
| 27 |
err <- as.numeric(sum(labels != (preds > 0.5))) / length(labels) |
| 28 |
return(list(metric = "error", value = err)) |
| 29 |
} |
| 30 |
|
| 31 |
param <- list(max_depth = 2, learning_rate = 1, nthread = n_threads, |
| 32 |
objective = logregobj, eval_metric = evalerror) |
| 33 |
num_round <- 2 |
| 34 |
|
| 35 |
test_that("custom objective works", { |
| 36 |
bst <- xgb.train(param, dtrain, num_round, evals, verbose = 0) |
| 37 |
expect_equal(class(bst), "xgb.Booster") |
| 38 |
expect_false(is.null(attributes(bst)$evaluation_log)) |
| 39 |
expect_false(is.null(attributes(bst)$evaluation_log$eval_error)) |
| 40 |
expect_lt(attributes(bst)$evaluation_log[num_round, eval_error], 0.03) |
| 41 |
}) |
| 42 |
|
| 43 |
test_that("custom objective in CV works", { |
| 44 |
cv <- xgb.cv(param, dtrain, num_round, nfold = 10, verbose = FALSE, stratified = FALSE) |
| 45 |
expect_false(is.null(cv$evaluation_log)) |
| 46 |
expect_equal(dim(cv$evaluation_log), c(2, 5)) |
| 47 |
expect_lt(cv$evaluation_log[num_round, test_error_mean], 0.03) |
| 48 |
}) |
| 49 |
|
| 50 |
test_that("custom objective with early stop works", { |
| 51 |
bst <- xgb.train(param, dtrain, 10, evals, verbose = 0) |
| 52 |
expect_equal(class(bst), "xgb.Booster") |
| 53 |
train_log <- attributes(bst)$evaluation_log$train_error |
| 54 |
expect_true(all(diff(train_log) <= 0)) |
| 55 |
}) |
| 56 |
|
| 57 |
test_that("custom objective using DMatrix attr works", { |
| 58 |
|
| 59 |
attr(dtrain, 'label') <- getinfo(dtrain, 'label') |
| 60 |
|
| 61 |
logregobjattr <- function(preds, dtrain) { |
| 62 |
labels <- attr(dtrain, 'label') |
| 63 |
preds <- 1 / (1 + exp(-preds)) |
| 64 |
grad <- preds - labels |
| 65 |
hess <- preds * (1 - preds) |
| 66 |
return(list(grad = grad, hess = hess)) |
| 67 |
} |
| 68 |
param$objective <- logregobjattr |
| 69 |
bst <- xgb.train(param, dtrain, num_round, evals, verbose = 0) |
| 70 |
expect_equal(class(bst), "xgb.Booster") |
| 71 |
}) |
| 72 |
|
| 73 |
test_that("custom objective with multi-class shape", { |
| 74 |
data <- as.matrix(iris[, -5]) |
| 75 |
label <- as.numeric(iris$Species) - 1 |
| 76 |
dtrain <- xgb.DMatrix(data = data, label = label, nthread = n_threads) |
| 77 |
n_classes <- 3 |
| 78 |
|
| 79 |
fake_softprob <- function(preds, dtrain) { |
| 80 |
mpreds <- as.matrix(preds) |
| 81 |
expect_equal( |
| 82 |
mpreds, |
| 83 |
matrix(0.5, nrow = nrow(mpreds), ncol = ncol(mpreds)), |
| 84 |
tolerance = 1e-4 |
| 85 |
) |
| 86 |
## use numeric vector here to test compatibility with XGBoost < 2.1 |
| 87 |
grad <- rnorm(length(as.matrix(preds))) |
| 88 |
expect_equal(dim(data)[1] * n_classes, dim(as.matrix(preds))[1] * n_classes) |
| 89 |
hess <- rnorm(length(as.matrix(preds))) |
| 90 |
list(grad = grad, hess = hess) |
| 91 |
} |
| 92 |
fake_merror <- function(preds, dtrain) { |
| 93 |
expect_equal(dim(data)[1] * n_classes, dim(as.matrix(preds))[1]) |
| 94 |
} |
| 95 |
param$objective <- fake_softprob |
| 96 |
param$eval_metric <- fake_merror |
| 97 |
expect_warning({ |
| 98 |
bst <- xgb.train(c(param, list(num_class = n_classes)), dtrain, nrounds = 1) |
| 99 |
}) |
| 100 |
}) |
| 101 |
|
| 102 |
softmax <- function(values) { |
| 103 |
values <- as.numeric(values) |
| 104 |
exps <- exp(values) |
| 105 |
den <- sum(exps) |
| 106 |
return(exps / den) |
| 107 |
} |
| 108 |
|
| 109 |
softprob <- function(predt, dtrain) { |
| 110 |
y <- getinfo(dtrain, "label") |
| 111 |
|
| 112 |
n_samples <- dim(predt)[1] |
| 113 |
n_classes <- dim(predt)[2] |
| 114 |
|
| 115 |
grad <- matrix(nrow = n_samples, ncol = n_classes) |
| 116 |
hess <- matrix(nrow = n_samples, ncol = n_classes) |
| 117 |
|
| 118 |
for (i in seq_len(n_samples)) { |
| 119 |
t <- y[i] |
| 120 |
p <- softmax(predt[i, ]) |
| 121 |
for (c in seq_len(n_classes)) { |
| 122 |
g <- if (c - 1 == t) { |
| 123 |
p[c] - 1.0 |
| 124 |
} else { |
| 125 |
p[c] |
| 126 |
} |
| 127 |
h <- max((2.0 * p[c] * (1.0 - p[c])), 1e-6) |
| 128 |
grad[i, c] <- g |
| 129 |
hess[i, c] <- h |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return(list(grad = grad, hess = hess)) |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
test_that("custom objective with multi-class works", { |
| 138 |
data <- as.matrix(iris[, -5]) |
| 139 |
label <- as.numeric(iris$Species) - 1 |
| 140 |
|
| 141 |
dtrain <- xgb.DMatrix(data = data, label = label, nthread = 1) |
| 142 |
|
| 143 |
param$num_class <- 3 |
| 144 |
param$objective <- softprob |
| 145 |
param$eval_metric <- "merror" |
| 146 |
param$base_score <- 0.5 |
| 147 |
|
| 148 |
custom_bst <- xgb.train(param, dtrain, 2) |
| 149 |
custom_predt <- predict(custom_bst, dtrain) |
| 150 |
|
| 151 |
param$objective <- "multi:softmax" |
| 152 |
builtin_bst <- xgb.train(param, dtrain, 2) |
| 153 |
builtin_predt <- predict(builtin_bst, dtrain) |
| 154 |
|
| 155 |
expect_equal(custom_predt, builtin_predt) |
| 156 |
}) |
| 157 |
|
| 158 |
test_that("custom metric with multi-target passes reshaped data to feval", { |
| 159 |
x <- as.matrix(iris[, -5]) |
| 160 |
y <- as.numeric(iris$Species) - 1 |
| 161 |
dtrain <- xgb.DMatrix(data = x, label = y, nthread = 1) |
| 162 |
|
| 163 |
multinomial.ll <- function(predt, dtrain) { |
| 164 |
expect_equal(dim(predt), c(nrow(iris), 3L)) |
| 165 |
y <- getinfo(dtrain, "label") |
| 166 |
probs <- apply(predt, 1, softmax) |> t() |
| 167 |
probs.y <- probs[cbind(seq(1L, nrow(predt)), y + 1L)] |
| 168 |
ll <- sum(log(probs.y)) |
| 169 |
return(list(metric = "multinomial-ll", value = -ll)) |
| 170 |
} |
| 171 |
|
| 172 |
model <- xgb.train( |
| 173 |
params = list( |
| 174 |
objective = "multi:softmax", |
| 175 |
num_class = 3L, |
| 176 |
base_score = 0, |
| 177 |
disable_default_eval_metric = TRUE, |
| 178 |
eval_metric = multinomial.ll, |
| 179 |
max_depth = 123, |
| 180 |
seed = 123 |
| 181 |
), |
| 182 |
data = dtrain, |
| 183 |
nrounds = 2L, |
| 184 |
evals = list(Train = dtrain), |
| 185 |
verbose = 0 |
| 186 |
) |
| 187 |
|
| 188 |
model <- xgb.train( |
| 189 |
params = list( |
| 190 |
objective = "multi:softmax", |
| 191 |
num_class = 3L, |
| 192 |
base_score = 0, |
| 193 |
disable_default_eval_metric = TRUE, |
| 194 |
max_depth = 123, |
| 195 |
seed = 123 |
| 196 |
), |
| 197 |
data = dtrain, |
| 198 |
nrounds = 2L, |
| 199 |
evals = list(Train = dtrain), |
| 200 |
custom_metric = multinomial.ll, |
| 201 |
verbose = 0 |
| 202 |
) |
| 203 |
}) |