predictor.hpp 8.67 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
6
7
8
#ifndef LIGHTGBM_PREDICTOR_HPP_
#define LIGHTGBM_PREDICTOR_HPP_

#include <LightGBM/meta.h>
#include <LightGBM/boosting.h>
#include <LightGBM/utils/text_reader.h>
#include <LightGBM/dataset.h>

9
#include <LightGBM/utils/openmp_wrapper.h>
Guolin Ke's avatar
Guolin Ke committed
10

ww's avatar
ww committed
11
#include <map>
Guolin Ke's avatar
Guolin Ke committed
12
13
14
15
16
17
#include <cstring>
#include <cstdio>
#include <vector>
#include <utility>
#include <functional>
#include <string>
Guolin Ke's avatar
Guolin Ke committed
18
#include <memory>
Guolin Ke's avatar
Guolin Ke committed
19
20
21
22

namespace LightGBM {

/*!
zhangyafeikimi's avatar
zhangyafeikimi committed
23
* \brief Used to predict data with input model
Guolin Ke's avatar
Guolin Ke committed
24
25
26
27
28
29
*/
class Predictor {
public:
  /*!
  * \brief Constructor
  * \param boosting Input boosting model
Guolin Ke's avatar
Guolin Ke committed
30
  * \param num_iteration Number of boosting round
31
  * \param is_raw_score True if need to predict result with raw score
32
33
  * \param is_predict_leaf_index True to output leaf index instead of prediction score
  * \param is_predict_contrib True to output feature contributions instead of prediction score
Guolin Ke's avatar
Guolin Ke committed
34
  */
Guolin Ke's avatar
Guolin Ke committed
35
  Predictor(Boosting* boosting, int num_iteration,
36
            bool is_raw_score, bool is_predict_leaf_index, bool is_predict_contrib,
37
38
39
40
41
42
43
44
45
46
47
48
49
50
            bool early_stop, int early_stop_freq, double early_stop_margin) {

    early_stop_ = CreatePredictionEarlyStopInstance("none", LightGBM::PredictionEarlyStopConfig());
    if (early_stop && !boosting->NeedAccuratePrediction()) {
      PredictionEarlyStopConfig pred_early_stop_config;
      pred_early_stop_config.margin_threshold = early_stop_margin;
      pred_early_stop_config.round_period = early_stop_freq;
      if (boosting->NumberOfClasses() == 1) {
        early_stop_ = CreatePredictionEarlyStopInstance("binary", pred_early_stop_config);
      } else {
        early_stop_ = CreatePredictionEarlyStopInstance("multiclass", pred_early_stop_config);
      }
    }

Guolin Ke's avatar
Guolin Ke committed
51
52
53
54
55
    #pragma omp parallel
    #pragma omp master
    {
      num_threads_ = omp_get_num_threads();
    }
56
    boosting->InitPredict(num_iteration);
Guolin Ke's avatar
Guolin Ke committed
57
    boosting_ = boosting;
58
    num_pred_one_row_ = boosting_->NumPredictOneRow(num_iteration, is_predict_leaf_index, is_predict_contrib);
59
60
    num_feature_ = boosting_->MaxFeatureIdx() + 1;
    predict_buf_ = std::vector<std::vector<double>>(num_threads_, std::vector<double>(num_feature_, 0.0f));
Guolin Ke's avatar
Guolin Ke committed
61

Guolin Ke's avatar
Guolin Ke committed
62
    if (is_predict_leaf_index) {
Guolin Ke's avatar
Guolin Ke committed
63
      predict_fun_ = [this](const std::vector<std::pair<int, double>>& features, double* output) {
Guolin Ke's avatar
Guolin Ke committed
64
65
        int tid = omp_get_thread_num();
        CopyToPredictBuffer(predict_buf_[tid].data(), features);
Guolin Ke's avatar
Guolin Ke committed
66
        // get result for leaf index
Guolin Ke's avatar
Guolin Ke committed
67
68
        boosting_->PredictLeafIndex(predict_buf_[tid].data(), output);
        ClearPredictBuffer(predict_buf_[tid].data(), predict_buf_[tid].size(), features);
Guolin Ke's avatar
Guolin Ke committed
69
      };
Guolin Ke's avatar
Guolin Ke committed
70

71
72
73
74
75
76
77
78
79
    } else if (is_predict_contrib) {
      predict_fun_ = [this](const std::vector<std::pair<int, double>>& features, double* output) {
        int tid = omp_get_thread_num();
        CopyToPredictBuffer(predict_buf_[tid].data(), features);
        // get result for leaf index
        boosting_->PredictContrib(predict_buf_[tid].data(), output, &early_stop_);
        ClearPredictBuffer(predict_buf_[tid].data(), predict_buf_[tid].size(), features);
      };

Guolin Ke's avatar
Guolin Ke committed
80
    } else {
Guolin Ke's avatar
Guolin Ke committed
81
      if (is_raw_score) {
82
        predict_fun_ = [this](const std::vector<std::pair<int, double>>& features, double* output) {
Guolin Ke's avatar
Guolin Ke committed
83
84
          int tid = omp_get_thread_num();
          CopyToPredictBuffer(predict_buf_[tid].data(), features);
85
          boosting_->PredictRaw(predict_buf_[tid].data(), output, &early_stop_);
Guolin Ke's avatar
Guolin Ke committed
86
          ClearPredictBuffer(predict_buf_[tid].data(), predict_buf_[tid].size(), features);
Guolin Ke's avatar
Guolin Ke committed
87
88
        };
      } else {
89
        predict_fun_ = [this](const std::vector<std::pair<int, double>>& features, double* output) {
Guolin Ke's avatar
Guolin Ke committed
90
91
          int tid = omp_get_thread_num();
          CopyToPredictBuffer(predict_buf_[tid].data(), features);
92
          boosting_->Predict(predict_buf_[tid].data(), output, &early_stop_);
Guolin Ke's avatar
Guolin Ke committed
93
          ClearPredictBuffer(predict_buf_[tid].data(), predict_buf_[tid].size(), features);
Guolin Ke's avatar
Guolin Ke committed
94
95
96
        };
      }
    }
Guolin Ke's avatar
Guolin Ke committed
97
  }
98

Guolin Ke's avatar
Guolin Ke committed
99
100
101
102
103
104
  /*!
  * \brief Destructor
  */
  ~Predictor() {
  }

zhangyafeikimi's avatar
zhangyafeikimi committed
105
  inline const PredictFunction& GetPredictFunction() const {
Guolin Ke's avatar
Guolin Ke committed
106
    return predict_fun_;
107
  }
108

Guolin Ke's avatar
Guolin Ke committed
109
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
110
  * \brief predicting on data, then saving result to disk
Guolin Ke's avatar
Guolin Ke committed
111
112
113
  * \param data_filename Filename of data
  * \param result_filename Filename of output result
  */
Guolin Ke's avatar
Guolin Ke committed
114
  void Predict(const char* data_filename, const char* result_filename, bool has_header) {
Guolin Ke's avatar
Guolin Ke committed
115
116
    FILE* result_file;

Guolin Ke's avatar
Guolin Ke committed
117
    #ifdef _MSC_VER
Guolin Ke's avatar
Guolin Ke committed
118
    fopen_s(&result_file, result_filename, "w");
Guolin Ke's avatar
Guolin Ke committed
119
    #else
Guolin Ke's avatar
Guolin Ke committed
120
    result_file = fopen(result_filename, "w");
Guolin Ke's avatar
Guolin Ke committed
121
    #endif
Guolin Ke's avatar
Guolin Ke committed
122
123

    if (result_file == NULL) {
Qiwei Ye's avatar
Qiwei Ye committed
124
      Log::Fatal("Prediction results file %s cannot be found.", result_filename);
Guolin Ke's avatar
Guolin Ke committed
125
    }
126
    auto parser = std::unique_ptr<Parser>(Parser::CreateParser(data_filename, has_header, boosting_->MaxFeatureIdx() + 1, boosting_->LabelIdx()));
Guolin Ke's avatar
Guolin Ke committed
127
128

    if (parser == nullptr) {
Qiwei Ye's avatar
Qiwei Ye committed
129
      Log::Fatal("Could not recognize the data format of data file %s.", data_filename);
Guolin Ke's avatar
Guolin Ke committed
130
131
    }

ww's avatar
ww committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
    TextReader<data_size_t> predict_data_reader(data_filename, has_header);
    std::unordered_map<int, int> feature_names_map_;
    bool need_adjust = false;
    if(has_header) {
      std::string first_line = predict_data_reader.first_line();
      std::vector<std::string> header = Common::Split(first_line.c_str(), "\t,");
      header.erase(header.begin() + boosting_->LabelIdx());
      for(int i = 0; i < static_cast<int>(header.size()); ++i) {
        for(int j = 0; j < static_cast<int>(boosting_->FeatureNames().size()); ++j) {
          if(header[i] == boosting_->FeatureNames()[j]) {
            feature_names_map_[i] = j;
            break;
          }
        }
      }
      for(auto s:feature_names_map_) {
        if(s.first != s.second) {
          need_adjust = true;
          break;
        }
      }
    }
Guolin Ke's avatar
Guolin Ke committed
154
    // function for parse data
155
156
    std::function<void(const char*, std::vector<std::pair<int, double>>*)> parser_fun;
    double tmp_label;
ww's avatar
ww committed
157
    parser_fun = [this, &parser, &tmp_label, &need_adjust, &feature_names_map_]
158
    (const char* buffer, std::vector<std::pair<int, double>>* feature) {
Guolin Ke's avatar
Guolin Ke committed
159
      parser->ParseOneLine(buffer, feature, &tmp_label);
ww's avatar
ww committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
      if(need_adjust) {
        int i = 0, j = static_cast<int>(feature->size());
        while(i < j) {
          if(feature_names_map_.find((*feature)[i].first) != feature_names_map_.end()) {
            (*feature)[i].first = feature_names_map_[(*feature)[i].first];
            ++i;
          }
          else {
            //move the non-used features to the end of the feature vector
            std::swap((*feature)[i], (*feature)[--j]);
          }
        }
        feature->resize(i);
      }
Guolin Ke's avatar
Guolin Ke committed
174
175
    };

Guolin Ke's avatar
Guolin Ke committed
176
    std::function<void(data_size_t, const std::vector<std::string>&)> process_fun =
Guolin Ke's avatar
Guolin Ke committed
177
      [this, &parser_fun, &result_file]
Guolin Ke's avatar
Guolin Ke committed
178
    (data_size_t, const std::vector<std::string>& lines) {
179
      std::vector<std::pair<int, double>> oneline_features;
180
181
182
      std::vector<std::string> result_to_write(lines.size());
      OMP_INIT_EX();
      #pragma omp parallel for schedule(static) firstprivate(oneline_features)
183
      for (data_size_t i = 0; i < static_cast<data_size_t>(lines.size()); ++i) {
184
        OMP_LOOP_EX_BEGIN();
Guolin Ke's avatar
Guolin Ke committed
185
186
187
188
        oneline_features.clear();
        // parser
        parser_fun(lines[i].c_str(), &oneline_features);
        // predict
Guolin Ke's avatar
Guolin Ke committed
189
190
191
        std::vector<double> result(num_pred_one_row_);
        predict_fun_(oneline_features, result.data());
        auto str_result = Common::Join<double>(result, "\t");
192
193
194
195
196
197
        result_to_write[i] = str_result;
        OMP_LOOP_EX_END();
      }
      OMP_THROW_EX();
      for (data_size_t i = 0; i < static_cast<data_size_t>(result_to_write.size()); ++i) {
        fprintf(result_file, "%s\n", result_to_write[i].c_str());
Guolin Ke's avatar
Guolin Ke committed
198
199
200
201
202
203
204
      }
    };
    predict_data_reader.ReadAllAndProcessParallel(process_fun);
    fclose(result_file);
  }

private:
205

Guolin Ke's avatar
Guolin Ke committed
206
  void CopyToPredictBuffer(double* pred_buf, const std::vector<std::pair<int, double>>& features) {
Guolin Ke's avatar
Guolin Ke committed
207
208
    int loop_size = static_cast<int>(features.size());
    for (int i = 0; i < loop_size; ++i) {
209
210
211
      if (features[i].first < num_feature_) {
        pred_buf[features[i].first] = features[i].second;
      }
212
213
214
    }
  }

Guolin Ke's avatar
Guolin Ke committed
215
216
217
  void ClearPredictBuffer(double* pred_buf, size_t buf_size, const std::vector<std::pair<int, double>>& features) {
    if (features.size() < static_cast<size_t>(buf_size / 2)) {
      std::memset(pred_buf, 0, sizeof(double)*(buf_size));
218
219
220
    } else {
      int loop_size = static_cast<int>(features.size());
      for (int i = 0; i < loop_size; ++i) {
Guolin Ke's avatar
Guolin Ke committed
221
222
223
        if (features[i].first < num_feature_) {
          pred_buf[features[i].first] = 0.0f;
        }
Guolin Ke's avatar
Guolin Ke committed
224
225
226
      }
    }
  }
227

Guolin Ke's avatar
Guolin Ke committed
228
229
  /*! \brief Boosting model */
  const Boosting* boosting_;
Guolin Ke's avatar
Guolin Ke committed
230
231
  /*! \brief function for prediction */
  PredictFunction predict_fun_;
232
  PredictionEarlyStopInstance early_stop_;
233
  int num_feature_;
Guolin Ke's avatar
Guolin Ke committed
234
  int num_pred_one_row_;
Guolin Ke's avatar
Guolin Ke committed
235
236
  int num_threads_;
  std::vector<std::vector<double>> predict_buf_;
Guolin Ke's avatar
Guolin Ke committed
237
238
239
240
};

}  // namespace LightGBM

Guolin Ke's avatar
Guolin Ke committed
241
#endif   // LightGBM_PREDICTOR_HPP_