1010
1111namespace LightGBM {
1212
13- void GBDT::PredictRaw (const double * features, double * output, const PredictionEarlyStopInstance* early_stop) const {
14- int early_stop_round_counter = 0 ;
15- // set zero
16- std::memset (output, 0 , sizeof (double ) * num_tree_per_iteration_);
17- const int end_iteration_for_pred = start_iteration_for_pred_ + num_iteration_for_pred_;
18- for (int i = start_iteration_for_pred_; i < end_iteration_for_pred; ++i) {
19- // predict all the trees for one iteration
20- for (int k = 0 ; k < num_tree_per_iteration_; ++k) {
21- output[k] += models_[i * num_tree_per_iteration_ + k]->Predict (features);
13+ void GBDT::PredictRaw (const double * features, double * output, const PredictionEarlyStopInstance* early_stop) const {
14+ int early_stop_round_counter = 0 ;
15+ // set zero
16+ std::memset (output, 0 , sizeof (double ) * num_tree_per_iteration_);
17+ const int end_iteration_for_pred = start_iteration_for_pred_ + num_iteration_for_pred_;
18+ for (int i = start_iteration_for_pred_; i < end_iteration_for_pred; ++i) {
19+ // predict all the trees for one iteration
20+ for (int k = 0 ; k < num_tree_per_iteration_; ++k) {
21+ output[k] += models_[i * num_tree_per_iteration_ + k]->Predict (features);
22+ }
23+ // check early stopping
24+ ++early_stop_round_counter;
25+ if (early_stop->round_period == early_stop_round_counter) {
26+ if (early_stop->callback_function (output, num_tree_per_iteration_)) {
27+ return ;
28+ }
29+ early_stop_round_counter = 0 ;
30+ }
31+ }
2232 }
23- // check early stopping
24- ++early_stop_round_counter;
25- if (early_stop->round_period == early_stop_round_counter) {
26- if (early_stop->callback_function (output, num_tree_per_iteration_)) {
27- return ;
28- }
29- early_stop_round_counter = 0 ;
30- }
31- }
32- }
3333
34- void GBDT::PredictRawByMap (const std::unordered_map<int , double >& features, double * output, const PredictionEarlyStopInstance* early_stop) const {
35- int early_stop_round_counter = 0 ;
36- // set zero
37- std::memset (output, 0 , sizeof (double ) * num_tree_per_iteration_);
38- const int end_iteration_for_pred = start_iteration_for_pred_ + num_iteration_for_pred_;
39- for (int i = start_iteration_for_pred_; i < end_iteration_for_pred; ++i) {
40- // predict all the trees for one iteration
41- for (int k = 0 ; k < num_tree_per_iteration_; ++k) {
42- output[k] += models_[i * num_tree_per_iteration_ + k]->PredictByMap (features);
43- }
44- // check early stopping
45- ++early_stop_round_counter;
46- if (early_stop->round_period == early_stop_round_counter) {
47- if (early_stop->callback_function (output, num_tree_per_iteration_)) {
48- return ;
49- }
50- early_stop_round_counter = 0 ;
34+ void GBDT::PredictRawByMap (const std::unordered_map<int , double >& features, double * output, const PredictionEarlyStopInstance* early_stop) const {
35+ int early_stop_round_counter = 0 ;
36+ // set zero
37+ std::memset (output, 0 , sizeof (double ) * num_tree_per_iteration_);
38+ const int end_iteration_for_pred = start_iteration_for_pred_ + num_iteration_for_pred_;
39+ for (int i = start_iteration_for_pred_; i < end_iteration_for_pred; ++i) {
40+ // predict all the trees for one iteration
41+ for (int k = 0 ; k < num_tree_per_iteration_; ++k) {
42+ output[k] += models_[i * num_tree_per_iteration_ + k]->PredictByMap (features);
43+ }
44+ // check early stopping
45+ ++early_stop_round_counter;
46+ if (early_stop->round_period == early_stop_round_counter) {
47+ if (early_stop->callback_function (output, num_tree_per_iteration_)) {
48+ return ;
49+ }
50+ early_stop_round_counter = 0 ;
51+ }
52+ }
5153 }
52- }
53- }
5454
55- void GBDT::Predict (const double * features, double * output, const PredictionEarlyStopInstance* early_stop) const {
56- PredictRaw (features, output, early_stop);
57- if (average_output_) {
58- for (int k = 0 ; k < num_tree_per_iteration_; ++k) {
59- output[k] /= num_iteration_for_pred_;
55+ void GBDT::Predict (const double * features, double * output, const PredictionEarlyStopInstance* early_stop) const {
56+ PredictRaw (features, output, early_stop);
57+ if (average_output_) {
58+ for (int k = 0 ; k < num_tree_per_iteration_; ++k) {
59+ output[k] /= num_iteration_for_pred_;
60+ }
61+ }
62+ if (objective_function_ != nullptr ) {
63+ objective_function_->ConvertOutput (output, output);
64+ }
6065 }
61- }
62- if (objective_function_ != nullptr ) {
63- objective_function_->ConvertOutput (output, output);
64- }
65- }
6666
67- void GBDT::PredictByMap (const std::unordered_map<int , double >& features, double * output, const PredictionEarlyStopInstance* early_stop) const {
68- PredictRawByMap (features, output, early_stop);
69- if (average_output_) {
70- for (int k = 0 ; k < num_tree_per_iteration_; ++k) {
71- output[k] /= num_iteration_for_pred_;
67+ void GBDT::PredictByMap (const std::unordered_map<int , double >& features, double * output, const PredictionEarlyStopInstance* early_stop) const {
68+ PredictRawByMap (features, output, early_stop);
69+ if (average_output_) {
70+ for (int k = 0 ; k < num_tree_per_iteration_; ++k) {
71+ output[k] /= num_iteration_for_pred_;
72+ }
73+ }
74+ if (objective_function_ != nullptr ) {
75+ objective_function_->ConvertOutput (output, output);
76+ }
7277 }
73- }
74- if (objective_function_ != nullptr ) {
75- objective_function_->ConvertOutput (output, output);
76- }
77- }
7878
79- void GBDT::PredictLeafIndex (const double * features, double * output) const {
80- int start_tree = start_iteration_for_pred_ * num_tree_per_iteration_;
81- int num_trees = num_iteration_for_pred_ * num_tree_per_iteration_;
82- const auto * models_ptr = models_.data () + start_tree;
83- for (int i = 0 ; i < num_trees; ++i) {
84- output[i] = models_ptr[i]->PredictLeafIndex (features);
85- }
86- }
79+ void GBDT::PredictLeafIndex (const double * features, double * output) const {
80+ int start_tree = start_iteration_for_pred_ * num_tree_per_iteration_;
81+ int num_trees = num_iteration_for_pred_ * num_tree_per_iteration_;
82+ const auto * models_ptr = models_.data () + start_tree;
83+ for (int i = 0 ; i < num_trees; ++i) {
84+ output[i] = models_ptr[i]->PredictLeafIndex (features);
85+ }
86+ }
8787
88- void GBDT::PredictLeafIndexByMap (const std::unordered_map<int , double >& features, double * output) const {
89- int start_tree = start_iteration_for_pred_ * num_tree_per_iteration_;
90- int num_trees = num_iteration_for_pred_ * num_tree_per_iteration_;
91- const auto * models_ptr = models_.data () + start_tree;
92- for (int i = 0 ; i < num_trees; ++i) {
93- output[i] = models_ptr[i]->PredictLeafIndexByMap (features);
94- }
95- }
88+ void GBDT::PredictLeafIndexByMap (const std::unordered_map<int , double >& features, double * output) const {
89+ int start_tree = start_iteration_for_pred_ * num_tree_per_iteration_;
90+ int num_trees = num_iteration_for_pred_ * num_tree_per_iteration_;
91+ const auto * models_ptr = models_.data () + start_tree;
92+ for (int i = 0 ; i < num_trees; ++i) {
93+ output[i] = models_ptr[i]->PredictLeafIndexByMap (features);
94+ }
95+ }
9696
97- } // namespace LightGBM
97+ } // namespace LightGBM
0 commit comments