-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
feature_histogram.hpp
1308 lines (1213 loc) · 49.9 KB
/
feature_histogram.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* Copyright (c) 2016 Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for
* license information.
*/
#ifndef LIGHTGBM_TREELEARNER_FEATURE_HISTOGRAM_HPP_
#define LIGHTGBM_TREELEARNER_FEATURE_HISTOGRAM_HPP_
#include <LightGBM/bin.h>
#include <LightGBM/dataset.h>
#include <LightGBM/utils/array_args.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <memory>
#include <utility>
#include <vector>
#include "monotone_constraints.hpp"
#include "split_info.hpp"
namespace LightGBM {
class FeatureMetainfo {
public:
int num_bin;
MissingType missing_type;
int8_t offset = 0;
uint32_t default_bin;
int8_t monotone_type = 0;
double penalty = 1.0;
/*! \brief pointer of tree config */
const Config* config;
BinType bin_type;
/*! \brief random number generator for extremely randomized trees */
mutable Random rand;
};
/*!
* \brief FeatureHistogram is used to construct and store a histogram for a
* feature.
*/
class FeatureHistogram {
public:
FeatureHistogram() { data_ = nullptr; }
~FeatureHistogram() {}
/*! \brief Disable copy */
FeatureHistogram& operator=(const FeatureHistogram&) = delete;
/*! \brief Disable copy */
FeatureHistogram(const FeatureHistogram&) = delete;
/*!
* \brief Init the feature histogram
* \param feature the feature data for this histogram
* \param min_num_data_one_leaf minimal number of data in one leaf
*/
void Init(hist_t* data, const FeatureMetainfo* meta) {
meta_ = meta;
data_ = data;
ResetFunc();
}
void ResetFunc() {
if (meta_->bin_type == BinType::NumericalBin) {
FuncForNumrical();
} else {
FuncForCategorical();
}
}
hist_t* RawData() { return data_; }
/*!
* \brief Subtract current histograms with other
* \param other The histogram that want to subtract
*/
void Subtract(const FeatureHistogram& other) {
for (int i = 0; i < (meta_->num_bin - meta_->offset) * 2; ++i) {
data_[i] -= other.data_[i];
}
}
void FindBestThreshold(double sum_gradient, double sum_hessian,
data_size_t num_data,
const FeatureConstraint* constraints,
double parent_output,
SplitInfo* output) {
output->default_left = true;
output->gain = kMinScore;
find_best_threshold_fun_(sum_gradient, sum_hessian + 2 * kEpsilon, num_data,
constraints, parent_output, output);
output->gain *= meta_->penalty;
}
template <bool USE_RAND, bool USE_L1, bool USE_MAX_OUTPUT, bool USE_SMOOTHING>
double BeforeNumercal(double sum_gradient, double sum_hessian, double parent_output, data_size_t num_data,
SplitInfo* output, int* rand_threshold) {
is_splittable_ = false;
output->monotone_type = meta_->monotone_type;
double gain_shift = GetLeafGain<USE_L1, USE_MAX_OUTPUT, USE_SMOOTHING>(
sum_gradient, sum_hessian, meta_->config->lambda_l1, meta_->config->lambda_l2,
meta_->config->max_delta_step, meta_->config->path_smooth, num_data, parent_output);
*rand_threshold = 0;
if (USE_RAND) {
if (meta_->num_bin - 2 > 0) {
*rand_threshold = meta_->rand.NextInt(0, meta_->num_bin - 2);
}
}
return gain_shift + meta_->config->min_gain_to_split;
}
void FuncForNumrical() {
if (meta_->config->extra_trees) {
if (meta_->config->monotone_constraints.empty()) {
FuncForNumricalL1<true, false>();
} else {
FuncForNumricalL1<true, true>();
}
} else {
if (meta_->config->monotone_constraints.empty()) {
FuncForNumricalL1<false, false>();
} else {
FuncForNumricalL1<false, true>();
}
}
}
template <bool USE_RAND, bool USE_MC>
void FuncForNumricalL1() {
if (meta_->config->lambda_l1 > 0) {
if (meta_->config->max_delta_step > 0) {
FuncForNumricalL2<USE_RAND, USE_MC, true, true>();
} else {
FuncForNumricalL2<USE_RAND, USE_MC, true, false>();
}
} else {
if (meta_->config->max_delta_step > 0) {
FuncForNumricalL2<USE_RAND, USE_MC, false, true>();
} else {
FuncForNumricalL2<USE_RAND, USE_MC, false, false>();
}
}
}
template <bool USE_RAND, bool USE_MC, bool USE_L1, bool USE_MAX_OUTPUT>
void FuncForNumricalL2() {
if (meta_->config->path_smooth > kEpsilon) {
FuncForNumricalL3<USE_RAND, USE_MC, USE_L1, USE_MAX_OUTPUT, true>();
} else {
FuncForNumricalL3<USE_RAND, USE_MC, USE_L1, USE_MAX_OUTPUT, false>();
}
}
template <bool USE_RAND, bool USE_MC, bool USE_L1, bool USE_MAX_OUTPUT, bool USE_SMOOTHING>
void FuncForNumricalL3() {
#define TEMPLATE_PREFIX USE_RAND, USE_MC, USE_L1, USE_MAX_OUTPUT, USE_SMOOTHING
#define LAMBDA_ARGUMENTS \
double sum_gradient, double sum_hessian, data_size_t num_data, \
const FeatureConstraint* constraints, double parent_output, SplitInfo *output
#define BEFORE_ARGUMENTS sum_gradient, sum_hessian, parent_output, num_data, output, &rand_threshold
#define FUNC_ARGUMENTS \
sum_gradient, sum_hessian, num_data, constraints, min_gain_shift, \
output, rand_threshold, parent_output
if (meta_->num_bin > 2 && meta_->missing_type != MissingType::None) {
if (meta_->missing_type == MissingType::Zero) {
find_best_threshold_fun_ = [=](LAMBDA_ARGUMENTS) {
int rand_threshold = 0;
double min_gain_shift =
BeforeNumercal<USE_RAND, USE_L1, USE_MAX_OUTPUT, USE_SMOOTHING>(
BEFORE_ARGUMENTS);
FindBestThresholdSequentially<TEMPLATE_PREFIX, true, true, false>(
FUNC_ARGUMENTS);
FindBestThresholdSequentially<TEMPLATE_PREFIX, false, true, false>(
FUNC_ARGUMENTS);
};
} else {
find_best_threshold_fun_ = [=](LAMBDA_ARGUMENTS) {
int rand_threshold = 0;
double min_gain_shift =
BeforeNumercal<USE_RAND, USE_L1, USE_MAX_OUTPUT, USE_SMOOTHING>(
BEFORE_ARGUMENTS);
FindBestThresholdSequentially<TEMPLATE_PREFIX, true, false, true>(
FUNC_ARGUMENTS);
FindBestThresholdSequentially<TEMPLATE_PREFIX, false, false, true>(
FUNC_ARGUMENTS);
};
}
} else {
if (meta_->missing_type != MissingType::NaN) {
find_best_threshold_fun_ = [=](LAMBDA_ARGUMENTS) {
int rand_threshold = 0;
double min_gain_shift =
BeforeNumercal<USE_RAND, USE_L1, USE_MAX_OUTPUT, USE_SMOOTHING>(
BEFORE_ARGUMENTS);
FindBestThresholdSequentially<TEMPLATE_PREFIX, true, false, false>(
FUNC_ARGUMENTS);
};
} else {
find_best_threshold_fun_ = [=](LAMBDA_ARGUMENTS) {
int rand_threshold = 0;
double min_gain_shift =
BeforeNumercal<USE_RAND, USE_L1, USE_MAX_OUTPUT, USE_SMOOTHING>(
BEFORE_ARGUMENTS);
FindBestThresholdSequentially<TEMPLATE_PREFIX, true, false, false>(
FUNC_ARGUMENTS);
output->default_left = false;
};
}
}
#undef TEMPLATE_PREFIX
#undef LAMBDA_ARGUMENTS
#undef BEFORE_ARGUMENTS
#undef FUNC_ARGURMENTS
}
void FuncForCategorical() {
if (meta_->config->extra_trees) {
if (meta_->config->monotone_constraints.empty()) {
FuncForCategoricalL1<true, false>();
} else {
FuncForCategoricalL1<true, true>();
}
} else {
if (meta_->config->monotone_constraints.empty()) {
FuncForCategoricalL1<false, false>();
} else {
FuncForCategoricalL1<false, true>();
}
}
}
template <bool USE_RAND, bool USE_MC>
void FuncForCategoricalL1() {
if (meta_->config->path_smooth > kEpsilon) {
FuncForCategoricalL2<USE_RAND, USE_MC, true>();
} else {
FuncForCategoricalL2<USE_RAND, USE_MC, false>();
}
}
template <bool USE_RAND, bool USE_MC, bool USE_SMOOTHING>
void FuncForCategoricalL2() {
#define ARGUMENTS \
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, \
std::placeholders::_4, std::placeholders::_5, std::placeholders::_6
if (meta_->config->lambda_l1 > 0) {
if (meta_->config->max_delta_step > 0) {
find_best_threshold_fun_ =
std::bind(&FeatureHistogram::FindBestThresholdCategoricalInner<
USE_RAND, USE_MC, true, true, USE_SMOOTHING>,
this, ARGUMENTS);
} else {
find_best_threshold_fun_ =
std::bind(&FeatureHistogram::FindBestThresholdCategoricalInner<
USE_RAND, USE_MC, true, false, USE_SMOOTHING>,
this, ARGUMENTS);
}
} else {
if (meta_->config->max_delta_step > 0) {
find_best_threshold_fun_ =
std::bind(&FeatureHistogram::FindBestThresholdCategoricalInner<
USE_RAND, USE_MC, false, true, USE_SMOOTHING>,
this, ARGUMENTS);
} else {
find_best_threshold_fun_ =
std::bind(&FeatureHistogram::FindBestThresholdCategoricalInner<
USE_RAND, USE_MC, false, false, USE_SMOOTHING>,
this, ARGUMENTS);
}
}
#undef ARGUMENTS
}
template <bool USE_RAND, bool USE_MC, bool USE_L1, bool USE_MAX_OUTPUT, bool USE_SMOOTHING>
void FindBestThresholdCategoricalInner(double sum_gradient,
double sum_hessian,
data_size_t num_data,
const FeatureConstraint* constraints,
double parent_output,
SplitInfo* output) {
is_splittable_ = false;
output->default_left = false;
double best_gain = kMinScore;
data_size_t best_left_count = 0;
double best_sum_left_gradient = 0;
double best_sum_left_hessian = 0;
double gain_shift;
if (USE_MC) {
constraints->InitCumulativeConstraints(true);
}
if (USE_SMOOTHING) {
gain_shift = GetLeafGainGivenOutput<USE_L1>(
sum_gradient, sum_hessian, meta_->config->lambda_l1, meta_->config->lambda_l2, parent_output);
} else {
// Need special case for no smoothing to preserve existing behaviour. If no smoothing, the parent output is calculated
// with the larger categorical l2, whereas min_split_gain uses the original l2.
gain_shift = GetLeafGain<USE_L1, USE_MAX_OUTPUT, false>(sum_gradient, sum_hessian,
meta_->config->lambda_l1, meta_->config->lambda_l2, meta_->config->max_delta_step, 0,
num_data, 0);
}
double min_gain_shift = gain_shift + meta_->config->min_gain_to_split;
const int8_t offset = meta_->offset;
const int bin_start = 1 - offset;
const int bin_end = meta_->num_bin - offset;
int used_bin = -1;
std::vector<int> sorted_idx;
double l2 = meta_->config->lambda_l2;
bool use_onehot = meta_->num_bin <= meta_->config->max_cat_to_onehot;
int best_threshold = -1;
int best_dir = 1;
const double cnt_factor = num_data / sum_hessian;
int rand_threshold = 0;
if (use_onehot) {
if (USE_RAND) {
if (bin_end - bin_start > 0) {
rand_threshold = meta_->rand.NextInt(bin_start, bin_end);
}
}
for (int t = bin_start; t < bin_end; ++t) {
const auto grad = GET_GRAD(data_, t);
const auto hess = GET_HESS(data_, t);
data_size_t cnt =
static_cast<data_size_t>(Common::RoundInt(hess * cnt_factor));
// if data not enough, or sum hessian too small
if (cnt < meta_->config->min_data_in_leaf ||
hess < meta_->config->min_sum_hessian_in_leaf) {
continue;
}
data_size_t other_count = num_data - cnt;
// if data not enough
if (other_count < meta_->config->min_data_in_leaf) {
continue;
}
double sum_other_hessian = sum_hessian - hess - kEpsilon;
// if sum hessian too small
if (sum_other_hessian < meta_->config->min_sum_hessian_in_leaf) {
continue;
}
double sum_other_gradient = sum_gradient - grad;
if (USE_RAND) {
if (t != rand_threshold) {
continue;
}
}
// current split gain
double current_gain = GetSplitGains<USE_MC, USE_L1, USE_MAX_OUTPUT, USE_SMOOTHING>(
sum_other_gradient, sum_other_hessian, grad, hess + kEpsilon,
meta_->config->lambda_l1, l2, meta_->config->max_delta_step,
constraints, 0, meta_->config->path_smooth, other_count, cnt, parent_output);
// gain with split is worse than without split
if (current_gain <= min_gain_shift) {
continue;
}
// mark as able to be split
is_splittable_ = true;
// better split point
if (current_gain > best_gain) {
best_threshold = t;
best_sum_left_gradient = grad;
best_sum_left_hessian = hess + kEpsilon;
best_left_count = cnt;
best_gain = current_gain;
}
}
} else {
for (int i = bin_start; i < bin_end; ++i) {
if (Common::RoundInt(GET_HESS(data_, i) * cnt_factor) >=
meta_->config->cat_smooth) {
sorted_idx.push_back(i);
}
}
used_bin = static_cast<int>(sorted_idx.size());
l2 += meta_->config->cat_l2;
auto ctr_fun = [this](double sum_grad, double sum_hess) {
return (sum_grad) / (sum_hess + meta_->config->cat_smooth);
};
std::stable_sort(
sorted_idx.begin(), sorted_idx.end(), [this, &ctr_fun](int i, int j) {
return ctr_fun(GET_GRAD(data_, i), GET_HESS(data_, i)) <
ctr_fun(GET_GRAD(data_, j), GET_HESS(data_, j));
});
std::vector<int> find_direction(1, 1);
std::vector<int> start_position(1, 0);
find_direction.push_back(-1);
start_position.push_back(used_bin - 1);
const int max_num_cat =
std::min(meta_->config->max_cat_threshold, (used_bin + 1) / 2);
int max_threshold = std::max(std::min(max_num_cat, used_bin) - 1, 0);
if (USE_RAND) {
if (max_threshold > 0) {
rand_threshold = meta_->rand.NextInt(0, max_threshold);
}
}
is_splittable_ = false;
for (size_t out_i = 0; out_i < find_direction.size(); ++out_i) {
auto dir = find_direction[out_i];
auto start_pos = start_position[out_i];
data_size_t min_data_per_group = meta_->config->min_data_per_group;
data_size_t cnt_cur_group = 0;
double sum_left_gradient = 0.0f;
double sum_left_hessian = kEpsilon;
data_size_t left_count = 0;
for (int i = 0; i < used_bin && i < max_num_cat; ++i) {
auto t = sorted_idx[start_pos];
start_pos += dir;
const auto grad = GET_GRAD(data_, t);
const auto hess = GET_HESS(data_, t);
data_size_t cnt =
static_cast<data_size_t>(Common::RoundInt(hess * cnt_factor));
sum_left_gradient += grad;
sum_left_hessian += hess;
left_count += cnt;
cnt_cur_group += cnt;
if (left_count < meta_->config->min_data_in_leaf ||
sum_left_hessian < meta_->config->min_sum_hessian_in_leaf) {
continue;
}
data_size_t right_count = num_data - left_count;
if (right_count < meta_->config->min_data_in_leaf ||
right_count < min_data_per_group) {
break;
}
double sum_right_hessian = sum_hessian - sum_left_hessian;
if (sum_right_hessian < meta_->config->min_sum_hessian_in_leaf) {
break;
}
if (cnt_cur_group < min_data_per_group) {
continue;
}
cnt_cur_group = 0;
double sum_right_gradient = sum_gradient - sum_left_gradient;
if (USE_RAND) {
if (i != rand_threshold) {
continue;
}
}
double current_gain = GetSplitGains<USE_MC, USE_L1, USE_MAX_OUTPUT, USE_SMOOTHING>(
sum_left_gradient, sum_left_hessian, sum_right_gradient,
sum_right_hessian, meta_->config->lambda_l1, l2,
meta_->config->max_delta_step, constraints, 0, meta_->config->path_smooth,
left_count, right_count, parent_output);
if (current_gain <= min_gain_shift) {
continue;
}
is_splittable_ = true;
if (current_gain > best_gain) {
best_left_count = left_count;
best_sum_left_gradient = sum_left_gradient;
best_sum_left_hessian = sum_left_hessian;
best_threshold = i;
best_gain = current_gain;
best_dir = dir;
}
}
}
}
if (is_splittable_) {
output->left_output = CalculateSplittedLeafOutput<USE_MC, USE_L1, USE_MAX_OUTPUT, USE_SMOOTHING>(
best_sum_left_gradient, best_sum_left_hessian,
meta_->config->lambda_l1, l2, meta_->config->max_delta_step,
constraints->LeftToBasicConstraint(), meta_->config->path_smooth, best_left_count, parent_output);
output->left_count = best_left_count;
output->left_sum_gradient = best_sum_left_gradient;
output->left_sum_hessian = best_sum_left_hessian - kEpsilon;
output->right_output = CalculateSplittedLeafOutput<USE_MC, USE_L1, USE_MAX_OUTPUT, USE_SMOOTHING>(
sum_gradient - best_sum_left_gradient,
sum_hessian - best_sum_left_hessian, meta_->config->lambda_l1, l2,
meta_->config->max_delta_step, constraints->RightToBasicConstraint(), meta_->config->path_smooth,
num_data - best_left_count, parent_output);
output->right_count = num_data - best_left_count;
output->right_sum_gradient = sum_gradient - best_sum_left_gradient;
output->right_sum_hessian =
sum_hessian - best_sum_left_hessian - kEpsilon;
output->gain = best_gain - min_gain_shift;
if (use_onehot) {
output->num_cat_threshold = 1;
output->cat_threshold =
std::vector<uint32_t>(1, static_cast<uint32_t>(best_threshold + offset));
} else {
output->num_cat_threshold = best_threshold + 1;
output->cat_threshold =
std::vector<uint32_t>(output->num_cat_threshold);
if (best_dir == 1) {
for (int i = 0; i < output->num_cat_threshold; ++i) {
auto t = sorted_idx[i] + offset;
output->cat_threshold[i] = t;
}
} else {
for (int i = 0; i < output->num_cat_threshold; ++i) {
auto t = sorted_idx[used_bin - 1 - i] + offset;
output->cat_threshold[i] = t;
}
}
}
output->monotone_type = 0;
}
}
void GatherInfoForThreshold(double sum_gradient, double sum_hessian,
uint32_t threshold, data_size_t num_data,
double parent_output, SplitInfo* output) {
if (meta_->bin_type == BinType::NumericalBin) {
GatherInfoForThresholdNumerical(sum_gradient, sum_hessian, threshold,
num_data, parent_output, output);
} else {
GatherInfoForThresholdCategorical(sum_gradient, sum_hessian, threshold,
num_data, parent_output, output);
}
}
void GatherInfoForThresholdNumerical(double sum_gradient, double sum_hessian,
uint32_t threshold, data_size_t num_data,
double parent_output, SplitInfo* output) {
bool use_smoothing = meta_->config->path_smooth > kEpsilon;
if (use_smoothing) {
GatherInfoForThresholdNumericalInner<true>(sum_gradient, sum_hessian,
threshold, num_data,
parent_output, output);
} else {
GatherInfoForThresholdNumericalInner<false>(sum_gradient, sum_hessian,
threshold, num_data,
parent_output, output);
}
}
template<bool USE_SMOOTHING>
void GatherInfoForThresholdNumericalInner(double sum_gradient, double sum_hessian,
uint32_t threshold, data_size_t num_data,
double parent_output, SplitInfo* output) {
double gain_shift = GetLeafGainGivenOutput<true>(
sum_gradient, sum_hessian, meta_->config->lambda_l1,
meta_->config->lambda_l2, parent_output);
double min_gain_shift = gain_shift + meta_->config->min_gain_to_split;
// do stuff here
const int8_t offset = meta_->offset;
double sum_right_gradient = 0.0f;
double sum_right_hessian = kEpsilon;
data_size_t right_count = 0;
// set values
bool use_na_as_missing = false;
bool skip_default_bin = false;
if (meta_->missing_type == MissingType::Zero) {
skip_default_bin = true;
} else if (meta_->missing_type == MissingType::NaN) {
use_na_as_missing = true;
}
int t = meta_->num_bin - 1 - offset - use_na_as_missing;
const int t_end = 1 - offset;
const double cnt_factor = num_data / sum_hessian;
// from right to left, and we don't need data in bin0
for (; t >= t_end; --t) {
if (static_cast<uint32_t>(t + offset) <= threshold) {
break;
}
// need to skip default bin
if (skip_default_bin &&
(t + offset) == static_cast<int>(meta_->default_bin)) {
continue;
}
const auto grad = GET_GRAD(data_, t);
const auto hess = GET_HESS(data_, t);
data_size_t cnt =
static_cast<data_size_t>(Common::RoundInt(hess * cnt_factor));
sum_right_gradient += grad;
sum_right_hessian += hess;
right_count += cnt;
}
double sum_left_gradient = sum_gradient - sum_right_gradient;
double sum_left_hessian = sum_hessian - sum_right_hessian;
data_size_t left_count = num_data - right_count;
double current_gain =
GetLeafGain<true, true, USE_SMOOTHING>(
sum_left_gradient, sum_left_hessian, meta_->config->lambda_l1,
meta_->config->lambda_l2, meta_->config->max_delta_step,
meta_->config->path_smooth, left_count, parent_output) +
GetLeafGain<true, true, USE_SMOOTHING>(
sum_right_gradient, sum_right_hessian, meta_->config->lambda_l1,
meta_->config->lambda_l2, meta_->config->max_delta_step,
meta_->config->path_smooth, right_count, parent_output);
// gain with split is worse than without split
if (std::isnan(current_gain) || current_gain <= min_gain_shift) {
output->gain = kMinScore;
Log::Warning(
"'Forced Split' will be ignored since the gain getting worse.");
return;
}
// update split information
output->threshold = threshold;
output->left_output = CalculateSplittedLeafOutput<true, true, USE_SMOOTHING>(
sum_left_gradient, sum_left_hessian, meta_->config->lambda_l1,
meta_->config->lambda_l2, meta_->config->max_delta_step,
meta_->config->path_smooth, left_count, parent_output);
output->left_count = left_count;
output->left_sum_gradient = sum_left_gradient;
output->left_sum_hessian = sum_left_hessian - kEpsilon;
output->right_output = CalculateSplittedLeafOutput<true, true, USE_SMOOTHING>(
sum_gradient - sum_left_gradient, sum_hessian - sum_left_hessian,
meta_->config->lambda_l1, meta_->config->lambda_l2,
meta_->config->max_delta_step, meta_->config->path_smooth,
right_count, parent_output);
output->right_count = num_data - left_count;
output->right_sum_gradient = sum_gradient - sum_left_gradient;
output->right_sum_hessian = sum_hessian - sum_left_hessian - kEpsilon;
output->gain = current_gain - min_gain_shift;
output->default_left = true;
}
void GatherInfoForThresholdCategorical(double sum_gradient, double sum_hessian,
uint32_t threshold, data_size_t num_data,
double parent_output, SplitInfo* output) {
bool use_smoothing = meta_->config->path_smooth > kEpsilon;
if (use_smoothing) {
GatherInfoForThresholdCategoricalInner<true>(sum_gradient, sum_hessian, threshold,
num_data, parent_output, output);
} else {
GatherInfoForThresholdCategoricalInner<false>(sum_gradient, sum_hessian, threshold,
num_data, parent_output, output);
}
}
template<bool USE_SMOOTHING>
void GatherInfoForThresholdCategoricalInner(double sum_gradient,
double sum_hessian, uint32_t threshold,
data_size_t num_data, double parent_output,
SplitInfo* output) {
// get SplitInfo for a given one-hot categorical split.
output->default_left = false;
double gain_shift = GetLeafGainGivenOutput<true>(
sum_gradient, sum_hessian, meta_->config->lambda_l1, meta_->config->lambda_l2, parent_output);
double min_gain_shift = gain_shift + meta_->config->min_gain_to_split;
if (threshold >= static_cast<uint32_t>(meta_->num_bin) || threshold == 0) {
output->gain = kMinScore;
Log::Warning("Invalid categorical threshold split");
return;
}
const double cnt_factor = num_data / sum_hessian;
const auto grad = GET_GRAD(data_, threshold - meta_->offset);
const auto hess = GET_HESS(data_, threshold - meta_->offset);
data_size_t cnt =
static_cast<data_size_t>(Common::RoundInt(hess * cnt_factor));
double l2 = meta_->config->lambda_l2;
data_size_t left_count = cnt;
data_size_t right_count = num_data - left_count;
double sum_left_hessian = hess + kEpsilon;
double sum_right_hessian = sum_hessian - sum_left_hessian;
double sum_left_gradient = grad;
double sum_right_gradient = sum_gradient - sum_left_gradient;
// current split gain
double current_gain =
GetLeafGain<true, true, USE_SMOOTHING>(sum_right_gradient, sum_right_hessian,
meta_->config->lambda_l1, l2,
meta_->config->max_delta_step,
meta_->config->path_smooth, right_count,
parent_output) +
GetLeafGain<true, true, USE_SMOOTHING>(sum_left_gradient, sum_left_hessian,
meta_->config->lambda_l1, l2,
meta_->config->max_delta_step,
meta_->config->path_smooth, left_count,
parent_output);
if (std::isnan(current_gain) || current_gain <= min_gain_shift) {
output->gain = kMinScore;
Log::Warning(
"'Forced Split' will be ignored since the gain getting worse.");
return;
}
output->left_output = CalculateSplittedLeafOutput<true, true, USE_SMOOTHING>(
sum_left_gradient, sum_left_hessian, meta_->config->lambda_l1, l2,
meta_->config->max_delta_step, meta_->config->path_smooth, left_count,
parent_output);
output->left_count = left_count;
output->left_sum_gradient = sum_left_gradient;
output->left_sum_hessian = sum_left_hessian - kEpsilon;
output->right_output = CalculateSplittedLeafOutput<true, true, USE_SMOOTHING>(
sum_right_gradient, sum_right_hessian, meta_->config->lambda_l1, l2,
meta_->config->max_delta_step, meta_->config->path_smooth, right_count,
parent_output);
output->right_count = right_count;
output->right_sum_gradient = sum_gradient - sum_left_gradient;
output->right_sum_hessian = sum_right_hessian - kEpsilon;
output->gain = current_gain - min_gain_shift;
output->num_cat_threshold = 1;
output->cat_threshold = std::vector<uint32_t>(1, threshold);
}
/*!
* \brief Binary size of this histogram
*/
int SizeOfHistgram() const {
return (meta_->num_bin - meta_->offset) * kHistEntrySize;
}
/*!
* \brief Restore histogram from memory
*/
void FromMemory(char* memory_data) {
std::memcpy(data_, memory_data,
(meta_->num_bin - meta_->offset) * kHistEntrySize);
}
/*!
* \brief True if this histogram can be splitted
*/
bool is_splittable() { return is_splittable_; }
/*!
* \brief Set splittable to this histogram
*/
void set_is_splittable(bool val) { is_splittable_ = val; }
static double ThresholdL1(double s, double l1) {
const double reg_s = std::max(0.0, std::fabs(s) - l1);
return Common::Sign(s) * reg_s;
}
template <bool USE_L1, bool USE_MAX_OUTPUT, bool USE_SMOOTHING>
static double CalculateSplittedLeafOutput(double sum_gradients,
double sum_hessians, double l1,
double l2, double max_delta_step,
double smoothing, data_size_t num_data,
double parent_output) {
double ret;
if (USE_L1) {
ret = -ThresholdL1(sum_gradients, l1) / (sum_hessians + l2);
} else {
ret = -sum_gradients / (sum_hessians + l2);
}
if (USE_MAX_OUTPUT) {
if (max_delta_step > 0 && std::fabs(ret) > max_delta_step) {
ret = Common::Sign(ret) * max_delta_step;
}
}
if (USE_SMOOTHING) {
ret = ret * (num_data / smoothing) / (num_data / smoothing + 1) \
+ parent_output / (num_data / smoothing + 1);
}
return ret;
}
template <bool USE_MC, bool USE_L1, bool USE_MAX_OUTPUT, bool USE_SMOOTHING>
static double CalculateSplittedLeafOutput(
double sum_gradients, double sum_hessians, double l1, double l2,
double max_delta_step, const BasicConstraint& constraints,
double smoothing, data_size_t num_data, double parent_output) {
double ret = CalculateSplittedLeafOutput<USE_L1, USE_MAX_OUTPUT, USE_SMOOTHING>(
sum_gradients, sum_hessians, l1, l2, max_delta_step, smoothing, num_data, parent_output);
if (USE_MC) {
if (ret < constraints.min) {
ret = constraints.min;
} else if (ret > constraints.max) {
ret = constraints.max;
}
}
return ret;
}
private:
template <bool USE_MC, bool USE_L1, bool USE_MAX_OUTPUT, bool USE_SMOOTHING>
static double GetSplitGains(double sum_left_gradients,
double sum_left_hessians,
double sum_right_gradients,
double sum_right_hessians, double l1, double l2,
double max_delta_step,
const FeatureConstraint* constraints,
int8_t monotone_constraint,
double smoothing,
data_size_t left_count,
data_size_t right_count,
double parent_output) {
if (!USE_MC) {
return GetLeafGain<USE_L1, USE_MAX_OUTPUT, USE_SMOOTHING>(sum_left_gradients,
sum_left_hessians, l1, l2,
max_delta_step, smoothing,
left_count, parent_output) +
GetLeafGain<USE_L1, USE_MAX_OUTPUT, USE_SMOOTHING>(sum_right_gradients,
sum_right_hessians, l1, l2,
max_delta_step, smoothing,
right_count, parent_output);
} else {
double left_output =
CalculateSplittedLeafOutput<USE_MC, USE_L1, USE_MAX_OUTPUT, USE_SMOOTHING>(
sum_left_gradients, sum_left_hessians, l1, l2, max_delta_step,
constraints->LeftToBasicConstraint(), smoothing, left_count, parent_output);
double right_output =
CalculateSplittedLeafOutput<USE_MC, USE_L1, USE_MAX_OUTPUT, USE_SMOOTHING>(
sum_right_gradients, sum_right_hessians, l1, l2, max_delta_step,
constraints->RightToBasicConstraint(), smoothing, right_count, parent_output);
if (((monotone_constraint > 0) && (left_output > right_output)) ||
((monotone_constraint < 0) && (left_output < right_output))) {
return 0;
}
return GetLeafGainGivenOutput<USE_L1>(
sum_left_gradients, sum_left_hessians, l1, l2, left_output) +
GetLeafGainGivenOutput<USE_L1>(
sum_right_gradients, sum_right_hessians, l1, l2, right_output);
}
}
template <bool USE_L1, bool USE_MAX_OUTPUT, bool USE_SMOOTHING>
static double GetLeafGain(double sum_gradients, double sum_hessians,
double l1, double l2, double max_delta_step,
double smoothing, data_size_t num_data, double parent_output) {
if (!USE_MAX_OUTPUT && !USE_SMOOTHING) {
if (USE_L1) {
const double sg_l1 = ThresholdL1(sum_gradients, l1);
return (sg_l1 * sg_l1) / (sum_hessians + l2);
} else {
return (sum_gradients * sum_gradients) / (sum_hessians + l2);
}
} else {
double output = CalculateSplittedLeafOutput<USE_L1, USE_MAX_OUTPUT, USE_SMOOTHING>(
sum_gradients, sum_hessians, l1, l2, max_delta_step, smoothing, num_data, parent_output);
return GetLeafGainGivenOutput<USE_L1>(sum_gradients, sum_hessians, l1, l2, output);
}
}
template <bool USE_L1>
static double GetLeafGainGivenOutput(double sum_gradients,
double sum_hessians, double l1,
double l2, double output) {
if (USE_L1) {
const double sg_l1 = ThresholdL1(sum_gradients, l1);
return -(2.0 * sg_l1 * output + (sum_hessians + l2) * output * output);
} else {
return -(2.0 * sum_gradients * output +
(sum_hessians + l2) * output * output);
}
}
template <bool USE_RAND, bool USE_MC, bool USE_L1, bool USE_MAX_OUTPUT, bool USE_SMOOTHING,
bool REVERSE, bool SKIP_DEFAULT_BIN, bool NA_AS_MISSING>
void FindBestThresholdSequentially(double sum_gradient, double sum_hessian,
data_size_t num_data,
const FeatureConstraint* constraints,
double min_gain_shift, SplitInfo* output,
int rand_threshold, double parent_output) {
const int8_t offset = meta_->offset;
double best_sum_left_gradient = NAN;
double best_sum_left_hessian = NAN;
double best_gain = kMinScore;
data_size_t best_left_count = 0;
uint32_t best_threshold = static_cast<uint32_t>(meta_->num_bin);
const double cnt_factor = num_data / sum_hessian;
BasicConstraint best_right_constraints;
BasicConstraint best_left_constraints;
bool constraint_update_necessary =
USE_MC && constraints->ConstraintDifferentDependingOnThreshold();
if (USE_MC) {
constraints->InitCumulativeConstraints(REVERSE);
}
if (REVERSE) {
double sum_right_gradient = 0.0f;
double sum_right_hessian = kEpsilon;
data_size_t right_count = 0;
int t = meta_->num_bin - 1 - offset - NA_AS_MISSING;
const int t_end = 1 - offset;
// from right to left, and we don't need data in bin0
for (; t >= t_end; --t) {
// need to skip default bin
if (SKIP_DEFAULT_BIN) {
if ((t + offset) == static_cast<int>(meta_->default_bin)) {
continue;
}
}
const auto grad = GET_GRAD(data_, t);
const auto hess = GET_HESS(data_, t);
data_size_t cnt =
static_cast<data_size_t>(Common::RoundInt(hess * cnt_factor));
sum_right_gradient += grad;
sum_right_hessian += hess;
right_count += cnt;
// if data not enough, or sum hessian too small
if (right_count < meta_->config->min_data_in_leaf ||
sum_right_hessian < meta_->config->min_sum_hessian_in_leaf) {
continue;
}
data_size_t left_count = num_data - right_count;
// if data not enough
if (left_count < meta_->config->min_data_in_leaf) {
break;
}
double sum_left_hessian = sum_hessian - sum_right_hessian;
// if sum hessian too small
if (sum_left_hessian < meta_->config->min_sum_hessian_in_leaf) {
break;
}
double sum_left_gradient = sum_gradient - sum_right_gradient;
if (USE_RAND) {
if (t - 1 + offset != rand_threshold) {
continue;
}
}
if (USE_MC && constraint_update_necessary) {
constraints->Update(t + offset);
}
// current split gain
double current_gain = GetSplitGains<USE_MC, USE_L1, USE_MAX_OUTPUT, USE_SMOOTHING>(
sum_left_gradient, sum_left_hessian, sum_right_gradient,
sum_right_hessian, meta_->config->lambda_l1,
meta_->config->lambda_l2, meta_->config->max_delta_step,
constraints, meta_->monotone_type, meta_->config->path_smooth,
left_count, right_count, parent_output);
// gain with split is worse than without split
if (current_gain <= min_gain_shift) {
continue;
}
// mark as able to be split
is_splittable_ = true;
// better split point
if (current_gain > best_gain) {
if (USE_MC) {
best_right_constraints = constraints->RightToBasicConstraint();
best_left_constraints = constraints->LeftToBasicConstraint();
if (best_right_constraints.min > best_right_constraints.max ||
best_left_constraints.min > best_left_constraints.max) {
continue;
}
}
best_left_count = left_count;
best_sum_left_gradient = sum_left_gradient;
best_sum_left_hessian = sum_left_hessian;
// left is <= threshold, right is > threshold. so this is t-1
best_threshold = static_cast<uint32_t>(t - 1 + offset);
best_gain = current_gain;
}
}
} else {
double sum_left_gradient = 0.0f;
double sum_left_hessian = kEpsilon;
data_size_t left_count = 0;
int t = 0;
const int t_end = meta_->num_bin - 2 - offset;
if (NA_AS_MISSING) {
if (offset == 1) {
sum_left_gradient = sum_gradient;
sum_left_hessian = sum_hessian - kEpsilon;
left_count = num_data;
for (int i = 0; i < meta_->num_bin - offset; ++i) {
const auto grad = GET_GRAD(data_, i);
const auto hess = GET_HESS(data_, i);
data_size_t cnt =
static_cast<data_size_t>(Common::RoundInt(hess * cnt_factor));
sum_left_gradient -= grad;
sum_left_hessian -= hess;
left_count -= cnt;
}
t = -1;
}
}
for (; t <= t_end; ++t) {
if (SKIP_DEFAULT_BIN) {
if ((t + offset) == static_cast<int>(meta_->default_bin)) {
continue;
}
}
if (t >= 0) {
sum_left_gradient += GET_GRAD(data_, t);
sum_left_hessian += GET_HESS(data_, t);
left_count += static_cast<data_size_t>(
Common::RoundInt(GET_HESS(data_, t) * cnt_factor));
}