Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for tree grow policy. #7302

Merged
merged 1 commit into from
Oct 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions tests/cpp/tree/test_tree_policy.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*!
* 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, DISABLED_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