xgboost/tests/cpp/tree/test_tree_policy.cc
Jiaming Yuan 001503186c
Rewrite approx (#7214)
This PR rewrites the approx tree method to use codebase from hist for better performance and code sharing.

The rewrite has many benefits:
- Support for both `max_leaves` and `max_depth`.
- Support for `grow_policy`.
- Support for mono constraint.
- Support for feature weights.
- Support for easier bin configuration (`max_bin`).
- Support for categorical data.
- Faster performance for most of the datasets. (many times faster)
- Support for prediction cache.
- Significantly better performance for external memory.
- Unites the code base between approx and hist.
2022-01-10 21:15:05 +08:00

81 lines
2.2 KiB
C++

/*!
* 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);
}
void TestTreeGrowPolicy(std::string tree_method, std::string policy) {
{
std::unique_ptr<Learner> learner{Learner::Create({this->Xy_})};
learner->SetParam("tree_method", tree_method);
learner->SetParam("max_leaves", "16");
learner->SetParam("grow_policy", policy);
learner->Configure();
learner->UpdateOneIter(0, Xy_);
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);
}
{
std::unique_ptr<Learner> learner{Learner::Create({this->Xy_})};
learner->SetParam("tree_method", tree_method);
learner->SetParam("max_depth", "3");
learner->SetParam("grow_policy", policy);
learner->Configure();
learner->UpdateOneIter(0, Xy_);
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) {
this->TestTreeGrowPolicy("approx", "depthwise");
this->TestTreeGrowPolicy("approx", "lossguide");
}
TEST_F(TestGrowPolicy, Hist) {
this->TestTreeGrowPolicy("hist", "depthwise");
this->TestTreeGrowPolicy("hist", "lossguide");
}
#if defined(XGBOOST_USE_CUDA)
TEST_F(TestGrowPolicy, GpuHist) {
this->TestTreeGrowPolicy("gpu_hist", "depthwise");
this->TestTreeGrowPolicy("gpu_hist", "lossguide");
}
#endif // defined(XGBOOST_USE_CUDA)
} // namespace xgboost