! * \brief Constructor * \param boosting Input boosting model * \param num_iteration Number of boosting round * \param is_raw_score True if need to predict result with raw score * \param predict_leaf_index True to output leaf index instead of prediction score * \param predict_contrib True to output feature contributions instead of prediction score */
| 37 | * \param predict_contrib True to output feature contributions instead of prediction score |
| 38 | */ |
| 39 | Predictor(Boosting* boosting, int num_iteration, |
| 40 | bool is_raw_score, bool predict_leaf_index, bool predict_contrib, |
| 41 | bool early_stop, int early_stop_freq, double early_stop_margin) { |
| 42 | early_stop_ = CreatePredictionEarlyStopInstance("none", LightGBM::PredictionEarlyStopConfig()); |
| 43 | if (early_stop && !boosting->NeedAccuratePrediction()) { |
| 44 | PredictionEarlyStopConfig pred_early_stop_config; |
| 45 | CHECK(early_stop_freq > 0); |
| 46 | CHECK(early_stop_margin >= 0); |
| 47 | pred_early_stop_config.margin_threshold = early_stop_margin; |
| 48 | pred_early_stop_config.round_period = early_stop_freq; |
| 49 | if (boosting->NumberOfClasses() == 1) { |
| 50 | early_stop_ = CreatePredictionEarlyStopInstance("binary", pred_early_stop_config); |
| 51 | } else { |
| 52 | early_stop_ = CreatePredictionEarlyStopInstance("multiclass", pred_early_stop_config); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | #pragma omp parallel |
| 57 | #pragma omp master |
| 58 | { |
| 59 | num_threads_ = omp_get_num_threads(); |
| 60 | } |
| 61 | boosting->InitPredict(num_iteration, predict_contrib); |
| 62 | boosting_ = boosting; |
| 63 | num_pred_one_row_ = boosting_->NumPredictOneRow(num_iteration, predict_leaf_index, predict_contrib); |
| 64 | num_feature_ = boosting_->MaxFeatureIdx() + 1; |
| 65 | predict_buf_ = std::vector<std::vector<double>>(num_threads_, std::vector<double>(num_feature_, 0.0f)); |
| 66 | const int kFeatureThreshold = 100000; |
| 67 | const size_t KSparseThreshold = static_cast<size_t>(0.01 * num_feature_); |
| 68 | if (predict_leaf_index) { |
| 69 | predict_fun_ = [=](const std::vector<std::pair<int, double>>& features, double* output) { |
| 70 | int tid = omp_get_thread_num(); |
| 71 | if (num_feature_ > kFeatureThreshold && features.size() < KSparseThreshold) { |
| 72 | auto buf = CopyToPredictMap(features); |
| 73 | boosting_->PredictLeafIndexByMap(buf, output); |
| 74 | } else { |
| 75 | CopyToPredictBuffer(predict_buf_[tid].data(), features); |
| 76 | // get result for leaf index |
| 77 | boosting_->PredictLeafIndex(predict_buf_[tid].data(), output); |
| 78 | ClearPredictBuffer(predict_buf_[tid].data(), predict_buf_[tid].size(), features); |
| 79 | } |
| 80 | }; |
| 81 | } else if (predict_contrib) { |
| 82 | predict_fun_ = [=](const std::vector<std::pair<int, double>>& features, double* output) { |
| 83 | int tid = omp_get_thread_num(); |
| 84 | CopyToPredictBuffer(predict_buf_[tid].data(), features); |
| 85 | // get result for leaf index |
| 86 | boosting_->PredictContrib(predict_buf_[tid].data(), output, &early_stop_); |
| 87 | ClearPredictBuffer(predict_buf_[tid].data(), predict_buf_[tid].size(), features); |
| 88 | }; |
| 89 | } else { |
| 90 | if (is_raw_score) { |
| 91 | predict_fun_ = [=](const std::vector<std::pair<int, double>>& features, double* output) { |
| 92 | int tid = omp_get_thread_num(); |
| 93 | if (num_feature_ > kFeatureThreshold && features.size() < KSparseThreshold) { |
| 94 | auto buf = CopyToPredictMap(features); |
| 95 | boosting_->PredictRawByMap(buf, output, &early_stop_); |
| 96 | } else { |
nothing calls this directly
no test coverage detected