[R] Set number of threads in demos and tests. (#9591)
- Restrict the number of threads in IO. - Specify the number of threads in demos and tests. - Add helper scripts for checks.
This commit is contained in:
@@ -1,23 +1,28 @@
|
||||
context("basic functions")
|
||||
|
||||
data(agaricus.train, package = 'xgboost')
|
||||
data(agaricus.test, package = 'xgboost')
|
||||
data(agaricus.train, package = "xgboost")
|
||||
data(agaricus.test, package = "xgboost")
|
||||
train <- agaricus.train
|
||||
test <- agaricus.test
|
||||
set.seed(1994)
|
||||
|
||||
# disable some tests for Win32
|
||||
windows_flag <- .Platform$OS.type == "windows" &&
|
||||
.Machine$sizeof.pointer != 8
|
||||
solaris_flag <- (Sys.info()['sysname'] == "SunOS")
|
||||
.Machine$sizeof.pointer != 8
|
||||
solaris_flag <- (Sys.info()["sysname"] == "SunOS")
|
||||
n_threads <- 1
|
||||
|
||||
|
||||
test_that("train and predict binary classification", {
|
||||
nrounds <- 2
|
||||
expect_output(
|
||||
bst <- xgboost(data = train$data, label = train$label, max_depth = 2,
|
||||
eta = 1, nthread = 2, nrounds = nrounds, objective = "binary:logistic",
|
||||
eval_metric = "error")
|
||||
, "train-error")
|
||||
bst <- xgboost(
|
||||
data = train$data, label = train$label, max_depth = 2,
|
||||
eta = 1, nthread = n_threads, nrounds = nrounds,
|
||||
objective = "binary:logistic", eval_metric = "error"
|
||||
),
|
||||
"train-error"
|
||||
)
|
||||
expect_equal(class(bst), "xgb.Booster")
|
||||
expect_equal(bst$niter, nrounds)
|
||||
expect_false(is.null(bst$evaluation_log))
|
||||
@@ -46,26 +51,39 @@ test_that("parameter validation works", {
|
||||
d <- cbind(
|
||||
x1 = rnorm(10),
|
||||
x2 = rnorm(10),
|
||||
x3 = rnorm(10))
|
||||
x3 = rnorm(10)
|
||||
)
|
||||
y <- d[, "x1"] + d[, "x2"]^2 +
|
||||
ifelse(d[, "x3"] > .5, d[, "x3"]^2, 2^d[, "x3"]) +
|
||||
rnorm(10)
|
||||
dtrain <- xgb.DMatrix(data = d, info = list(label = y))
|
||||
dtrain <- xgb.DMatrix(data = d, info = list(label = y), nthread = n_threads)
|
||||
|
||||
correct <- function() {
|
||||
params <- list(max_depth = 2, booster = "dart",
|
||||
rate_drop = 0.5, one_drop = TRUE,
|
||||
objective = "reg:squarederror")
|
||||
params <- list(
|
||||
max_depth = 2,
|
||||
booster = "dart",
|
||||
rate_drop = 0.5,
|
||||
one_drop = TRUE,
|
||||
nthread = n_threads,
|
||||
objective = "reg:squarederror"
|
||||
)
|
||||
xgb.train(params = params, data = dtrain, nrounds = nrounds)
|
||||
}
|
||||
expect_silent(correct())
|
||||
incorrect <- function() {
|
||||
params <- list(max_depth = 2, booster = "dart",
|
||||
rate_drop = 0.5, one_drop = TRUE,
|
||||
objective = "reg:squarederror",
|
||||
foo = "bar", bar = "foo")
|
||||
params <- list(
|
||||
max_depth = 2,
|
||||
booster = "dart",
|
||||
rate_drop = 0.5,
|
||||
one_drop = TRUE,
|
||||
objective = "reg:squarederror",
|
||||
nthread = n_threads,
|
||||
foo = "bar",
|
||||
bar = "foo"
|
||||
)
|
||||
output <- capture.output(
|
||||
xgb.train(params = params, data = dtrain, nrounds = nrounds))
|
||||
xgb.train(params = params, data = dtrain, nrounds = nrounds)
|
||||
)
|
||||
print(output)
|
||||
}
|
||||
expect_output(incorrect(), '\\\\"bar\\\\", \\\\"foo\\\\"')
|
||||
@@ -79,7 +97,8 @@ test_that("dart prediction works", {
|
||||
d <- cbind(
|
||||
x1 = rnorm(100),
|
||||
x2 = rnorm(100),
|
||||
x3 = rnorm(100))
|
||||
x3 = rnorm(100)
|
||||
)
|
||||
y <- d[, "x1"] + d[, "x2"]^2 +
|
||||
ifelse(d[, "x3"] > .5, d[, "x3"]^2, 2^d[, "x3"]) +
|
||||
rnorm(100)
|
||||
@@ -93,7 +112,7 @@ test_that("dart prediction works", {
|
||||
rate_drop = 0.5,
|
||||
one_drop = TRUE,
|
||||
eta = 1,
|
||||
nthread = 2,
|
||||
nthread = n_threads,
|
||||
nrounds = nrounds,
|
||||
objective = "reg:squarederror"
|
||||
)
|
||||
@@ -105,7 +124,7 @@ test_that("dart prediction works", {
|
||||
expect_false(all(matrix(pred_by_xgboost_0, byrow = TRUE) == matrix(pred_by_xgboost_2, byrow = TRUE)))
|
||||
|
||||
set.seed(1994)
|
||||
dtrain <- xgb.DMatrix(data = d, info = list(label = y))
|
||||
dtrain <- xgb.DMatrix(data = d, info = list(label = y), nthread = n_threads)
|
||||
booster_by_train <- xgb.train(
|
||||
params = list(
|
||||
booster = "dart",
|
||||
@@ -113,7 +132,7 @@ test_that("dart prediction works", {
|
||||
eta = 1,
|
||||
rate_drop = 0.5,
|
||||
one_drop = TRUE,
|
||||
nthread = 1,
|
||||
nthread = n_threads,
|
||||
objective = "reg:squarederror"
|
||||
),
|
||||
data = dtrain,
|
||||
@@ -132,10 +151,13 @@ test_that("train and predict softprob", {
|
||||
lb <- as.numeric(iris$Species) - 1
|
||||
set.seed(11)
|
||||
expect_output(
|
||||
bst <- xgboost(data = as.matrix(iris[, -5]), label = lb,
|
||||
max_depth = 3, eta = 0.5, nthread = 2, nrounds = 5,
|
||||
objective = "multi:softprob", num_class = 3, eval_metric = "merror")
|
||||
, "train-merror")
|
||||
bst <- xgboost(
|
||||
data = as.matrix(iris[, -5]), label = lb,
|
||||
max_depth = 3, eta = 0.5, nthread = n_threads, nrounds = 5,
|
||||
objective = "multi:softprob", num_class = 3, eval_metric = "merror"
|
||||
),
|
||||
"train-merror"
|
||||
)
|
||||
expect_false(is.null(bst$evaluation_log))
|
||||
expect_lt(bst$evaluation_log[, min(train_merror)], 0.025)
|
||||
expect_equal(bst$niter * 3, xgb.ntree(bst))
|
||||
@@ -164,9 +186,10 @@ test_that("train and predict softprob", {
|
||||
x3 = rnorm(100)
|
||||
)
|
||||
y <- sample.int(10, 100, replace = TRUE) - 1
|
||||
dtrain <- xgb.DMatrix(data = d, info = list(label = y))
|
||||
dtrain <- xgb.DMatrix(data = d, info = list(label = y), nthread = n_threads)
|
||||
booster <- xgb.train(
|
||||
params = list(tree_method = "hist"), data = dtrain, nrounds = 4, num_class = 10,
|
||||
params = list(tree_method = "hist", nthread = n_threads),
|
||||
data = dtrain, nrounds = 4, num_class = 10,
|
||||
objective = "multi:softprob"
|
||||
)
|
||||
predt <- predict(booster, as.matrix(d), reshape = TRUE, strict_shape = FALSE)
|
||||
@@ -178,10 +201,13 @@ test_that("train and predict softmax", {
|
||||
lb <- as.numeric(iris$Species) - 1
|
||||
set.seed(11)
|
||||
expect_output(
|
||||
bst <- xgboost(data = as.matrix(iris[, -5]), label = lb,
|
||||
max_depth = 3, eta = 0.5, nthread = 2, nrounds = 5,
|
||||
objective = "multi:softmax", num_class = 3, eval_metric = "merror")
|
||||
, "train-merror")
|
||||
bst <- xgboost(
|
||||
data = as.matrix(iris[, -5]), label = lb,
|
||||
max_depth = 3, eta = 0.5, nthread = n_threads, nrounds = 5,
|
||||
objective = "multi:softmax", num_class = 3, eval_metric = "merror"
|
||||
),
|
||||
"train-merror"
|
||||
)
|
||||
expect_false(is.null(bst$evaluation_log))
|
||||
expect_lt(bst$evaluation_log[, min(train_merror)], 0.025)
|
||||
expect_equal(bst$niter * 3, xgb.ntree(bst))
|
||||
@@ -196,16 +222,19 @@ test_that("train and predict RF", {
|
||||
set.seed(11)
|
||||
lb <- train$label
|
||||
# single iteration
|
||||
bst <- xgboost(data = train$data, label = lb, max_depth = 5,
|
||||
nthread = 2, nrounds = 1, objective = "binary:logistic", eval_metric = "error",
|
||||
num_parallel_tree = 20, subsample = 0.6, colsample_bytree = 0.1)
|
||||
bst <- xgboost(
|
||||
data = train$data, label = lb, max_depth = 5,
|
||||
nthread = n_threads,
|
||||
nrounds = 1, objective = "binary:logistic", eval_metric = "error",
|
||||
num_parallel_tree = 20, subsample = 0.6, colsample_bytree = 0.1
|
||||
)
|
||||
expect_equal(bst$niter, 1)
|
||||
expect_equal(xgb.ntree(bst), 20)
|
||||
|
||||
pred <- predict(bst, train$data)
|
||||
pred_err <- sum((pred > 0.5) != lb) / length(lb)
|
||||
expect_lt(abs(bst$evaluation_log[1, train_error] - pred_err), 10e-6)
|
||||
#expect_lt(pred_err, 0.03)
|
||||
# expect_lt(pred_err, 0.03)
|
||||
|
||||
pred <- predict(bst, train$data, ntreelimit = 20)
|
||||
pred_err_20 <- sum((pred > 0.5) != lb) / length(lb)
|
||||
@@ -219,11 +248,13 @@ test_that("train and predict RF with softprob", {
|
||||
lb <- as.numeric(iris$Species) - 1
|
||||
nrounds <- 15
|
||||
set.seed(11)
|
||||
bst <- xgboost(data = as.matrix(iris[, -5]), label = lb,
|
||||
max_depth = 3, eta = 0.9, nthread = 2, nrounds = nrounds,
|
||||
objective = "multi:softprob", eval_metric = "merror",
|
||||
num_class = 3, verbose = 0,
|
||||
num_parallel_tree = 4, subsample = 0.5, colsample_bytree = 0.5)
|
||||
bst <- xgboost(
|
||||
data = as.matrix(iris[, -5]), label = lb,
|
||||
max_depth = 3, eta = 0.9, nthread = n_threads, nrounds = nrounds,
|
||||
objective = "multi:softprob", eval_metric = "merror",
|
||||
num_class = 3, verbose = 0,
|
||||
num_parallel_tree = 4, subsample = 0.5, colsample_bytree = 0.5
|
||||
)
|
||||
expect_equal(bst$niter, 15)
|
||||
expect_equal(xgb.ntree(bst), 15 * 3 * 4)
|
||||
# predict for all iterations:
|
||||
@@ -240,18 +271,24 @@ test_that("train and predict RF with softprob", {
|
||||
|
||||
test_that("use of multiple eval metrics works", {
|
||||
expect_output(
|
||||
bst <- xgboost(data = train$data, label = train$label, max_depth = 2,
|
||||
eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic",
|
||||
eval_metric = 'error', eval_metric = 'auc', eval_metric = "logloss")
|
||||
, "train-error.*train-auc.*train-logloss")
|
||||
bst <- xgboost(
|
||||
data = train$data, label = train$label, max_depth = 2,
|
||||
eta = 1, nthread = n_threads, nrounds = 2, objective = "binary:logistic",
|
||||
eval_metric = "error", eval_metric = "auc", eval_metric = "logloss"
|
||||
),
|
||||
"train-error.*train-auc.*train-logloss"
|
||||
)
|
||||
expect_false(is.null(bst$evaluation_log))
|
||||
expect_equal(dim(bst$evaluation_log), c(2, 4))
|
||||
expect_equal(colnames(bst$evaluation_log), c("iter", "train_error", "train_auc", "train_logloss"))
|
||||
expect_output(
|
||||
bst2 <- xgboost(data = train$data, label = train$label, max_depth = 2,
|
||||
eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic",
|
||||
eval_metric = list("error", "auc", "logloss"))
|
||||
, "train-error.*train-auc.*train-logloss")
|
||||
bst2 <- xgboost(
|
||||
data = train$data, label = train$label, max_depth = 2,
|
||||
eta = 1, nthread = n_threads, nrounds = 2, objective = "binary:logistic",
|
||||
eval_metric = list("error", "auc", "logloss")
|
||||
),
|
||||
"train-error.*train-auc.*train-logloss"
|
||||
)
|
||||
expect_false(is.null(bst2$evaluation_log))
|
||||
expect_equal(dim(bst2$evaluation_log), c(2, 4))
|
||||
expect_equal(colnames(bst2$evaluation_log), c("iter", "train_error", "train_auc", "train_logloss"))
|
||||
@@ -259,9 +296,11 @@ test_that("use of multiple eval metrics works", {
|
||||
|
||||
|
||||
test_that("training continuation works", {
|
||||
dtrain <- xgb.DMatrix(train$data, label = train$label)
|
||||
dtrain <- xgb.DMatrix(train$data, label = train$label, nthread = n_threads)
|
||||
watchlist <- list(train = dtrain)
|
||||
param <- list(objective = "binary:logistic", max_depth = 2, eta = 1, nthread = 2)
|
||||
param <- list(
|
||||
objective = "binary:logistic", max_depth = 2, eta = 1, nthread = n_threads
|
||||
)
|
||||
|
||||
# for the reference, use 4 iterations at once:
|
||||
set.seed(11)
|
||||
@@ -271,30 +310,33 @@ test_that("training continuation works", {
|
||||
bst1 <- xgb.train(param, dtrain, nrounds = 2, watchlist, verbose = 0)
|
||||
# continue for two more:
|
||||
bst2 <- xgb.train(param, dtrain, nrounds = 2, watchlist, verbose = 0, xgb_model = bst1)
|
||||
if (!windows_flag && !solaris_flag)
|
||||
if (!windows_flag && !solaris_flag) {
|
||||
expect_equal(bst$raw, bst2$raw)
|
||||
}
|
||||
expect_false(is.null(bst2$evaluation_log))
|
||||
expect_equal(dim(bst2$evaluation_log), c(4, 2))
|
||||
expect_equal(bst2$evaluation_log, bst$evaluation_log)
|
||||
# test continuing from raw model data
|
||||
bst2 <- xgb.train(param, dtrain, nrounds = 2, watchlist, verbose = 0, xgb_model = bst1$raw)
|
||||
if (!windows_flag && !solaris_flag)
|
||||
if (!windows_flag && !solaris_flag) {
|
||||
expect_equal(bst$raw, bst2$raw)
|
||||
}
|
||||
expect_equal(dim(bst2$evaluation_log), c(2, 2))
|
||||
# test continuing from a model in file
|
||||
xgb.save(bst1, "xgboost.json")
|
||||
bst2 <- xgb.train(param, dtrain, nrounds = 2, watchlist, verbose = 0, xgb_model = "xgboost.json")
|
||||
if (!windows_flag && !solaris_flag)
|
||||
if (!windows_flag && !solaris_flag) {
|
||||
expect_equal(bst$raw, bst2$raw)
|
||||
}
|
||||
expect_equal(dim(bst2$evaluation_log), c(2, 2))
|
||||
file.remove("xgboost.json")
|
||||
})
|
||||
|
||||
test_that("model serialization works", {
|
||||
out_path <- "model_serialization"
|
||||
dtrain <- xgb.DMatrix(train$data, label = train$label)
|
||||
dtrain <- xgb.DMatrix(train$data, label = train$label, nthread = n_threads)
|
||||
watchlist <- list(train = dtrain)
|
||||
param <- list(objective = "binary:logistic")
|
||||
param <- list(objective = "binary:logistic", nthread = n_threads)
|
||||
booster <- xgb.train(param, dtrain, nrounds = 4, watchlist)
|
||||
raw <- xgb.serialize(booster)
|
||||
saveRDS(raw, out_path)
|
||||
@@ -309,11 +351,14 @@ test_that("model serialization works", {
|
||||
test_that("xgb.cv works", {
|
||||
set.seed(11)
|
||||
expect_output(
|
||||
cv <- xgb.cv(data = train$data, label = train$label, max_depth = 2, nfold = 5,
|
||||
eta = 1., nthread = 2, nrounds = 2, objective = "binary:logistic",
|
||||
eval_metric = "error", verbose = TRUE)
|
||||
, "train-error:")
|
||||
expect_is(cv, 'xgb.cv.synchronous')
|
||||
cv <- xgb.cv(
|
||||
data = train$data, label = train$label, max_depth = 2, nfold = 5,
|
||||
eta = 1., nthread = n_threads, nrounds = 2, objective = "binary:logistic",
|
||||
eval_metric = "error", verbose = TRUE
|
||||
),
|
||||
"train-error:"
|
||||
)
|
||||
expect_is(cv, "xgb.cv.synchronous")
|
||||
expect_false(is.null(cv$evaluation_log))
|
||||
expect_lt(cv$evaluation_log[, min(test_error_mean)], 0.03)
|
||||
expect_lt(cv$evaluation_log[, min(test_error_std)], 0.008)
|
||||
@@ -326,15 +371,19 @@ test_that("xgb.cv works", {
|
||||
})
|
||||
|
||||
test_that("xgb.cv works with stratified folds", {
|
||||
dtrain <- xgb.DMatrix(train$data, label = train$label)
|
||||
dtrain <- xgb.DMatrix(train$data, label = train$label, nthread = n_threads)
|
||||
set.seed(314159)
|
||||
cv <- xgb.cv(data = dtrain, max_depth = 2, nfold = 5,
|
||||
eta = 1., nthread = 2, nrounds = 2, objective = "binary:logistic",
|
||||
verbose = TRUE, stratified = FALSE)
|
||||
cv <- xgb.cv(
|
||||
data = dtrain, max_depth = 2, nfold = 5,
|
||||
eta = 1., nthread = n_threads, nrounds = 2, objective = "binary:logistic",
|
||||
verbose = TRUE, stratified = FALSE
|
||||
)
|
||||
set.seed(314159)
|
||||
cv2 <- xgb.cv(data = dtrain, max_depth = 2, nfold = 5,
|
||||
eta = 1., nthread = 2, nrounds = 2, objective = "binary:logistic",
|
||||
verbose = TRUE, stratified = TRUE)
|
||||
cv2 <- xgb.cv(
|
||||
data = dtrain, max_depth = 2, nfold = 5,
|
||||
eta = 1., nthread = n_threads, nrounds = 2, objective = "binary:logistic",
|
||||
verbose = TRUE, stratified = TRUE
|
||||
)
|
||||
# Stratified folds should result in a different evaluation logs
|
||||
expect_true(all(cv$evaluation_log[, test_logloss_mean] != cv2$evaluation_log[, test_logloss_mean]))
|
||||
})
|
||||
@@ -342,40 +391,57 @@ test_that("xgb.cv works with stratified folds", {
|
||||
test_that("train and predict with non-strict classes", {
|
||||
# standard dense matrix input
|
||||
train_dense <- as.matrix(train$data)
|
||||
bst <- xgboost(data = train_dense, label = train$label, max_depth = 2,
|
||||
eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic", verbose = 0)
|
||||
bst <- xgboost(
|
||||
data = train_dense, label = train$label, max_depth = 2,
|
||||
eta = 1, nthread = n_threads, nrounds = 2, objective = "binary:logistic",
|
||||
verbose = 0
|
||||
)
|
||||
pr0 <- predict(bst, train_dense)
|
||||
|
||||
# dense matrix-like input of non-matrix class
|
||||
class(train_dense) <- 'shmatrix'
|
||||
class(train_dense) <- "shmatrix"
|
||||
expect_true(is.matrix(train_dense))
|
||||
expect_error(
|
||||
bst <- xgboost(data = train_dense, label = train$label, max_depth = 2,
|
||||
eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic", verbose = 0)
|
||||
, regexp = NA)
|
||||
bst <- xgboost(
|
||||
data = train_dense, label = train$label, max_depth = 2,
|
||||
eta = 1, nthread = n_threads, nrounds = 2, objective = "binary:logistic",
|
||||
verbose = 0
|
||||
),
|
||||
regexp = NA
|
||||
)
|
||||
expect_error(pr <- predict(bst, train_dense), regexp = NA)
|
||||
expect_equal(pr0, pr)
|
||||
|
||||
# dense matrix-like input of non-matrix class with some inheritance
|
||||
class(train_dense) <- c('pphmatrix', 'shmatrix')
|
||||
class(train_dense) <- c("pphmatrix", "shmatrix")
|
||||
expect_true(is.matrix(train_dense))
|
||||
expect_error(
|
||||
bst <- xgboost(data = train_dense, label = train$label, max_depth = 2,
|
||||
eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic", verbose = 0)
|
||||
, regexp = NA)
|
||||
bst <- xgboost(
|
||||
data = train_dense, label = train$label, max_depth = 2,
|
||||
eta = 1, nthread = n_threads, nrounds = 2, objective = "binary:logistic",
|
||||
verbose = 0
|
||||
),
|
||||
regexp = NA
|
||||
)
|
||||
expect_error(pr <- predict(bst, train_dense), regexp = NA)
|
||||
expect_equal(pr0, pr)
|
||||
|
||||
# when someone inherits from xgb.Booster, it should still be possible to use it as xgb.Booster
|
||||
class(bst) <- c('super.Booster', 'xgb.Booster')
|
||||
class(bst) <- c("super.Booster", "xgb.Booster")
|
||||
expect_error(pr <- predict(bst, train_dense), regexp = NA)
|
||||
expect_equal(pr0, pr)
|
||||
})
|
||||
|
||||
test_that("max_delta_step works", {
|
||||
dtrain <- xgb.DMatrix(agaricus.train$data, label = agaricus.train$label)
|
||||
dtrain <- xgb.DMatrix(
|
||||
agaricus.train$data, label = agaricus.train$label, nthread = n_threads
|
||||
)
|
||||
watchlist <- list(train = dtrain)
|
||||
param <- list(objective = "binary:logistic", eval_metric = "logloss", max_depth = 2, nthread = 2, eta = 0.5)
|
||||
param <- list(
|
||||
objective = "binary:logistic", eval_metric = "logloss", max_depth = 2,
|
||||
nthread = n_threads,
|
||||
eta = 0.5
|
||||
)
|
||||
nrounds <- 5
|
||||
# model with no restriction on max_delta_step
|
||||
bst1 <- xgb.train(param, dtrain, nrounds, watchlist, verbose = 1)
|
||||
@@ -395,14 +461,16 @@ test_that("colsample_bytree works", {
|
||||
test_y <- as.numeric(rowSums(test_x) > 0)
|
||||
colnames(train_x) <- paste0("Feature_", sprintf("%03d", 1:100))
|
||||
colnames(test_x) <- paste0("Feature_", sprintf("%03d", 1:100))
|
||||
dtrain <- xgb.DMatrix(train_x, label = train_y)
|
||||
dtest <- xgb.DMatrix(test_x, label = test_y)
|
||||
dtrain <- xgb.DMatrix(train_x, label = train_y, nthread = n_threads)
|
||||
dtest <- xgb.DMatrix(test_x, label = test_y, nthread = n_threads)
|
||||
watchlist <- list(train = dtrain, eval = dtest)
|
||||
## Use colsample_bytree = 0.01, so that roughly one out of 100 features is chosen for
|
||||
## each tree
|
||||
param <- list(max_depth = 2, eta = 0, nthread = 2,
|
||||
colsample_bytree = 0.01, objective = "binary:logistic",
|
||||
eval_metric = "auc")
|
||||
param <- list(
|
||||
max_depth = 2, eta = 0, nthread = n_threads,
|
||||
colsample_bytree = 0.01, objective = "binary:logistic",
|
||||
eval_metric = "auc"
|
||||
)
|
||||
set.seed(2)
|
||||
bst <- xgb.train(param, dtrain, nrounds = 100, watchlist, verbose = 0)
|
||||
xgb.importance(model = bst)
|
||||
@@ -412,9 +480,11 @@ test_that("colsample_bytree works", {
|
||||
})
|
||||
|
||||
test_that("Configuration works", {
|
||||
bst <- xgboost(data = train$data, label = train$label, max_depth = 2,
|
||||
eta = 1, nthread = 2, nrounds = 2, objective = "binary:logistic",
|
||||
eval_metric = 'error', eval_metric = 'auc', eval_metric = "logloss")
|
||||
bst <- xgboost(
|
||||
data = train$data, label = train$label, max_depth = 2,
|
||||
eta = 1, nthread = n_threads, nrounds = 2, objective = "binary:logistic",
|
||||
eval_metric = "error", eval_metric = "auc", eval_metric = "logloss"
|
||||
)
|
||||
config <- xgb.config(bst)
|
||||
xgb.config(bst) <- config
|
||||
reloaded_config <- xgb.config(bst)
|
||||
@@ -451,22 +521,26 @@ test_that("strict_shape works", {
|
||||
y <- as.numeric(iris$Species) - 1
|
||||
X <- as.matrix(iris[, -5])
|
||||
|
||||
bst <- xgboost(data = X, label = y,
|
||||
max_depth = 2, nrounds = n_rounds,
|
||||
objective = "multi:softprob", num_class = 3, eval_metric = "merror")
|
||||
bst <- xgboost(
|
||||
data = X, label = y,
|
||||
max_depth = 2, nrounds = n_rounds, nthread = n_threads,
|
||||
objective = "multi:softprob", num_class = 3, eval_metric = "merror"
|
||||
)
|
||||
|
||||
test_strict_shape(bst, X, 3)
|
||||
}
|
||||
|
||||
|
||||
test_agaricus <- function() {
|
||||
data(agaricus.train, package = 'xgboost')
|
||||
data(agaricus.train, package = "xgboost")
|
||||
X <- agaricus.train$data
|
||||
y <- agaricus.train$label
|
||||
|
||||
bst <- xgboost(data = X, label = y, max_depth = 2,
|
||||
nrounds = n_rounds, objective = "binary:logistic",
|
||||
eval_metric = 'error', eval_metric = 'auc', eval_metric = "logloss")
|
||||
bst <- xgboost(
|
||||
data = X, label = y, max_depth = 2, nthread = n_threads,
|
||||
nrounds = n_rounds, objective = "binary:logistic",
|
||||
eval_metric = "error", eval_metric = "auc", eval_metric = "logloss"
|
||||
)
|
||||
|
||||
test_strict_shape(bst, X, 1)
|
||||
}
|
||||
@@ -481,8 +555,10 @@ test_that("'predict' accepts CSR data", {
|
||||
x_csc <- as(X[1L, , drop = FALSE], "CsparseMatrix")
|
||||
x_csr <- as(x_csc, "RsparseMatrix")
|
||||
x_spv <- as(x_csc, "sparseVector")
|
||||
bst <- xgboost(data = X, label = y, objective = "binary:logistic",
|
||||
nrounds = 5L, verbose = FALSE)
|
||||
bst <- xgboost(
|
||||
data = X, label = y, objective = "binary:logistic",
|
||||
nrounds = 5L, verbose = FALSE, nthread = n_threads,
|
||||
)
|
||||
p_csc <- predict(bst, x_csc)
|
||||
p_csr <- predict(bst, x_csr)
|
||||
p_spv <- predict(bst, x_spv)
|
||||
|
||||
@@ -6,6 +6,8 @@ data(agaricus.test, package = 'xgboost')
|
||||
train <- agaricus.train
|
||||
test <- agaricus.test
|
||||
|
||||
n_threads <- 2
|
||||
|
||||
# add some label noise for early stopping tests
|
||||
add.noise <- function(label, frac) {
|
||||
inoise <- sample(length(label), length(label) * frac)
|
||||
@@ -15,15 +17,15 @@ add.noise <- function(label, frac) {
|
||||
set.seed(11)
|
||||
ltrain <- add.noise(train$label, 0.2)
|
||||
ltest <- add.noise(test$label, 0.2)
|
||||
dtrain <- xgb.DMatrix(train$data, label = ltrain)
|
||||
dtest <- xgb.DMatrix(test$data, label = ltest)
|
||||
dtrain <- xgb.DMatrix(train$data, label = ltrain, nthread = n_threads)
|
||||
dtest <- xgb.DMatrix(test$data, label = ltest, nthread = n_threads)
|
||||
watchlist <- list(train = dtrain, test = dtest)
|
||||
|
||||
|
||||
err <- function(label, pr) sum((pr > 0.5) != label) / length(label)
|
||||
|
||||
param <- list(objective = "binary:logistic", eval_metric = "error",
|
||||
max_depth = 2, nthread = 2)
|
||||
max_depth = 2, nthread = n_threads)
|
||||
|
||||
|
||||
test_that("cb.print.evaluation works as expected", {
|
||||
@@ -103,7 +105,7 @@ test_that("cb.evaluation.log works as expected", {
|
||||
|
||||
|
||||
param <- list(objective = "binary:logistic", eval_metric = "error",
|
||||
max_depth = 4, nthread = 2)
|
||||
max_depth = 4, nthread = n_threads)
|
||||
|
||||
test_that("can store evaluation_log without printing", {
|
||||
expect_silent(
|
||||
@@ -179,8 +181,10 @@ test_that("cb.save.model works as expected", {
|
||||
expect_true(file.exists('xgboost_01.json'))
|
||||
expect_true(file.exists('xgboost_02.json'))
|
||||
b1 <- xgb.load('xgboost_01.json')
|
||||
xgb.parameters(b1) <- list(nthread = 2)
|
||||
expect_equal(xgb.ntree(b1), 1)
|
||||
b2 <- xgb.load('xgboost_02.json')
|
||||
xgb.parameters(b2) <- list(nthread = 2)
|
||||
expect_equal(xgb.ntree(b2), 2)
|
||||
|
||||
xgb.config(b2) <- xgb.config(bst)
|
||||
@@ -267,7 +271,8 @@ test_that("early stopping works with titanic", {
|
||||
objective = "binary:logistic",
|
||||
eval_metric = "auc",
|
||||
nrounds = 100,
|
||||
early_stopping_rounds = 3
|
||||
early_stopping_rounds = 3,
|
||||
nthread = n_threads
|
||||
)
|
||||
|
||||
expect_true(TRUE) # should not crash
|
||||
@@ -308,7 +313,7 @@ test_that("prediction in xgb.cv works", {
|
||||
|
||||
test_that("prediction in xgb.cv works for gblinear too", {
|
||||
set.seed(11)
|
||||
p <- list(booster = 'gblinear', objective = "reg:logistic", nthread = 2)
|
||||
p <- list(booster = 'gblinear', objective = "reg:logistic", nthread = n_threads)
|
||||
cv <- xgb.cv(p, dtrain, nfold = 5, eta = 0.5, nrounds = 2, prediction = TRUE, verbose = 0)
|
||||
expect_false(is.null(cv$evaluation_log))
|
||||
expect_false(is.null(cv$pred))
|
||||
@@ -341,7 +346,7 @@ test_that("prediction in xgb.cv for softprob works", {
|
||||
set.seed(11)
|
||||
expect_warning(
|
||||
cv <- xgb.cv(data = as.matrix(iris[, -5]), label = lb, nfold = 4,
|
||||
eta = 0.5, nrounds = 5, max_depth = 3, nthread = 2,
|
||||
eta = 0.5, nrounds = 5, max_depth = 3, nthread = n_threads,
|
||||
subsample = 0.8, gamma = 2, verbose = 0,
|
||||
prediction = TRUE, objective = "multi:softprob", num_class = 3)
|
||||
, NA)
|
||||
|
||||
@@ -2,10 +2,16 @@ context('Test models with custom objective')
|
||||
|
||||
set.seed(1994)
|
||||
|
||||
n_threads <- 2
|
||||
|
||||
data(agaricus.train, package = 'xgboost')
|
||||
data(agaricus.test, package = 'xgboost')
|
||||
dtrain <- xgb.DMatrix(agaricus.train$data, label = agaricus.train$label)
|
||||
dtest <- xgb.DMatrix(agaricus.test$data, label = agaricus.test$label)
|
||||
dtrain <- xgb.DMatrix(
|
||||
agaricus.train$data, label = agaricus.train$label, nthread = n_threads
|
||||
)
|
||||
dtest <- xgb.DMatrix(
|
||||
agaricus.test$data, label = agaricus.test$label, nthread = n_threads
|
||||
)
|
||||
watchlist <- list(eval = dtest, train = dtrain)
|
||||
|
||||
logregobj <- function(preds, dtrain) {
|
||||
@@ -22,7 +28,7 @@ evalerror <- function(preds, dtrain) {
|
||||
return(list(metric = "error", value = err))
|
||||
}
|
||||
|
||||
param <- list(max_depth = 2, eta = 1, nthread = 2,
|
||||
param <- list(max_depth = 2, eta = 1, nthread = n_threads,
|
||||
objective = logregobj, eval_metric = evalerror)
|
||||
num_round <- 2
|
||||
|
||||
@@ -67,7 +73,7 @@ test_that("custom objective using DMatrix attr works", {
|
||||
test_that("custom objective with multi-class shape", {
|
||||
data <- as.matrix(iris[, -5])
|
||||
label <- as.numeric(iris$Species) - 1
|
||||
dtrain <- xgb.DMatrix(data = data, label = label)
|
||||
dtrain <- xgb.DMatrix(data = data, label = label, nthread = n_threads)
|
||||
n_classes <- 3
|
||||
|
||||
fake_softprob <- function(preds, dtrain) {
|
||||
|
||||
@@ -5,19 +5,21 @@ data(agaricus.test, package = "xgboost")
|
||||
test_data <- agaricus.test$data[1:100, ]
|
||||
test_label <- agaricus.test$label[1:100]
|
||||
|
||||
n_threads <- 2
|
||||
|
||||
test_that("xgb.DMatrix: basic construction", {
|
||||
# from sparse matrix
|
||||
dtest1 <- xgb.DMatrix(test_data, label = test_label)
|
||||
dtest1 <- xgb.DMatrix(test_data, label = test_label, nthread = n_threads)
|
||||
|
||||
# from dense matrix
|
||||
dtest2 <- xgb.DMatrix(as.matrix(test_data), label = test_label)
|
||||
dtest2 <- xgb.DMatrix(as.matrix(test_data), label = test_label, nthread = n_threads)
|
||||
expect_equal(getinfo(dtest1, "label"), getinfo(dtest2, "label"))
|
||||
expect_equal(dim(dtest1), dim(dtest2))
|
||||
|
||||
# from dense integer matrix
|
||||
int_data <- as.matrix(test_data)
|
||||
storage.mode(int_data) <- "integer"
|
||||
dtest3 <- xgb.DMatrix(int_data, label = test_label)
|
||||
dtest3 <- xgb.DMatrix(int_data, label = test_label, nthread = n_threads)
|
||||
expect_equal(dim(dtest1), dim(dtest3))
|
||||
|
||||
n_samples <- 100
|
||||
@@ -29,15 +31,15 @@ test_that("xgb.DMatrix: basic construction", {
|
||||
X <- matrix(X, nrow = n_samples)
|
||||
y <- rbinom(n = n_samples, size = 1, prob = 1 / 2)
|
||||
|
||||
fd <- xgb.DMatrix(X, label = y, missing = 1)
|
||||
fd <- xgb.DMatrix(X, label = y, missing = 1, nthread = n_threads)
|
||||
|
||||
dgc <- as(X, "dgCMatrix")
|
||||
fdgc <- xgb.DMatrix(dgc, label = y, missing = 1.0)
|
||||
fdgc <- xgb.DMatrix(dgc, label = y, missing = 1.0, nthread = n_threads)
|
||||
|
||||
dgr <- as(X, "dgRMatrix")
|
||||
fdgr <- xgb.DMatrix(dgr, label = y, missing = 1)
|
||||
fdgr <- xgb.DMatrix(dgr, label = y, missing = 1, nthread = n_threads)
|
||||
|
||||
params <- list(tree_method = "hist")
|
||||
params <- list(tree_method = "hist", nthread = n_threads)
|
||||
bst_fd <- xgb.train(
|
||||
params, nrounds = 8, fd, watchlist = list(train = fd)
|
||||
)
|
||||
@@ -64,12 +66,12 @@ test_that("xgb.DMatrix: NA", {
|
||||
)
|
||||
x[1, "x1"] <- NA
|
||||
|
||||
m <- xgb.DMatrix(x)
|
||||
m <- xgb.DMatrix(x, nthread = n_threads)
|
||||
xgb.DMatrix.save(m, "int.dmatrix")
|
||||
|
||||
x <- matrix(as.numeric(x), nrow = n_samples, ncol = 2)
|
||||
colnames(x) <- c("x1", "x2")
|
||||
m <- xgb.DMatrix(x)
|
||||
m <- xgb.DMatrix(x, nthread = n_threads)
|
||||
|
||||
xgb.DMatrix.save(m, "float.dmatrix")
|
||||
|
||||
@@ -94,7 +96,7 @@ test_that("xgb.DMatrix: NA", {
|
||||
|
||||
test_that("xgb.DMatrix: saving, loading", {
|
||||
# save to a local file
|
||||
dtest1 <- xgb.DMatrix(test_data, label = test_label)
|
||||
dtest1 <- xgb.DMatrix(test_data, label = test_label, nthread = n_threads)
|
||||
tmp_file <- tempfile('xgb.DMatrix_')
|
||||
on.exit(unlink(tmp_file))
|
||||
expect_true(xgb.DMatrix.save(dtest1, tmp_file))
|
||||
@@ -109,13 +111,17 @@ test_that("xgb.DMatrix: saving, loading", {
|
||||
tmp_file <- tempfile(fileext = ".libsvm")
|
||||
writeLines(tmp, tmp_file)
|
||||
expect_true(file.exists(tmp_file))
|
||||
dtest4 <- xgb.DMatrix(paste(tmp_file, "?format=libsvm", sep = ""), silent = TRUE)
|
||||
dtest4 <- xgb.DMatrix(
|
||||
paste(tmp_file, "?format=libsvm", sep = ""), silent = TRUE, nthread = n_threads
|
||||
)
|
||||
expect_equal(dim(dtest4), c(3, 4))
|
||||
expect_equal(getinfo(dtest4, 'label'), c(0, 1, 0))
|
||||
|
||||
# check that feature info is saved
|
||||
data(agaricus.train, package = 'xgboost')
|
||||
dtrain <- xgb.DMatrix(data = agaricus.train$data, label = agaricus.train$label)
|
||||
dtrain <- xgb.DMatrix(
|
||||
data = agaricus.train$data, label = agaricus.train$label, nthread = n_threads
|
||||
)
|
||||
cnames <- colnames(dtrain)
|
||||
expect_equal(length(cnames), 126)
|
||||
tmp_file <- tempfile('xgb.DMatrix_')
|
||||
@@ -129,7 +135,7 @@ test_that("xgb.DMatrix: saving, loading", {
|
||||
})
|
||||
|
||||
test_that("xgb.DMatrix: getinfo & setinfo", {
|
||||
dtest <- xgb.DMatrix(test_data)
|
||||
dtest <- xgb.DMatrix(test_data, nthread = n_threads)
|
||||
expect_true(setinfo(dtest, 'label', test_label))
|
||||
labels <- getinfo(dtest, 'label')
|
||||
expect_equal(test_label, getinfo(dtest, 'label'))
|
||||
@@ -156,7 +162,7 @@ test_that("xgb.DMatrix: getinfo & setinfo", {
|
||||
})
|
||||
|
||||
test_that("xgb.DMatrix: slice, dim", {
|
||||
dtest <- xgb.DMatrix(test_data, label = test_label)
|
||||
dtest <- xgb.DMatrix(test_data, label = test_label, nthread = n_threads)
|
||||
expect_equal(dim(dtest), dim(test_data))
|
||||
dsub1 <- slice(dtest, 1:42)
|
||||
expect_equal(nrow(dsub1), 42)
|
||||
@@ -171,16 +177,20 @@ test_that("xgb.DMatrix: slice, trailing empty rows", {
|
||||
data(agaricus.train, package = 'xgboost')
|
||||
train_data <- agaricus.train$data
|
||||
train_label <- agaricus.train$label
|
||||
dtrain <- xgb.DMatrix(data = train_data, label = train_label)
|
||||
dtrain <- xgb.DMatrix(
|
||||
data = train_data, label = train_label, nthread = n_threads
|
||||
)
|
||||
slice(dtrain, 6513L)
|
||||
train_data[6513, ] <- 0
|
||||
dtrain <- xgb.DMatrix(data = train_data, label = train_label)
|
||||
dtrain <- xgb.DMatrix(
|
||||
data = train_data, label = train_label, nthread = n_threads
|
||||
)
|
||||
slice(dtrain, 6513L)
|
||||
expect_equal(nrow(dtrain), 6513)
|
||||
})
|
||||
|
||||
test_that("xgb.DMatrix: colnames", {
|
||||
dtest <- xgb.DMatrix(test_data, label = test_label)
|
||||
dtest <- xgb.DMatrix(test_data, label = test_label, nthread = n_threads)
|
||||
expect_equal(colnames(dtest), colnames(test_data))
|
||||
expect_error(colnames(dtest) <- 'asdf')
|
||||
new_names <- make.names(seq_len(ncol(test_data)))
|
||||
@@ -196,7 +206,7 @@ test_that("xgb.DMatrix: nrow is correct for a very sparse matrix", {
|
||||
x <- Matrix::rsparsematrix(nr, 100, density = 0.0005)
|
||||
# we want it very sparse, so that last rows are empty
|
||||
expect_lt(max(x@i), nr)
|
||||
dtest <- xgb.DMatrix(x)
|
||||
dtest <- xgb.DMatrix(x, nthread = n_threads)
|
||||
expect_equal(dim(dtest), dim(x))
|
||||
})
|
||||
|
||||
@@ -205,8 +215,8 @@ test_that("xgb.DMatrix: print", {
|
||||
|
||||
# core DMatrix with just data and labels
|
||||
dtrain <- xgb.DMatrix(
|
||||
data = agaricus.train$data
|
||||
, label = agaricus.train$label
|
||||
data = agaricus.train$data, label = agaricus.train$label,
|
||||
nthread = n_threads
|
||||
)
|
||||
txt <- capture.output({
|
||||
print(dtrain)
|
||||
@@ -222,10 +232,11 @@ test_that("xgb.DMatrix: print", {
|
||||
|
||||
# DMatrix with weights and base_margin
|
||||
dtrain <- xgb.DMatrix(
|
||||
data = agaricus.train$data
|
||||
, label = agaricus.train$label
|
||||
, weight = seq_along(agaricus.train$label)
|
||||
, base_margin = agaricus.train$label
|
||||
data = agaricus.train$data,
|
||||
label = agaricus.train$label,
|
||||
weight = seq_along(agaricus.train$label),
|
||||
base_margin = agaricus.train$label,
|
||||
nthread = n_threads
|
||||
)
|
||||
txt <- capture.output({
|
||||
print(dtrain)
|
||||
@@ -234,7 +245,8 @@ test_that("xgb.DMatrix: print", {
|
||||
|
||||
# DMatrix with just features
|
||||
dtrain <- xgb.DMatrix(
|
||||
data = agaricus.train$data
|
||||
data = agaricus.train$data,
|
||||
nthread = n_threads
|
||||
)
|
||||
txt <- capture.output({
|
||||
print(dtrain)
|
||||
@@ -245,7 +257,8 @@ test_that("xgb.DMatrix: print", {
|
||||
data_no_colnames <- agaricus.train$data
|
||||
colnames(data_no_colnames) <- NULL
|
||||
dtrain <- xgb.DMatrix(
|
||||
data = data_no_colnames
|
||||
data = data_no_colnames,
|
||||
nthread = n_threads
|
||||
)
|
||||
txt <- capture.output({
|
||||
print(dtrain)
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
context("feature weights")
|
||||
|
||||
n_threads <- 2
|
||||
|
||||
test_that("training with feature weights works", {
|
||||
nrows <- 1000
|
||||
ncols <- 9
|
||||
@@ -10,8 +12,12 @@ test_that("training with feature weights works", {
|
||||
|
||||
test <- function(tm) {
|
||||
names <- paste0("f", 1:ncols)
|
||||
xy <- xgb.DMatrix(data = x, label = y, feature_weights = weights)
|
||||
params <- list(colsample_bynode = 0.4, tree_method = tm, nthread = 1)
|
||||
xy <- xgb.DMatrix(
|
||||
data = x, label = y, feature_weights = weights, nthread = n_threads
|
||||
)
|
||||
params <- list(
|
||||
colsample_bynode = 0.4, tree_method = tm, nthread = n_threads
|
||||
)
|
||||
model <- xgb.train(params = params, data = xy, nrounds = 32)
|
||||
importance <- xgb.importance(model = model, feature_names = names)
|
||||
expect_equal(dim(importance), c(ncols, 4))
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
context('Test generalized linear models')
|
||||
|
||||
n_threads <- 2
|
||||
|
||||
test_that("gblinear works", {
|
||||
data(agaricus.train, package = 'xgboost')
|
||||
data(agaricus.test, package = 'xgboost')
|
||||
dtrain <- xgb.DMatrix(agaricus.train$data, label = agaricus.train$label)
|
||||
dtest <- xgb.DMatrix(agaricus.test$data, label = agaricus.test$label)
|
||||
dtrain <- xgb.DMatrix(
|
||||
agaricus.train$data, label = agaricus.train$label, nthread = n_threads
|
||||
)
|
||||
dtest <- xgb.DMatrix(
|
||||
agaricus.test$data, label = agaricus.test$label, nthread = n_threads
|
||||
)
|
||||
|
||||
param <- list(objective = "binary:logistic", eval_metric = "error", booster = "gblinear",
|
||||
nthread = 2, eta = 0.8, alpha = 0.0001, lambda = 0.0001)
|
||||
nthread = n_threads, eta = 0.8, alpha = 0.0001, lambda = 0.0001)
|
||||
watchlist <- list(eval = dtest, train = dtrain)
|
||||
|
||||
n <- 5 # iterations
|
||||
@@ -48,12 +54,16 @@ test_that("gblinear works", {
|
||||
test_that("gblinear early stopping works", {
|
||||
data(agaricus.train, package = 'xgboost')
|
||||
data(agaricus.test, package = 'xgboost')
|
||||
dtrain <- xgb.DMatrix(agaricus.train$data, label = agaricus.train$label)
|
||||
dtest <- xgb.DMatrix(agaricus.test$data, label = agaricus.test$label)
|
||||
dtrain <- xgb.DMatrix(
|
||||
agaricus.train$data, label = agaricus.train$label, nthread = n_threads
|
||||
)
|
||||
dtest <- xgb.DMatrix(
|
||||
agaricus.test$data, label = agaricus.test$label, nthread = n_threads
|
||||
)
|
||||
|
||||
param <- list(
|
||||
objective = "binary:logistic", eval_metric = "error", booster = "gblinear",
|
||||
nthread = 2, eta = 0.8, alpha = 0.0001, lambda = 0.0001,
|
||||
nthread = n_threads, eta = 0.8, alpha = 0.0001, lambda = 0.0001,
|
||||
updater = "coord_descent"
|
||||
)
|
||||
|
||||
|
||||
@@ -171,6 +171,7 @@ test_that("SHAPs sum to predictions, with or without DART", {
|
||||
fit <- xgboost(
|
||||
params = c(
|
||||
list(
|
||||
nthread = 2,
|
||||
booster = booster,
|
||||
objective = "reg:squarederror",
|
||||
eval_metric = "rmse"),
|
||||
@@ -257,7 +258,7 @@ test_that("xgb.Booster serializing as R object works", {
|
||||
.skip_if_vcd_not_available()
|
||||
saveRDS(bst.Tree, 'xgb.model.rds')
|
||||
bst <- readRDS('xgb.model.rds')
|
||||
dtrain <- xgb.DMatrix(sparse_matrix, label = label)
|
||||
dtrain <- xgb.DMatrix(sparse_matrix, label = label, nthread = 2)
|
||||
expect_equal(predict(bst.Tree, dtrain), predict(bst, dtrain), tolerance = float_tolerance)
|
||||
expect_equal(xgb.dump(bst.Tree), xgb.dump(bst))
|
||||
xgb.save(bst, 'xgb.model')
|
||||
@@ -363,7 +364,8 @@ test_that("xgb.importance works with and without feature names", {
|
||||
data = as.matrix(data.frame(x = c(0, 1))),
|
||||
label = c(1, 2),
|
||||
nrounds = 1,
|
||||
base_score = 0.5
|
||||
base_score = 0.5,
|
||||
nthread = 2
|
||||
)
|
||||
df <- xgb.model.dt.tree(model = m)
|
||||
expect_equal(df$Feature, "Leaf")
|
||||
|
||||
@@ -2,6 +2,8 @@ require(xgboost)
|
||||
|
||||
context("interaction constraints")
|
||||
|
||||
n_threads <- 2
|
||||
|
||||
set.seed(1024)
|
||||
x1 <- rnorm(1000, 1)
|
||||
x2 <- rnorm(1000, 1)
|
||||
@@ -45,11 +47,18 @@ test_that("interaction constraints scientific representation", {
|
||||
d <- matrix(rexp(rows, rate = .1), nrow = rows, ncol = cols)
|
||||
y <- rnorm(rows)
|
||||
|
||||
dtrain <- xgb.DMatrix(data = d, info = list(label = y))
|
||||
dtrain <- xgb.DMatrix(data = d, info = list(label = y), nthread = n_threads)
|
||||
inc <- list(c(seq.int(from = 0, to = cols, by = 1)))
|
||||
|
||||
with_inc <- xgb.train(data = dtrain, tree_method = 'hist',
|
||||
interaction_constraints = inc, nrounds = 10)
|
||||
without_inc <- xgb.train(data = dtrain, tree_method = 'hist', nrounds = 10)
|
||||
with_inc <- xgb.train(
|
||||
data = dtrain,
|
||||
tree_method = 'hist',
|
||||
interaction_constraints = inc,
|
||||
nrounds = 10,
|
||||
nthread = n_threads
|
||||
)
|
||||
without_inc <- xgb.train(
|
||||
data = dtrain, tree_method = 'hist', nrounds = 10, nthread = n_threads
|
||||
)
|
||||
expect_equal(xgb.save.raw(with_inc), xgb.save.raw(without_inc))
|
||||
})
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
context('Test prediction of feature interactions')
|
||||
|
||||
set.seed(123)
|
||||
n_threads <- 2
|
||||
|
||||
test_that("predict feature interactions works", {
|
||||
# simulate some binary data and a linear outcome with an interaction term
|
||||
@@ -19,8 +20,10 @@ test_that("predict feature interactions works", {
|
||||
|
||||
y <- f_int(X)
|
||||
|
||||
dm <- xgb.DMatrix(X, label = y)
|
||||
param <- list(eta = 0.1, max_depth = 4, base_score = mean(y), lambda = 0, nthread = 2)
|
||||
dm <- xgb.DMatrix(X, label = y, nthread = n_threads)
|
||||
param <- list(
|
||||
eta = 0.1, max_depth = 4, base_score = mean(y), lambda = 0, nthread = n_threads
|
||||
)
|
||||
b <- xgb.train(param, dm, 100)
|
||||
|
||||
pred <- predict(b, dm, outputmargin = TRUE)
|
||||
@@ -99,11 +102,13 @@ test_that("SHAP contribution values are not NAN", {
|
||||
verbose = 0,
|
||||
params = list(
|
||||
objective = "reg:squarederror",
|
||||
eval_metric = "rmse"),
|
||||
eval_metric = "rmse",
|
||||
nthread = n_threads
|
||||
),
|
||||
data = as.matrix(subset(d, fold == 2)[, ivs]),
|
||||
label = subset(d, fold == 2)$y,
|
||||
nthread = 1,
|
||||
nrounds = 3)
|
||||
nrounds = 3
|
||||
)
|
||||
|
||||
shaps <- as.data.frame(predict(fit,
|
||||
newdata = as.matrix(subset(d, fold == 1)[, ivs]),
|
||||
@@ -116,8 +121,12 @@ test_that("SHAP contribution values are not NAN", {
|
||||
|
||||
|
||||
test_that("multiclass feature interactions work", {
|
||||
dm <- xgb.DMatrix(as.matrix(iris[, -5]), label = as.numeric(iris$Species) - 1)
|
||||
param <- list(eta = 0.1, max_depth = 4, objective = 'multi:softprob', num_class = 3)
|
||||
dm <- xgb.DMatrix(
|
||||
as.matrix(iris[, -5]), label = as.numeric(iris$Species) - 1, nthread = n_threads
|
||||
)
|
||||
param <- list(
|
||||
eta = 0.1, max_depth = 4, objective = 'multi:softprob', num_class = 3, nthread = n_threads
|
||||
)
|
||||
b <- xgb.train(param, dm, 40)
|
||||
pred <- t(
|
||||
array(
|
||||
@@ -166,6 +175,7 @@ test_that("SHAP single sample works", {
|
||||
max_depth = 2,
|
||||
nrounds = 4,
|
||||
objective = "binary:logistic",
|
||||
nthread = n_threads
|
||||
)
|
||||
|
||||
predt <- predict(
|
||||
|
||||
@@ -9,7 +9,8 @@ test_that("load/save raw works", {
|
||||
nrounds <- 8
|
||||
booster <- xgboost(
|
||||
data = train$data, label = train$label,
|
||||
nrounds = nrounds, objective = "binary:logistic"
|
||||
nrounds = nrounds, objective = "binary:logistic",
|
||||
nthread = 2
|
||||
)
|
||||
|
||||
json_bytes <- xgb.save.raw(booster, raw_format = "json")
|
||||
|
||||
@@ -66,7 +66,7 @@ test_that("Models from previous versions of XGBoost can be loaded", {
|
||||
unzip(zipfile, exdir = extract_dir, overwrite = TRUE)
|
||||
model_dir <- file.path(extract_dir, 'models')
|
||||
|
||||
pred_data <- xgb.DMatrix(matrix(c(0, 0, 0, 0), nrow = 1, ncol = 4))
|
||||
pred_data <- xgb.DMatrix(matrix(c(0, 0, 0, 0), nrow = 1, ncol = 4), nthread = 2)
|
||||
|
||||
lapply(list.files(model_dir), function (x) {
|
||||
model_file <- file.path(model_dir, x)
|
||||
@@ -87,6 +87,7 @@ test_that("Models from previous versions of XGBoost can be loaded", {
|
||||
booster <- readRDS(model_file)
|
||||
} else {
|
||||
booster <- xgb.load(model_file)
|
||||
xgb.parameters(booster) <- list(nthread = 2)
|
||||
}
|
||||
predict(booster, newdata = pred_data)
|
||||
run_booster_check(booster, name)
|
||||
|
||||
@@ -3,8 +3,12 @@ context('Test model params and call are exposed to R')
|
||||
data(agaricus.train, package = 'xgboost')
|
||||
data(agaricus.test, package = 'xgboost')
|
||||
|
||||
dtrain <- xgb.DMatrix(agaricus.train$data, label = agaricus.train$label)
|
||||
dtest <- xgb.DMatrix(agaricus.test$data, label = agaricus.test$label)
|
||||
dtrain <- xgb.DMatrix(
|
||||
agaricus.train$data, label = agaricus.train$label, nthread = 2
|
||||
)
|
||||
dtest <- xgb.DMatrix(
|
||||
agaricus.test$data, label = agaricus.test$label, nthread = 2
|
||||
)
|
||||
|
||||
bst <- xgboost(data = dtrain,
|
||||
max_depth = 2,
|
||||
|
||||
@@ -4,8 +4,10 @@ set.seed(1994)
|
||||
|
||||
test_that("Poisson regression works", {
|
||||
data(mtcars)
|
||||
bst <- xgboost(data = as.matrix(mtcars[, -11]), label = mtcars[, 11],
|
||||
objective = 'count:poisson', nrounds = 10, verbose = 0)
|
||||
bst <- xgboost(
|
||||
data = as.matrix(mtcars[, -11]), label = mtcars[, 11],
|
||||
objective = 'count:poisson', nrounds = 10, verbose = 0, nthread = 2
|
||||
)
|
||||
expect_equal(class(bst), "xgb.Booster")
|
||||
pred <- predict(bst, as.matrix(mtcars[, -11]))
|
||||
expect_equal(length(pred), 32)
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
context('Learning to rank')
|
||||
|
||||
n_threads <- 2
|
||||
|
||||
test_that('Test ranking with unweighted data', {
|
||||
X <- Matrix::sparseMatrix(
|
||||
i = c(2, 3, 7, 9, 12, 15, 17, 18)
|
||||
@@ -9,10 +11,10 @@ test_that('Test ranking with unweighted data', {
|
||||
)
|
||||
y <- c(0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0)
|
||||
group <- c(5, 5, 5, 5)
|
||||
dtrain <- xgb.DMatrix(X, label = y, group = group)
|
||||
dtrain <- xgb.DMatrix(X, label = y, group = group, nthread = n_threads)
|
||||
|
||||
params <- list(eta = 1, tree_method = 'exact', objective = 'rank:pairwise', max_depth = 1,
|
||||
eval_metric = 'auc', eval_metric = 'aucpr')
|
||||
eval_metric = 'auc', eval_metric = 'aucpr', nthread = n_threads)
|
||||
bst <- xgb.train(params, dtrain, nrounds = 10, watchlist = list(train = dtrain))
|
||||
# Check if the metric is monotone increasing
|
||||
expect_true(all(diff(bst$evaluation_log$train_auc) >= 0))
|
||||
@@ -29,10 +31,14 @@ test_that('Test ranking with weighted data', {
|
||||
y <- c(0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0)
|
||||
group <- c(5, 5, 5, 5)
|
||||
weight <- c(1.0, 2.0, 3.0, 4.0)
|
||||
dtrain <- xgb.DMatrix(X, label = y, group = group, weight = weight)
|
||||
dtrain <- xgb.DMatrix(
|
||||
X, label = y, group = group, weight = weight, nthread = n_threads
|
||||
)
|
||||
|
||||
params <- list(eta = 1, tree_method = 'exact', objective = 'rank:pairwise', max_depth = 1,
|
||||
eval_metric = 'auc', eval_metric = 'aucpr')
|
||||
params <- list(
|
||||
eta = 1, tree_method = "exact", objective = "rank:pairwise", max_depth = 1,
|
||||
eval_metric = "auc", eval_metric = "aucpr", nthread = n_threads
|
||||
)
|
||||
bst <- xgb.train(params, dtrain, nrounds = 10, watchlist = list(train = dtrain))
|
||||
# Check if the metric is monotone increasing
|
||||
expect_true(all(diff(bst$evaluation_log$train_auc) >= 0))
|
||||
|
||||
@@ -16,6 +16,7 @@ test_that("Can save and load models with Unicode paths", {
|
||||
path <- file.path(tmpdir, x)
|
||||
xgb.save(bst, path)
|
||||
bst2 <- xgb.load(path)
|
||||
xgb.parameters(bst2) <- list(nthread = 2)
|
||||
expect_equal(predict(bst, test$data), predict(bst2, test$data))
|
||||
})
|
||||
})
|
||||
|
||||
@@ -2,8 +2,15 @@ context("update trees in an existing model")
|
||||
|
||||
data(agaricus.train, package = 'xgboost')
|
||||
data(agaricus.test, package = 'xgboost')
|
||||
dtrain <- xgb.DMatrix(agaricus.train$data, label = agaricus.train$label)
|
||||
dtest <- xgb.DMatrix(agaricus.test$data, label = agaricus.test$label)
|
||||
|
||||
n_threads <- 1
|
||||
|
||||
dtrain <- xgb.DMatrix(
|
||||
agaricus.train$data, label = agaricus.train$label, nthread = n_threads
|
||||
)
|
||||
dtest <- xgb.DMatrix(
|
||||
agaricus.test$data, label = agaricus.test$label, nthread = n_threads
|
||||
)
|
||||
|
||||
# Disable flaky tests for 32-bit Windows.
|
||||
# See https://github.com/dmlc/xgboost/issues/3720
|
||||
@@ -14,7 +21,7 @@ test_that("updating the model works", {
|
||||
|
||||
# no-subsampling
|
||||
p1 <- list(
|
||||
objective = "binary:logistic", max_depth = 2, eta = 0.05, nthread = 2,
|
||||
objective = "binary:logistic", max_depth = 2, eta = 0.05, nthread = n_threads,
|
||||
updater = "grow_colmaker,prune"
|
||||
)
|
||||
set.seed(11)
|
||||
@@ -86,9 +93,11 @@ test_that("updating the model works", {
|
||||
})
|
||||
|
||||
test_that("updating works for multiclass & multitree", {
|
||||
dtr <- xgb.DMatrix(as.matrix(iris[, -5]), label = as.numeric(iris$Species) - 1)
|
||||
dtr <- xgb.DMatrix(
|
||||
as.matrix(iris[, -5]), label = as.numeric(iris$Species) - 1, nthread = n_threads
|
||||
)
|
||||
watchlist <- list(train = dtr)
|
||||
p0 <- list(max_depth = 2, eta = 0.5, nthread = 2, subsample = 0.6,
|
||||
p0 <- list(max_depth = 2, eta = 0.5, nthread = n_threads, subsample = 0.6,
|
||||
objective = "multi:softprob", num_class = 3, num_parallel_tree = 2,
|
||||
base_score = 0)
|
||||
set.seed(121)
|
||||
|
||||
Reference in New Issue
Block a user