| 1 |
context('Test generalized linear models') |
| 2 |
|
| 3 |
n_threads <- 2 |
| 4 |
|
| 5 |
test_that("gblinear works", { |
| 6 |
data(agaricus.train, package = 'xgboost') |
| 7 |
data(agaricus.test, package = 'xgboost') |
| 8 |
dtrain <- xgb.DMatrix( |
| 9 |
agaricus.train$data, label = agaricus.train$label, nthread = n_threads |
| 10 |
) |
| 11 |
dtest <- xgb.DMatrix( |
| 12 |
agaricus.test$data, label = agaricus.test$label, nthread = n_threads |
| 13 |
) |
| 14 |
|
| 15 |
param <- list(objective = "binary:logistic", eval_metric = "error", booster = "gblinear", |
| 16 |
nthread = n_threads, learning_rate = 0.8, reg_alpha = 0.0001, reg_lambda = 0.0001) |
| 17 |
evals <- list(eval = dtest, train = dtrain) |
| 18 |
|
| 19 |
n <- 5 # iterations |
| 20 |
ERR_UL <- 0.005 # upper limit for the test set error |
| 21 |
VERB <- 0 # chatterbox switch |
| 22 |
|
| 23 |
param$updater <- 'shotgun' |
| 24 |
bst <- xgb.train(c(param, list(feature_selector = 'shuffle')), dtrain, n, evals, verbose = VERB) |
| 25 |
ypred <- predict(bst, dtest) |
| 26 |
expect_equal(length(getinfo(dtest, 'label')), 1611) |
| 27 |
expect_lt(attributes(bst)$evaluation_log$eval_error[n], ERR_UL) |
| 28 |
|
| 29 |
bst <- xgb.train(c(param, list(feature_selector = 'cyclic')), dtrain, n, evals, verbose = VERB, |
| 30 |
callbacks = list(xgb.cb.gblinear.history())) |
| 31 |
expect_lt(attributes(bst)$evaluation_log$eval_error[n], ERR_UL) |
| 32 |
h <- xgb.gblinear.history(bst) |
| 33 |
expect_equal(dim(h), c(n, ncol(dtrain) + 1)) |
| 34 |
expect_is(h, "matrix") |
| 35 |
|
| 36 |
param$updater <- 'coord_descent' |
| 37 |
bst <- xgb.train(c(param, list(feature_selector = 'cyclic')), dtrain, n, evals, verbose = VERB) |
| 38 |
expect_lt(attributes(bst)$evaluation_log$eval_error[n], ERR_UL) |
| 39 |
|
| 40 |
bst <- xgb.train(c(param, list(feature_selector = 'shuffle')), dtrain, n, evals, verbose = VERB) |
| 41 |
expect_lt(attributes(bst)$evaluation_log$eval_error[n], ERR_UL) |
| 42 |
|
| 43 |
bst <- xgb.train(c(param, list(feature_selector = 'greedy')), dtrain, 2, evals, verbose = VERB) |
| 44 |
expect_lt(attributes(bst)$evaluation_log$eval_error[2], ERR_UL) |
| 45 |
|
| 46 |
bst <- xgb.train(c(param, list(feature_selector = 'thrifty', top_k = 50)), dtrain, n, evals, verbose = VERB, |
| 47 |
callbacks = list(xgb.cb.gblinear.history(sparse = TRUE))) |
| 48 |
expect_lt(attributes(bst)$evaluation_log$eval_error[n], ERR_UL) |
| 49 |
h <- xgb.gblinear.history(bst) |
| 50 |
expect_equal(dim(h), c(n, ncol(dtrain) + 1)) |
| 51 |
expect_s4_class(h, "dgCMatrix") |
| 52 |
}) |
| 53 |
|
| 54 |
test_that("gblinear early stopping works", { |
| 55 |
data(agaricus.train, package = 'xgboost') |
| 56 |
data(agaricus.test, package = 'xgboost') |
| 57 |
dtrain <- xgb.DMatrix( |
| 58 |
agaricus.train$data, label = agaricus.train$label, nthread = n_threads |
| 59 |
) |
| 60 |
dtest <- xgb.DMatrix( |
| 61 |
agaricus.test$data, label = agaricus.test$label, nthread = n_threads |
| 62 |
) |
| 63 |
|
| 64 |
param <- xgb.params( |
| 65 |
objective = "binary:logistic", eval_metric = "error", booster = "gblinear", |
| 66 |
nthread = n_threads, learning_rate = 0.8, reg_alpha = 0.0001, reg_lambda = 0.0001, |
| 67 |
updater = "coord_descent" |
| 68 |
) |
| 69 |
|
| 70 |
es_round <- 1 |
| 71 |
n <- 10 |
| 72 |
booster <- xgb.train( |
| 73 |
param, dtrain, nrounds = n, evals = list(eval = dtest, train = dtrain), |
| 74 |
early_stopping_rounds = es_round, verbose = 0 |
| 75 |
) |
| 76 |
expect_equal(xgb.attr(booster, "best_iteration"), 4) |
| 77 |
predt_es <- predict(booster, dtrain) |
| 78 |
|
| 79 |
n <- xgb.attr(booster, "best_iteration") + es_round + 1 |
| 80 |
booster <- xgb.train( |
| 81 |
param, dtrain, nrounds = n, evals = list(eval = dtest, train = dtrain), |
| 82 |
early_stopping_rounds = es_round, verbose = 0 |
| 83 |
) |
| 84 |
predt <- predict(booster, dtrain) |
| 85 |
expect_equal(predt_es, predt) |
| 86 |
}) |