* [backport] Fix monotone constraints on CPU. (#9122)
This commit is contained in:
parent
abc80d2a6d
commit
573f1c7db4
@ -389,6 +389,7 @@ class HistEvaluator {
|
|||||||
tree_evaluator_.AddSplit(candidate.nid, left_child, right_child,
|
tree_evaluator_.AddSplit(candidate.nid, left_child, right_child,
|
||||||
tree[candidate.nid].SplitIndex(), left_weight,
|
tree[candidate.nid].SplitIndex(), left_weight,
|
||||||
right_weight);
|
right_weight);
|
||||||
|
evaluator = tree_evaluator_.GetEvaluator();
|
||||||
|
|
||||||
auto max_node = std::max(left_child, tree[candidate.nid].RightChild());
|
auto max_node = std::max(left_child, tree[candidate.nid].RightChild());
|
||||||
max_node = std::max(candidate.nid, max_node);
|
max_node = std::max(candidate.nid, max_node);
|
||||||
|
|||||||
@ -48,6 +48,8 @@ class TreeEvaluator {
|
|||||||
monotone_.HostVector().resize(n_features, 0);
|
monotone_.HostVector().resize(n_features, 0);
|
||||||
has_constraint_ = false;
|
has_constraint_ = false;
|
||||||
} else {
|
} else {
|
||||||
|
CHECK_LE(p.monotone_constraints.size(), n_features)
|
||||||
|
<< "The size of monotone constraint should be less or equal to the number of features.";
|
||||||
monotone_.HostVector() = p.monotone_constraints;
|
monotone_.HostVector() = p.monotone_constraints;
|
||||||
monotone_.HostVector().resize(n_features, 0);
|
monotone_.HostVector().resize(n_features, 0);
|
||||||
// Initialised to some small size, can grow if needed
|
// Initialised to some small size, can grow if needed
|
||||||
|
|||||||
@ -6,6 +6,9 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "../../../src/tree/constraints.h"
|
#include "../../../src/tree/constraints.h"
|
||||||
|
#include "../../../src/tree/hist/evaluate_splits.h"
|
||||||
|
#include "../../../src/tree/hist/expand_entry.h"
|
||||||
|
#include "../helpers.h"
|
||||||
|
|
||||||
namespace xgboost {
|
namespace xgboost {
|
||||||
namespace tree {
|
namespace tree {
|
||||||
@ -56,5 +59,38 @@ TEST(CPUFeatureInteractionConstraint, Basic) {
|
|||||||
ASSERT_FALSE(constraints.Query(1, 5));
|
ASSERT_FALSE(constraints.Query(1, 5));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(CPUMonoConstraint, Basic) {
|
||||||
|
std::size_t kRows{64}, kCols{16};
|
||||||
|
Context ctx;
|
||||||
|
|
||||||
|
TrainParam param;
|
||||||
|
std::vector<std::int32_t> mono(kCols, 1);
|
||||||
|
I32Array arr;
|
||||||
|
for (std::size_t i = 0; i < kCols; ++i) {
|
||||||
|
arr.GetArray().push_back(mono[i]);
|
||||||
|
}
|
||||||
|
Json jarr{std::move(arr)};
|
||||||
|
std::string str_mono;
|
||||||
|
Json::Dump(jarr, &str_mono);
|
||||||
|
str_mono.front() = '(';
|
||||||
|
str_mono.back() = ')';
|
||||||
|
|
||||||
|
param.UpdateAllowUnknown(Args{{"monotone_constraints", str_mono}});
|
||||||
|
|
||||||
|
auto Xy = RandomDataGenerator{kRows, kCols, 0.0}.GenerateDMatrix(true);
|
||||||
|
auto sampler = std::make_shared<common::ColumnSampler>();
|
||||||
|
|
||||||
|
HistEvaluator<CPUExpandEntry> evalutor{param, Xy->Info(), ctx.Threads(), sampler};
|
||||||
|
evalutor.InitRoot(GradStats{2.0, 2.0});
|
||||||
|
|
||||||
|
SplitEntry split;
|
||||||
|
split.Update(1.0f, 0, 3.0, false, false, GradStats{1.0, 1.0}, GradStats{1.0, 1.0});
|
||||||
|
CPUExpandEntry entry{0, 0, split};
|
||||||
|
RegTree tree;
|
||||||
|
tree.param.UpdateAllowUnknown(Args{{"num_feature", std::to_string(kCols)}});
|
||||||
|
evalutor.ApplyTreeSplit(entry, &tree);
|
||||||
|
|
||||||
|
ASSERT_TRUE(evalutor.Evaluator().has_constraint);
|
||||||
|
}
|
||||||
} // namespace tree
|
} // namespace tree
|
||||||
} // namespace xgboost
|
} // namespace xgboost
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user