gbdt.h 13.2 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
#ifndef LIGHTGBM_BOOSTING_GBDT_H_
#define LIGHTGBM_BOOSTING_GBDT_H_

#include <LightGBM/boosting.h>
5
#include <LightGBM/objective_function.h>
Guolin Ke's avatar
Guolin Ke committed
6
#include <LightGBM/prediction_early_stop.h>
7

Guolin Ke's avatar
Guolin Ke committed
8
9
10
11
12
#include "score_updater.hpp"

#include <cstdio>
#include <vector>
#include <string>
13
#include <fstream>
Guolin Ke's avatar
Guolin Ke committed
14
#include <memory>
15
#include <mutex>
Guolin Ke's avatar
Guolin Ke committed
16
17
18
19
20
21
22
23
24
25

namespace LightGBM {
/*!
* \brief GBDT algorithm implementation. including Training, prediction, bagging.
*/
class GBDT: public Boosting {
public:
  /*!
  * \brief Constructor
  */
26
  GBDT();
Guolin Ke's avatar
Guolin Ke committed
27
28
29
30
31
  /*!
  * \brief Destructor
  */
  ~GBDT();
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
32
  * \brief Initialization logic
zhangyafeikimi's avatar
zhangyafeikimi committed
33
  * \param gbdt_config Config for boosting
Guolin Ke's avatar
Guolin Ke committed
34
  * \param train_data Training data
35
  * \param objective_function Training objective function
Guolin Ke's avatar
Guolin Ke committed
36
37
  * \param training_metrics Training metrics
  */
38
39
40
  void Init(const BoostingConfig* gbdt_config, const Dataset* train_data, const ObjectiveFunction* objective_function,
            const std::vector<const Metric*>& training_metrics)
    override;
wxchan's avatar
wxchan committed
41
42
43

  /*!
  * \brief Merge model from other boosting object
44
  Will insert to the front of current boosting object
wxchan's avatar
wxchan committed
45
46
47
48
49
50
51
52
53
54
55
56
  * \param other
  */
  void MergeFrom(const Boosting* other) override {
    auto other_gbdt = reinterpret_cast<const GBDT*>(other);
    // tmp move to other vector
    auto original_models = std::move(models_);
    models_ = std::vector<std::unique_ptr<Tree>>();
    // push model from other first
    for (const auto& tree : other_gbdt->models_) {
      auto new_tree = std::unique_ptr<Tree>(new Tree(*(tree.get())));
      models_.push_back(std::move(new_tree));
    }
Guolin Ke's avatar
Guolin Ke committed
57
    num_init_iteration_ = static_cast<int>(models_.size()) / num_tree_per_iteration_;
wxchan's avatar
wxchan committed
58
59
60
61
62
    // push model in current object
    for (const auto& tree : original_models) {
      auto new_tree = std::unique_ptr<Tree>(new Tree(*(tree.get())));
      models_.push_back(std::move(new_tree));
    }
Guolin Ke's avatar
Guolin Ke committed
63
    num_iteration_for_pred_ = static_cast<int>(models_.size()) / num_tree_per_iteration_;
wxchan's avatar
wxchan committed
64
65
66
67
68
  }

  /*!
  * \brief Reset training data for current boosting
  * \param train_data Training data
69
  * \param objective_function Training objective function
wxchan's avatar
wxchan committed
70
71
  * \param training_metrics Training metric
  */
72
  void ResetTrainingData(const BoostingConfig* config, const Dataset* train_data, const ObjectiveFunction* objective_function, const std::vector<const Metric*>& training_metrics) override;
wxchan's avatar
wxchan committed
73

Guolin Ke's avatar
Guolin Ke committed
74
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
75
76
77
  * \brief Adding a validation dataset
  * \param valid_data Validation dataset
  * \param valid_metrics Metrics for validation dataset
Guolin Ke's avatar
Guolin Ke committed
78
  */
wxchan's avatar
wxchan committed
79
  void AddValidDataset(const Dataset* valid_data,
80
                       const std::vector<const Metric*>& valid_metrics) override;
Guolin Ke's avatar
Guolin Ke committed
81
  /*!
Guolin Ke's avatar
Guolin Ke committed
82
83
84
  * \brief Training logic
  * \param gradient nullptr for using default objective, otherwise use self-defined boosting
  * \param hessian nullptr for using default objective, otherwise use self-defined boosting
Guolin Ke's avatar
Guolin Ke committed
85
  * \param is_eval true if need evaluation or early stop
Guolin Ke's avatar
Guolin Ke committed
86
  * \return True if meet early stopping or cannot boosting
Guolin Ke's avatar
Guolin Ke committed
87
  */
88
  virtual bool TrainOneIter(const score_t* gradient, const score_t* hessian, bool is_eval) override;
89

wxchan's avatar
wxchan committed
90
91
92
93
94
  /*!
  * \brief Rollback one iteration
  */
  void RollbackOneIter() override;

Guolin Ke's avatar
Guolin Ke committed
95
  int GetCurrentIteration() const override { return static_cast<int>(models_.size()) / num_tree_per_iteration_; }
wxchan's avatar
wxchan committed
96

Guolin Ke's avatar
Guolin Ke committed
97
98
  bool EvalAndCheckEarlyStopping() override;

99
100
101
102
103
104
105
106
  bool NeedAccuratePrediction() const override { 
    if (objective_function_ == nullptr) {
      return true;
    } else {
      return objective_function_->NeedAccuratePrediction();
    }
  }

Guolin Ke's avatar
Guolin Ke committed
107
108
109
110
111
  /*!
  * \brief Get evaluation result at data_idx data
  * \param data_idx 0: training data, 1: 1st validation data
  * \return evaluation result
  */
112
  std::vector<double> GetEvalAt(int data_idx) const override;
113

Guolin Ke's avatar
Guolin Ke committed
114
115
  /*!
  * \brief Get current training score
Guolin Ke's avatar
Guolin Ke committed
116
  * \param out_len length of returned score
Guolin Ke's avatar
Guolin Ke committed
117
118
  * \return training score
  */
119
  virtual const double* GetTrainingScore(int64_t* out_len) override;
120

Guolin Ke's avatar
Guolin Ke committed
121
122
123
124
125
126
127
128
  virtual int64_t GetNumPredictAt(int data_idx) const override {
    CHECK(data_idx >= 0 && data_idx <= static_cast<int>(valid_score_updater_.size()));
    data_size_t num_data = train_data_->num_data();
    if (data_idx > 0) {
      num_data = valid_score_updater_[data_idx - 1]->num_data();
    }
    return num_data * num_class_;
  }
Guolin Ke's avatar
Guolin Ke committed
129
130
131
132
  /*!
  * \brief Get prediction result at data_idx data
  * \param data_idx 0: training data, 1: 1st validation data
  * \param result used to store prediction result, should allocate memory before call this function
133
  * \param out_len length of returned score
Guolin Ke's avatar
Guolin Ke committed
134
  */
Guolin Ke's avatar
Guolin Ke committed
135
  void GetPredictAt(int data_idx, double* out_result, int64_t* out_len) override;
Guolin Ke's avatar
Guolin Ke committed
136

Guolin Ke's avatar
Guolin Ke committed
137
138
139
140
141
142
143
144
145
146
147
148
  inline int NumPredictOneRow(int num_iteration, int is_pred_leaf) const override {
    int num_preb_in_one_row = num_class_;
    if (is_pred_leaf) {
      int max_iteration = GetCurrentIteration();
      if (num_iteration > 0) {
        num_preb_in_one_row *= static_cast<int>(std::min(max_iteration, num_iteration));
      } else {
        num_preb_in_one_row *= max_iteration;
      }
    }
    return num_preb_in_one_row;
  }
Guolin Ke's avatar
Guolin Ke committed
149

cbecker's avatar
cbecker committed
150
  void PredictRaw(const double* features, double* output,
151
                  const PredictionEarlyStopInstance* earlyStop) const override;
wxchan's avatar
wxchan committed
152

cbecker's avatar
cbecker committed
153
154
  void Predict(const double* features, double* output,
               const PredictionEarlyStopInstance* earlyStop) const override;
Guolin Ke's avatar
Guolin Ke committed
155

156
  void PredictLeafIndex(const double* features, double* output) const override;
wxchan's avatar
wxchan committed
157

Guolin Ke's avatar
Guolin Ke committed
158
  /*!
wxchan's avatar
wxchan committed
159
  * \brief Dump model to json format string
160
  * \param num_iteration Number of iterations that want to dump, -1 means dump all
wxchan's avatar
wxchan committed
161
  * \return Json format string of model
Guolin Ke's avatar
Guolin Ke committed
162
  */
163
  std::string DumpModel(int num_iteration) const override;
wxchan's avatar
wxchan committed
164

165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
  /*!
  * \brief Translate model to if-else statement
  * \param num_iteration Number of iterations that want to translate, -1 means translate all
  * \return if-else format codes of model
  */
  std::string ModelToIfElse(int num_iteration) const override;

  /*!
  * \brief Translate model to if-else statement
  * \param num_iteration Number of iterations that want to translate, -1 means translate all
  * \param filename Filename that want to save to
  * \return is_finish Is training finished or not
  */
  bool SaveModelToIfElse(int num_iteration, const char* filename) const override;

wxchan's avatar
wxchan committed
180
181
182
183
  /*!
  * \brief Save model to file
  * \param num_used_model Number of model that want to save, -1 means save all
  * \param filename Filename that want to save to
184
  * \return is_finish Is training finished or not
wxchan's avatar
wxchan committed
185
  */
186
  virtual bool SaveModelToFile(int num_iterations, const char* filename) const override;
wxchan's avatar
wxchan committed
187

188
189
190
191
192
  /*!
  * \brief Save model to string
  * \param num_used_model Number of model that want to save, -1 means save all
  * \return Non-empty string if succeeded
  */
193
  virtual std::string SaveModelToString(int num_iterations) const override;
194

Guolin Ke's avatar
Guolin Ke committed
195
196
197
  /*!
  * \brief Restore from a serialized string
  */
198
  bool LoadModelFromString(const std::string& model_str) override;
wxchan's avatar
wxchan committed
199

Guolin Ke's avatar
Guolin Ke committed
200
201
202
203
204
  /*!
  * \brief Get max feature index of this model
  * \return Max feature index of this model
  */
  inline int MaxFeatureIdx() const override { return max_feature_idx_; }
Guolin Ke's avatar
Guolin Ke committed
205

wxchan's avatar
wxchan committed
206
207
208
209
210
211
  /*!
  * \brief Get feature names of this model
  * \return Feature names of this model
  */
  inline std::vector<std::string> FeatureNames() const override { return feature_names_; }

Guolin Ke's avatar
Guolin Ke committed
212
213
214
215
216
217
  /*!
  * \brief Get index of label column
  * \return index of label column
  */
  inline int LabelIdx() const override { return label_idx_; }

Guolin Ke's avatar
Guolin Ke committed
218
219
220
221
  /*!
  * \brief Get number of weak sub-models
  * \return Number of weak sub-models
  */
wxchan's avatar
wxchan committed
222
  inline int NumberOfTotalModel() const override { return static_cast<int>(models_.size()); }
Guolin Ke's avatar
Guolin Ke committed
223

Guolin Ke's avatar
Guolin Ke committed
224
225
226
227
228
229
  /*!
  * \brief Get number of tree per iteration
  * \return number of tree per iteration
  */
  inline int NumTreePerIteration() const override { return num_tree_per_iteration_; }

230
231
232
233
  /*!
  * \brief Get number of classes
  * \return Number of classes
  */
Guolin Ke's avatar
Guolin Ke committed
234
  inline int NumberOfClasses() const override { return num_class_; }
235

236
  inline void InitPredict(int num_iteration) override {
Guolin Ke's avatar
Guolin Ke committed
237
    num_iteration_for_pred_ = static_cast<int>(models_.size()) / num_tree_per_iteration_;
wxchan's avatar
wxchan committed
238
    if (num_iteration > 0) {
239
      num_iteration_for_pred_ = std::min(num_iteration + (boost_from_average_ ? 1 : 0), num_iteration_for_pred_);
240
241
    }
  }
wxchan's avatar
wxchan committed
242

Guolin Ke's avatar
Guolin Ke committed
243
244
245
246
247
248
249
250
251
252
253
254
  inline double GetLeafValue(int tree_idx, int leaf_idx) const {
    CHECK(tree_idx >= 0 && static_cast<size_t>(tree_idx) < models_.size());
    CHECK(leaf_idx >= 0 && leaf_idx < models_[tree_idx]->num_leaves());
    return models_[tree_idx]->LeafOutput(leaf_idx);
  }

  inline void SetLeafValue(int tree_idx, int leaf_idx, double val) {
    CHECK(tree_idx >= 0 && static_cast<size_t>(tree_idx) < models_.size());
    CHECK(leaf_idx >= 0 && leaf_idx < models_[tree_idx]->num_leaves());
    models_[tree_idx]->SetLeafOutput(leaf_idx, val);
  }

255
256
257
  /*!
  * \brief Get Type name of this boosting object
  */
Guolin Ke's avatar
Guolin Ke committed
258
  virtual const char* SubModelName() const override { return "tree"; }
259

260
protected:
Guolin Ke's avatar
Guolin Ke committed
261
262
263
264
  /*!
  * \brief Implement bagging logic
  * \param iter Current interation
  */
265
266
267
268
269
270
271
272
273
  virtual void Bagging(int iter);

  /*!
  * \brief Helper function for bagging, used for multi-threading optimization
  * \param start start indice of bagging
  * \param cnt count
  * \param buffer output buffer
  * \return count of left size
  */
Guolin Ke's avatar
Guolin Ke committed
274
  data_size_t BaggingHelper(Random& cur_rand, data_size_t start, data_size_t cnt, data_size_t* buffer);
Guolin Ke's avatar
Guolin Ke committed
275
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
276
277
  * \brief updating score for out-of-bag data.
  *        Data should be update since we may re-bagging data on training
Guolin Ke's avatar
Guolin Ke committed
278
  * \param tree Trained tree of this iteration
279
  * \param cur_tree_id Current tree for multiclass training
Guolin Ke's avatar
Guolin Ke committed
280
  */
281
  void UpdateScoreOutOfBag(const Tree* tree, const int cur_tree_id);
Guolin Ke's avatar
Guolin Ke committed
282
283
284
285
286
  /*!
  * \brief calculate the object function
  */
  void Boosting();
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
287
  * \brief updating score after tree was trained
Guolin Ke's avatar
Guolin Ke committed
288
  * \param tree Trained tree of this iteration
289
  * \param cur_tree_id Current tree for multiclass training
Guolin Ke's avatar
Guolin Ke committed
290
  */
291
  virtual void UpdateScore(const Tree* tree, const int cur_tree_id);
Guolin Ke's avatar
Guolin Ke committed
292
  /*!
Hui Xue's avatar
Hui Xue committed
293
  * \brief Print metric result of current iteration
Guolin Ke's avatar
Guolin Ke committed
294
  * \param iter Current interation
Guolin Ke's avatar
Guolin Ke committed
295
  * \return best_msg if met early_stopping
Guolin Ke's avatar
Guolin Ke committed
296
  */
Guolin Ke's avatar
Guolin Ke committed
297
  std::string OutputMetric(int iter);
wxchan's avatar
wxchan committed
298
299
300
  /*!
  * \brief Calculate feature importances
  */
wxchan's avatar
wxchan committed
301
  std::vector<std::pair<size_t, std::string>> FeatureImportance() const;
302

303
304
  /*! \brief current iteration */
  int iter_;
Guolin Ke's avatar
Guolin Ke committed
305
306
307
  /*! \brief Pointer to training data */
  const Dataset* train_data_;
  /*! \brief Config of gbdt */
Guolin Ke's avatar
Guolin Ke committed
308
  std::unique_ptr<BoostingConfig> gbdt_config_;
Hui Xue's avatar
Hui Xue committed
309
  /*! \brief Tree learner, will use this class to learn trees */
310
  std::unique_ptr<TreeLearner> tree_learner_;
Guolin Ke's avatar
Guolin Ke committed
311
  /*! \brief Objective function */
312
  const ObjectiveFunction* objective_function_;
Hui Xue's avatar
Hui Xue committed
313
  /*! \brief Store and update training data's score */
Guolin Ke's avatar
Guolin Ke committed
314
  std::unique_ptr<ScoreUpdater> train_score_updater_;
Guolin Ke's avatar
Guolin Ke committed
315
316
317
  /*! \brief Metrics for training data */
  std::vector<const Metric*> training_metrics_;
  /*! \brief Store and update validation data's scores */
Guolin Ke's avatar
Guolin Ke committed
318
  std::vector<std::unique_ptr<ScoreUpdater>> valid_score_updater_;
Guolin Ke's avatar
Guolin Ke committed
319
320
  /*! \brief Metric for validation data */
  std::vector<std::vector<const Metric*>> valid_metrics_;
wxchan's avatar
wxchan committed
321
322
  /*! \brief Number of rounds for early stopping */
  int early_stopping_round_;
Guolin Ke's avatar
Guolin Ke committed
323
  /*! \brief Best iteration(s) for early stopping */
wxchan's avatar
wxchan committed
324
  std::vector<std::vector<int>> best_iter_;
Guolin Ke's avatar
Guolin Ke committed
325
  /*! \brief Best score(s) for early stopping */
326
  std::vector<std::vector<double>> best_score_;
Guolin Ke's avatar
Guolin Ke committed
327
328
  /*! \brief output message of best iteration */
  std::vector<std::vector<std::string>> best_msg_;
Guolin Ke's avatar
Guolin Ke committed
329
  /*! \brief Trained models(trees) */
Guolin Ke's avatar
Guolin Ke committed
330
  std::vector<std::unique_ptr<Tree>> models_;
Guolin Ke's avatar
Guolin Ke committed
331
332
333
  /*! \brief Max feature index of training data*/
  int max_feature_idx_;
  /*! \brief First order derivative of training data */
334
  std::vector<score_t> gradients_;
Guolin Ke's avatar
Guolin Ke committed
335
  /*! \brief Secend order derivative of training data */
336
  std::vector<score_t> hessians_;
Guolin Ke's avatar
Guolin Ke committed
337
  /*! \brief Store the indices of in-bag data */
Guolin Ke's avatar
Guolin Ke committed
338
  std::vector<data_size_t> bag_data_indices_;
Guolin Ke's avatar
Guolin Ke committed
339
340
  /*! \brief Number of in-bag data */
  data_size_t bag_data_cnt_;
341
342
  /*! \brief Store the indices of in-bag data */
  std::vector<data_size_t> tmp_indices_;
wxchan's avatar
wxchan committed
343
  /*! \brief Number of training data */
Guolin Ke's avatar
Guolin Ke committed
344
  data_size_t num_data_;
345
346
347
  /*! \brief Number of trees per iterations */
  int num_tree_per_iteration_;
  /*! \brief Number of class */
348
  int num_class_;
Guolin Ke's avatar
Guolin Ke committed
349
350
  /*! \brief Index of label column */
  data_size_t label_idx_;
351
  /*! \brief number of used model */
wxchan's avatar
wxchan committed
352
  int num_iteration_for_pred_;
Guolin Ke's avatar
Guolin Ke committed
353
354
  /*! \brief Shrinkage rate for one iteration */
  double shrinkage_rate_;
wxchan's avatar
wxchan committed
355
356
  /*! \brief Number of loaded initial models */
  int num_init_iteration_;
Guolin Ke's avatar
Guolin Ke committed
357
358
  /*! \brief Feature names */
  std::vector<std::string> feature_names_;
Guolin Ke's avatar
Guolin Ke committed
359
  std::vector<std::string> feature_infos_;
360
361
362
363
364
365
366
367
368
369
370
371
  /*! \brief number of threads */
  int num_threads_;
  /*! \brief Buffer for multi-threading bagging */
  std::vector<data_size_t> offsets_buf_;
  /*! \brief Buffer for multi-threading bagging */
  std::vector<data_size_t> left_cnts_buf_;
  /*! \brief Buffer for multi-threading bagging */
  std::vector<data_size_t> right_cnts_buf_;
  /*! \brief Buffer for multi-threading bagging */
  std::vector<data_size_t> left_write_pos_buf_;
  /*! \brief Buffer for multi-threading bagging */
  std::vector<data_size_t> right_write_pos_buf_;
Guolin Ke's avatar
Guolin Ke committed
372
373
  std::unique_ptr<Dataset> tmp_subset_;
  bool is_use_subset_;
374
  bool boost_from_average_;
375
376
  std::vector<bool> class_need_train_;
  std::vector<double> class_default_output_;
377
  bool is_constant_hessian_;
378
  std::unique_ptr<ObjectiveFunction> loaded_objective_;
Guolin Ke's avatar
Guolin Ke committed
379
380
381
};

}  // namespace LightGBM
Guolin Ke's avatar
Guolin Ke committed
382
#endif   // LightGBM_BOOSTING_GBDT_H_