"vscode:/vscode.git/clone" did not exist on "8a6bd5ec4e32a1357e722d813995a2a3086ea485"
gbdt_prediction.cpp 1.56 KB
Newer Older
1
2
3
4
#include "gbdt.h"

#include <LightGBM/utils/openmp_wrapper.h>
#include <LightGBM/objective_function.h>
cbecker's avatar
cbecker committed
5
#include <LightGBM/prediction_early_stop.h>
6
7
8

namespace LightGBM {

9
10
void GBDT::PredictRaw(const double* features, double* output, const PredictionEarlyStopInstance* early_stop) const {
  int early_stop_round_counter = 0;
11
12
  // set zero
  std::memset(output, 0, sizeof(double) * num_tree_per_iteration_);
cbecker's avatar
cbecker committed
13
14
  for (int i = 0; i < num_iteration_for_pred_; ++i) {
    // predict all the trees for one iteration
15
    for (int k = 0; k < num_tree_per_iteration_; ++k) {
cbecker's avatar
cbecker committed
16
      output[k] += models_[i * num_tree_per_iteration_ + k]->Predict(features);
17
    }
cbecker's avatar
cbecker committed
18
    // check early stopping
19
20
21
    ++early_stop_round_counter;
    if (early_stop->round_period == early_stop_round_counter) {
      if (early_stop->callback_function(output, num_tree_per_iteration_)) {
cbecker's avatar
cbecker committed
22
        return;
23
      }
24
      early_stop_round_counter = 0;
25
26
    }
  }
cbecker's avatar
cbecker committed
27
28
}

29
30
void GBDT::Predict(const double* features, double* output, const PredictionEarlyStopInstance* early_stop) const {
  PredictRaw(features, output, early_stop);
Guolin Ke's avatar
Guolin Ke committed
31
32
33
34
35
  if (average_output_) {
    for (int k = 0; k < num_tree_per_iteration_; ++k) {
      output[k] /= num_iteration_for_pred_;
    }
  } else if (objective_function_ != nullptr) {
36
37
38
39
40
41
42
43
44
45
46
    objective_function_->ConvertOutput(output, output);
  }
}

void GBDT::PredictLeafIndex(const double* features, double* output) const {
  int total_tree = num_iteration_for_pred_ * num_tree_per_iteration_;
  for (int i = 0; i < total_tree; ++i) {
    output[i] = models_[i]->PredictLeafIndex(features);
  }
}

cbecker's avatar
cbecker committed
47
}  // namespace LightGBM