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 165 166 167 168 169
|
/*!
* Copyright 2021 XGBoost contributors
*/
#include <gtest/gtest.h>
#include <xgboost/base.h>
#include <xgboost/tree_model.h>
#include "../helpers.h"
namespace xgboost {
class TestGrowPolicy : public ::testing::Test {
protected:
std::shared_ptr<DMatrix> Xy_;
size_t n_samples_ = 4096, n_features_ = 13;
float sparsity_ = 0.5;
protected:
void SetUp() override {
Xy_ =
RandomDataGenerator{n_samples_, n_features_, sparsity_}.GenerateDMatrix(
true);
}
std::unique_ptr<Learner> TrainOneIter(Context const* ctx, std::string tree_method,
std::string policy, int32_t max_leaves, int32_t max_depth) {
std::unique_ptr<Learner> learner{Learner::Create({this->Xy_})};
learner->SetParam("tree_method", tree_method);
learner->SetParam("device", ctx->DeviceName());
if (max_leaves >= 0) {
learner->SetParam("max_leaves", std::to_string(max_leaves));
}
if (max_depth >= 0) {
learner->SetParam("max_depth", std::to_string(max_depth));
}
learner->SetParam("grow_policy", policy);
auto check_max_leave = [&]() {
Json model{Object{}};
learner->SaveModel(&model);
auto j_tree = model["learner"]["gradient_booster"]["model"]["trees"][0];
RegTree tree;
tree.LoadModel(j_tree);
CHECK_LE(tree.GetNumLeaves(), max_leaves);
};
auto check_max_depth = [&](int32_t sol) {
Json model{Object{}};
learner->SaveModel(&model);
auto j_tree = model["learner"]["gradient_booster"]["model"]["trees"][0];
RegTree tree;
tree.LoadModel(j_tree);
bst_node_t depth = 0;
tree.WalkTree([&](bst_node_t nidx) {
depth = std::max(tree.GetDepth(nidx), depth);
return true;
});
if (sol > -1) {
CHECK_EQ(depth, sol);
} else {
CHECK_EQ(depth, max_depth) << "tree method: " << tree_method << " policy: " << policy
<< " leaves:" << max_leaves << ", depth:" << max_depth;
}
};
if (max_leaves == 0 && max_depth == 0) {
// unconstrainted
if (ctx->IsCPU()) {
// GPU pre-allocates for all nodes.
learner->UpdateOneIter(0, Xy_);
}
} else if (max_leaves > 0 && max_depth == 0) {
learner->UpdateOneIter(0, Xy_);
check_max_leave();
} else if (max_leaves == 0 && max_depth > 0) {
learner->UpdateOneIter(0, Xy_);
check_max_depth(-1);
} else if (max_leaves > 0 && max_depth > 0) {
learner->UpdateOneIter(0, Xy_);
check_max_leave();
check_max_depth(2);
} else if (max_leaves == -1 && max_depth == 0) {
// default max_leaves is 0, so both of them are now 0
} else {
// default parameters
learner->UpdateOneIter(0, Xy_);
}
return learner;
}
void TestCombination(Context const* ctx, std::string tree_method) {
for (auto policy : {"depthwise", "lossguide"}) {
// -1 means default
for (auto leaves : {-1, 0, 3}) {
for (auto depth : {-1, 0, 3}) {
this->TrainOneIter(ctx, tree_method, policy, leaves, depth);
}
}
}
}
void TestTreeGrowPolicy(Context const* ctx, std::string tree_method, std::string policy) {
{
/**
* max_leaves
*/
auto learner = this->TrainOneIter(ctx, tree_method, policy, 16, -1);
Json model{Object{}};
learner->SaveModel(&model);
auto j_tree = model["learner"]["gradient_booster"]["model"]["trees"][0];
RegTree tree;
tree.LoadModel(j_tree);
ASSERT_EQ(tree.GetNumLeaves(), 16);
}
{
/**
* max_depth
*/
auto learner = this->TrainOneIter(ctx, tree_method, policy, -1, 3);
Json model{Object{}};
learner->SaveModel(&model);
auto j_tree = model["learner"]["gradient_booster"]["model"]["trees"][0];
RegTree tree;
tree.LoadModel(j_tree);
bst_node_t depth = 0;
tree.WalkTree([&](bst_node_t nidx) {
depth = std::max(tree.GetDepth(nidx), depth);
return true;
});
ASSERT_EQ(depth, 3);
}
}
};
TEST_F(TestGrowPolicy, Approx) {
Context ctx;
this->TestTreeGrowPolicy(&ctx, "approx", "depthwise");
this->TestTreeGrowPolicy(&ctx, "approx", "lossguide");
this->TestCombination(&ctx, "approx");
}
TEST_F(TestGrowPolicy, Hist) {
Context ctx;
this->TestTreeGrowPolicy(&ctx, "hist", "depthwise");
this->TestTreeGrowPolicy(&ctx, "hist", "lossguide");
this->TestCombination(&ctx, "hist");
}
#if defined(XGBOOST_USE_CUDA)
TEST_F(TestGrowPolicy, GpuHist) {
auto ctx = MakeCUDACtx(0);
this->TestTreeGrowPolicy(&ctx, "hist", "depthwise");
this->TestTreeGrowPolicy(&ctx, "hist", "lossguide");
this->TestCombination(&ctx, "hist");
}
TEST_F(TestGrowPolicy, GpuApprox) {
auto ctx = MakeCUDACtx(0);
this->TestTreeGrowPolicy(&ctx, "approx", "depthwise");
this->TestTreeGrowPolicy(&ctx, "approx", "lossguide");
this->TestCombination(&ctx, "approx");
}
#endif // defined(XGBOOST_USE_CUDA)
} // namespace xgboost
|