MCPcopy Create free account
hub / github.com/dmlc/xgboost / CompareOneHotAndPartition

Function CompareOneHotAndPartition

tests/cpp/tree/hist/test_evaluate_splits.cc:279–327  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

277
278namespace {
279auto CompareOneHotAndPartition(bool onehot) {
280 Context ctx;
281 static constexpr bst_idx_t kRows = 128, kCols = 1;
282 std::vector<FeatureType> ft(kCols, FeatureType::kCategorical);
283
284 TrainParam param;
285 if (onehot) {
286 // force use one-hot
287 param.UpdateAllowUnknown(
288 Args{{"min_child_weight", "0"}, {"reg_lambda", "0"}, {"max_cat_to_onehot", "100"}});
289 } else {
290 param.UpdateAllowUnknown(
291 Args{{"min_child_weight", "0"}, {"reg_lambda", "0"}, {"max_cat_to_onehot", "1"}});
292 }
293
294 size_t n_cats{2};
295
296 auto dmat =
297 RandomDataGenerator(kRows, kCols, 0).Seed(3).Type(ft).MaxCategory(n_cats).GenerateDMatrix();
298
299 auto sampler = std::make_shared<common::ColumnSampler>();
300 auto evaluator = HistEvaluator{&ctx, &param, dmat->Info(), sampler};
301 std::vector<CPUExpandEntry> entries(1);
302 HistMakerTrainParam hist_param;
303
304 for (auto const &gmat : dmat->GetBatches<GHistIndexMatrix>(&ctx, {32, param.sparse_threshold})) {
305 BoundedHistCollection hist;
306
307 entries.front().nid = 0;
308 entries.front().depth = 0;
309
310 hist.Reset(gmat.cut.TotalBins(), hist_param.MaxCachedHistNodes(ctx.Device()));
311 hist.AllocateHistograms({0});
312 auto node_hist = hist[0];
313
314 CHECK_EQ(node_hist.size(), n_cats);
315 CHECK_EQ(node_hist.size(), gmat.cut.Ptrs().back());
316
317 GradientPairPrecise total_gpair;
318 for (size_t i = 0; i < node_hist.size(); ++i) {
319 node_hist[i] = {static_cast<double>(node_hist.size() - i), 1.0};
320 total_gpair += node_hist[i];
321 }
322 RegTree tree;
323 evaluator.InitRoot(GradStats{total_gpair});
324 evaluator.EvaluateSplits(hist, gmat.cut, ft, tree, &entries);
325 }
326 return entries.front();
327}
328} // anonymous namespace
329
330TEST(HistEvaluator, Categorical) {

Callers 1

TESTFunction · 0.85

Calls 15

RandomDataGeneratorClass · 0.85
GenerateDMatrixMethod · 0.80
MaxCategoryMethod · 0.80
SeedMethod · 0.80
MaxCachedHistNodesMethod · 0.80
AllocateHistogramsMethod · 0.80
UpdateAllowUnknownMethod · 0.45
TypeMethod · 0.45
InfoMethod · 0.45
ResetMethod · 0.45
TotalBinsMethod · 0.45
DeviceMethod · 0.45

Tested by

no test coverage detected