Debian Codegraph @

tests/testthat/test_update.R

raw
1 context("update trees in an existing model")
2
3 data(agaricus.train, package = 'xgboost')
4 data(agaricus.test, package = 'xgboost')
5
6 n_threads <- 1
7
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 # Disable flaky tests for 32-bit Windows.
16 # See https://github.com/dmlc/xgboost/issues/3720
17 win32_flag <- .Platform$OS.type == "windows" && .Machine$sizeof.pointer != 8
18
19 test_that("updating the model works", {
20 evals <- list(train = dtrain, test = dtest)
21
22 # no-subsampling
23 p1 <- xgb.params(
24 objective = "binary:logistic",
25 max_depth = 2,
26 learning_rate = 0.05,
27 nthread = n_threads,
28 updater = "grow_colmaker,prune"
29 )
30 set.seed(11)
31 bst1 <- xgb.train(p1, dtrain, nrounds = 10, evals = evals, verbose = 0)
32 tr1 <- xgb.model.dt.tree(model = bst1)
33
34 # with subsampling
35 p2 <- modifyList(p1, list(subsample = 0.1))
36 set.seed(11)
37 bst2 <- xgb.train(p2, dtrain, nrounds = 10, evals = evals, verbose = 0)
38 tr2 <- xgb.model.dt.tree(model = bst2)
39
40 # the same no-subsampling boosting with an extra 'refresh' updater:
41 p1r <- modifyList(p1, list(updater = 'grow_colmaker,prune,refresh', refresh_leaf = FALSE))
42 set.seed(11)
43 bst1r <- xgb.train(p1r, dtrain, nrounds = 10, evals = evals, verbose = 0)
44 tr1r <- xgb.model.dt.tree(model = bst1r)
45 # all should be the same when no subsampling
46 expect_equal(attributes(bst1)$evaluation_log, attributes(bst1r)$evaluation_log)
47 expect_equal(
48 jsonlite::fromJSON(rawToChar(xgb.save.raw(bst1, raw_format = "json"))),
49 jsonlite::fromJSON(rawToChar(xgb.save.raw(bst1r, raw_format = "json"))),
50 tolerance = 1e-6
51 )
52 if (!win32_flag) {
53 expect_equal(tr1, tr1r, tolerance = 0.00001, check.attributes = FALSE)
54 }
55
56 # the same boosting with subsampling with an extra 'refresh' updater:
57 p2r <- modifyList(p2, list(updater = 'grow_colmaker,prune,refresh', refresh_leaf = FALSE))
58 set.seed(11)
59 bst2r <- xgb.train(p2r, dtrain, nrounds = 10, evals = evals, verbose = 0)
60 tr2r <- xgb.model.dt.tree(model = bst2r)
61 # should be the same evaluation but different gains and larger cover
62 expect_equal(attributes(bst2)$evaluation_log, attributes(bst2r)$evaluation_log)
63 if (!win32_flag) {
64 expect_equal(tr2[Feature == 'Leaf']$Gain, tr2r[Feature == 'Leaf']$Gain)
65 }
66 expect_gt(sum(abs(tr2[Feature != 'Leaf']$Gain - tr2r[Feature != 'Leaf']$Gain)), 100)
67 expect_gt(sum(tr2r$Cover) / sum(tr2$Cover), 1.5)
68
69 # process type 'update' for no-subsampling model, refreshing the tree stats AND leaves from training data:
70 set.seed(123)
71 p1u <- modifyList(p1, list(process_type = 'update', updater = 'refresh', refresh_leaf = TRUE))
72 bst1u <- xgb.train(p1u, dtrain, nrounds = 10, evals = evals, verbose = 0, xgb_model = bst1)
73 tr1u <- xgb.model.dt.tree(model = bst1u)
74 # all should be the same when no subsampling
75 expect_equal(attributes(bst1)$evaluation_log, attributes(bst1u)$evaluation_log)
76 expect_equal(
77 jsonlite::fromJSON(rawToChar(xgb.save.raw(bst1, raw_format = "json"))),
78 jsonlite::fromJSON(rawToChar(xgb.save.raw(bst1u, raw_format = "json"))),
79 tolerance = 1e-6
80 )
81 expect_equal(tr1, tr1u, tolerance = 0.00001, check.attributes = FALSE)
82
83 # same thing but with a serialized model
84 set.seed(123)
85 bst1u <- xgb.train(p1u, dtrain, nrounds = 10, evals = evals, verbose = 0, xgb_model = xgb.save.raw(bst1))
86 tr1u <- xgb.model.dt.tree(model = bst1u)
87 # all should be the same when no subsampling
88 expect_equal(attributes(bst1)$evaluation_log, attributes(bst1u)$evaluation_log)
89 expect_equal(tr1, tr1u, tolerance = 0.00001, check.attributes = FALSE)
90
91 # process type 'update' for model with subsampling, refreshing only the tree stats from training data:
92 p2u <- modifyList(p2, list(process_type = 'update', updater = 'refresh', refresh_leaf = FALSE))
93 bst2u <- xgb.train(p2u, dtrain, nrounds = 10, evals = evals, verbose = 0, xgb_model = bst2)
94 tr2u <- xgb.model.dt.tree(model = bst2u)
95 # should be the same evaluation but different gains and larger cover
96 expect_equal(attributes(bst2)$evaluation_log, attributes(bst2u)$evaluation_log)
97 expect_equal(tr2[Feature == 'Leaf']$Gain, tr2u[Feature == 'Leaf']$Gain)
98 expect_gt(sum(abs(tr2[Feature != 'Leaf']$Gain - tr2u[Feature != 'Leaf']$Gain)), 100)
99 expect_gt(sum(tr2u$Cover) / sum(tr2$Cover), 1.5)
100 # the results should be the same as for the model with an extra 'refresh' updater
101 expect_equal(attributes(bst2r)$evaluation_log, attributes(bst2u)$evaluation_log)
102 if (!win32_flag) {
103 expect_equal(tr2r, tr2u, tolerance = 0.00001, check.attributes = FALSE)
104 }
105
106 # process type 'update' for no-subsampling model, refreshing only the tree stats from TEST data:
107 p1ut <- modifyList(p1, list(process_type = 'update', updater = 'refresh', refresh_leaf = FALSE))
108 bst1ut <- xgb.train(p1ut, dtest, nrounds = 10, evals = evals, verbose = 0, xgb_model = bst1)
109 tr1ut <- xgb.model.dt.tree(model = bst1ut)
110 # should be the same evaluations but different gains and smaller cover (test data is smaller)
111 expect_equal(attributes(bst1)$evaluation_log, attributes(bst1ut)$evaluation_log)
112 expect_equal(tr1[Feature == 'Leaf']$Gain, tr1ut[Feature == 'Leaf']$Gain)
113 expect_gt(sum(abs(tr1[Feature != 'Leaf']$Gain - tr1ut[Feature != 'Leaf']$Gain)), 100)
114 expect_lt(sum(tr1ut$Cover) / sum(tr1$Cover), 0.5)
115 })
116
117 test_that("updating works for multiclass & multitree", {
118 dtr <- xgb.DMatrix(
119 as.matrix(iris[, -5]), label = as.numeric(iris$Species) - 1, nthread = n_threads
120 )
121 evals <- list(train = dtr)
122 p0 <- xgb.params(
123 max_depth = 2,
124 learning_rate = 0.5,
125 nthread = n_threads,
126 subsample = 0.6,
127 objective = "multi:softprob",
128 num_class = 3,
129 num_parallel_tree = 2,
130 base_score = 0
131 )
132 set.seed(121)
133 bst0 <- xgb.train(p0, dtr, 5, evals = evals, verbose = 0)
134 tr0 <- xgb.model.dt.tree(model = bst0)
135
136 # run update process for an original model with subsampling
137 p0u <- modifyList(p0, list(process_type = 'update', updater = 'refresh', refresh_leaf = FALSE))
138 bst0u <- xgb.train(p0u, dtr, nrounds = xgb.get.num.boosted.rounds(bst0),
139 evals = evals, xgb_model = bst0, verbose = 0)
140 tr0u <- xgb.model.dt.tree(model = bst0u)
141
142 # should be the same evaluation but different gains and larger cover
143 expect_equal(attributes(bst0)$evaluation_log, attributes(bst0u)$evaluation_log)
144 expect_equal(tr0[Feature == 'Leaf']$Gain, tr0u[Feature == 'Leaf']$Gain)
145 expect_gt(sum(abs(tr0[Feature != 'Leaf']$Gain - tr0u[Feature != 'Leaf']$Gain)), 100)
146 expect_gt(sum(tr0u$Cover) / sum(tr0$Cover), 1.5)
147 })