gbdt_prediction.cpp 1.52 KB
Newer Older
1
2
3
4
5
6
7
8
#include "gbdt.h"

#include <LightGBM/utils/openmp_wrapper.h>

#include <LightGBM/utils/common.h>

#include <LightGBM/objective_function.h>
#include <LightGBM/metric.h>
cbecker's avatar
cbecker committed
9
#include <LightGBM/prediction_early_stop.h>
10
11
12
13
14
15
16
17
18
19
20

#include <ctime>

#include <sstream>
#include <chrono>
#include <string>
#include <vector>
#include <utility>

namespace LightGBM {

21
22
void GBDT::PredictRaw(const double* features, double* output, const PredictionEarlyStopInstance* early_stop) const {
  int early_stop_round_counter = 0;
cbecker's avatar
cbecker committed
23
24
  for (int i = 0; i < num_iteration_for_pred_; ++i) {
    // predict all the trees for one iteration
25
    for (int k = 0; k < num_tree_per_iteration_; ++k) {
cbecker's avatar
cbecker committed
26
      output[k] += models_[i * num_tree_per_iteration_ + k]->Predict(features);
27
    }
cbecker's avatar
cbecker committed
28
29

    // check early stopping
30
31
32
    ++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
33
        return;
34
      }
35
      early_stop_round_counter = 0;
36
37
    }
  }
cbecker's avatar
cbecker committed
38
39
}

40
41
void GBDT::Predict(const double* features, double* output, const PredictionEarlyStopInstance* early_stop) const {
  PredictRaw(features, output, early_stop);
cbecker's avatar
cbecker committed
42

43
44
45
46
47
48
49
50
51
52
53
54
  if (objective_function_ != nullptr) {
    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
55
}  // namespace LightGBM