gbdt_prediction.cpp 3.69 KB
Newer Older
1
2
3
4
/*!
 * Copyright (c) 2017 Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See LICENSE file in the project root for license information.
 */
5
#include <LightGBM/objective_function.h>
cbecker's avatar
cbecker committed
6
#include <LightGBM/prediction_early_stop.h>
7
8
#include <LightGBM/utils/openmp_wrapper.h>

9
10
#include <unordered_map>

11
12
#include "gbdt.h"

13
14
namespace LightGBM {

15
16
void GBDT::PredictRaw(const double* features, double* output, const PredictionEarlyStopInstance* early_stop) const {
  int early_stop_round_counter = 0;
17
18
  // set zero
  std::memset(output, 0, sizeof(double) * num_tree_per_iteration_);
19
20
  const int end_iteration_for_pred = start_iteration_for_pred_ + num_iteration_for_pred_;
  for (int i = start_iteration_for_pred_; i < end_iteration_for_pred; ++i) {
cbecker's avatar
cbecker committed
21
    // predict all the trees for one iteration
22
    for (int k = 0; k < num_tree_per_iteration_; ++k) {
cbecker's avatar
cbecker committed
23
      output[k] += models_[i * num_tree_per_iteration_ + k]->Predict(features);
24
    }
cbecker's avatar
cbecker committed
25
    // check early stopping
26
27
28
    ++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
29
        return;
30
      }
31
      early_stop_round_counter = 0;
32
33
    }
  }
cbecker's avatar
cbecker committed
34
35
}

36
37
38
39
void GBDT::PredictRawByMap(const std::unordered_map<int, double>& features, double* output, const PredictionEarlyStopInstance* early_stop) const {
  int early_stop_round_counter = 0;
  // set zero
  std::memset(output, 0, sizeof(double) * num_tree_per_iteration_);
40
41
  const int end_iteration_for_pred = start_iteration_for_pred_ + num_iteration_for_pred_;
  for (int i = start_iteration_for_pred_; i < end_iteration_for_pred; ++i) {
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
    // predict all the trees for one iteration
    for (int k = 0; k < num_tree_per_iteration_; ++k) {
      output[k] += models_[i * num_tree_per_iteration_ + k]->PredictByMap(features);
    }
    // check early stopping
    ++early_stop_round_counter;
    if (early_stop->round_period == early_stop_round_counter) {
      if (early_stop->callback_function(output, num_tree_per_iteration_)) {
        return;
      }
      early_stop_round_counter = 0;
    }
  }
}

57
58
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
59
60
61
62
  if (average_output_) {
    for (int k = 0; k < num_tree_per_iteration_; ++k) {
      output[k] /= num_iteration_for_pred_;
    }
Guolin Ke's avatar
Guolin Ke committed
63
64
  }
  if (objective_function_ != nullptr) {
65
66
67
68
    objective_function_->ConvertOutput(output, output);
  }
}

69
70
71
72
73
74
void GBDT::PredictByMap(const std::unordered_map<int, double>& features, double* output, const PredictionEarlyStopInstance* early_stop) const {
  PredictRawByMap(features, output, early_stop);
  if (average_output_) {
    for (int k = 0; k < num_tree_per_iteration_; ++k) {
      output[k] /= num_iteration_for_pred_;
    }
Guolin Ke's avatar
Guolin Ke committed
75
76
  }
  if (objective_function_ != nullptr) {
77
78
79
80
    objective_function_->ConvertOutput(output, output);
  }
}

81
void GBDT::PredictLeafIndex(const double* features, double* output) const {
82
  int start_tree = start_iteration_for_pred_ * num_tree_per_iteration_;
83
  int num_trees = num_iteration_for_pred_ * num_tree_per_iteration_;
84
85
86
  const auto* models_ptr = models_.data() + start_tree;
  for (int i = 0; i < num_trees; ++i) {
    output[i] = models_ptr[i]->PredictLeafIndex(features);
87
88
89
  }
}

90
void GBDT::PredictLeafIndexByMap(const std::unordered_map<int, double>& features, double* output) const {
91
92
93
94
95
  int start_tree = start_iteration_for_pred_ * num_tree_per_iteration_;
  int num_trees = num_iteration_for_pred_ * num_tree_per_iteration_;
  const auto* models_ptr = models_.data() + start_tree;
  for (int i = 0; i < num_trees; ++i) {
    output[i] = models_ptr[i]->PredictLeafIndexByMap(features);
96
97
98
  }
}

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