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

Function CheckSampling

tests/cpp/tree/test_sampler.h:47–77  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

45}
46
47inline void CheckSampling(float subsample, bst_target_t n_targets, bool check_sum,
48 std::vector<GradientPairPrecise> const& sum_sampled_gpair,
49 std::vector<GradientPairPrecise> const& sum_gpair,
50 linalg::MatrixView<GradientPair> h_gpair) {
51 auto n_samples = h_gpair.Shape(0);
52 bst_idx_t sample_rows = n_samples * subsample;
53
54 // Verify gradient sums per target
55 for (bst_target_t t = 0; t < n_targets; ++t) {
56 if (check_sum) {
57 // Gradient-based sampling preserves the sum approximately
58 ASSERT_NEAR(sum_gpair[t].GetGrad(), sum_sampled_gpair[t].GetGrad(), 0.03f * n_samples);
59 ASSERT_NEAR(sum_gpair[t].GetHess(), sum_sampled_gpair[t].GetHess(), 0.03f * n_samples);
60 } else {
61 // Uniform sampling preserves the mean approximately
62 auto mean_grad = sum_gpair[t].GetGrad() / n_samples;
63 auto mean_hess = sum_gpair[t].GetHess() / n_samples;
64 auto sampled_mean_grad = sum_sampled_gpair[t].GetGrad() / sample_rows;
65 auto sampled_mean_hess = sum_sampled_gpair[t].GetHess() / sample_rows;
66 ASSERT_NEAR(mean_grad, sampled_mean_grad, mean_grad * 0.1);
67 ASSERT_NEAR(mean_hess, sampled_mean_hess, mean_hess * 0.1);
68 }
69 }
70
71 // Verify multi-target consistency and sample fraction (reuse CheckSampledRows)
72 auto sampled_count = CheckSampledRows(h_gpair, h_gpair);
73 if (subsample < 1.0f) {
74 double sampled_fraction = static_cast<double>(sampled_count) / n_samples;
75 ASSERT_NEAR(sampled_fraction, subsample, 0.05);
76 }
77}
78
79// Validate that value gradients are reweighted using the provided threshold and reg_abs_grad.
80inline void CheckValueReweight(linalg::MatrixView<GradientPair const> sampled_split,

Callers 1

VerifySamplingFunction · 0.85

Calls 4

CheckSampledRowsFunction · 0.85
ShapeMethod · 0.80
GetGradMethod · 0.80
GetHessMethod · 0.80

Tested by

no test coverage detected