gbdt.h 7.08 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
6
7
8
9
#ifndef LIGHTGBM_BOOSTING_GBDT_H_
#define LIGHTGBM_BOOSTING_GBDT_H_

#include <LightGBM/boosting.h>
#include "score_updater.hpp"

#include <cstdio>
#include <vector>
#include <string>
10
#include <fstream>
Guolin Ke's avatar
Guolin Ke committed
11
12
13
14
15
16
17
18
19
20

namespace LightGBM {
/*!
* \brief GBDT algorithm implementation. including Training, prediction, bagging.
*/
class GBDT: public Boosting {
public:
  /*!
  * \brief Constructor
  */
21
  GBDT();
Guolin Ke's avatar
Guolin Ke committed
22
23
24
25
26
  /*!
  * \brief Destructor
  */
  ~GBDT();
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
27
  * \brief Initialization logic
Guolin Ke's avatar
Guolin Ke committed
28
29
30
31
32
33
  * \param config Config for boosting
  * \param train_data Training data
  * \param object_function Training objective function
  * \param training_metrics Training metrics
  * \param output_model_filename Filename of output model
  */
34
35
  void Init(const BoostingConfig* gbdt_config, const Dataset* train_data, const ObjectiveFunction* object_function,
                             const std::vector<const Metric*>& training_metrics)
Guolin Ke's avatar
Guolin Ke committed
36
37
                                                                       override;
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
38
39
40
  * \brief Adding a validation dataset
  * \param valid_data Validation dataset
  * \param valid_metrics Metrics for validation dataset
Guolin Ke's avatar
Guolin Ke committed
41
42
43
44
45
46
  */
  void AddDataset(const Dataset* valid_data,
       const std::vector<const Metric*>& valid_metrics) override;
  /*!
  * \brief one training iteration
  */
47
48
49
50
51
52
53
54
  bool TrainOneIter(const score_t* gradient, const score_t* hessian, bool is_eval) override;

  /*! \brief Get eval result */
  std::vector<std::string> EvalCurrent(bool is_eval_train) const override;

  /*! \brief Get prediction result */
  const std::vector<const score_t*> PredictCurrent(bool is_predict_train) const override;

Guolin Ke's avatar
Guolin Ke committed
55
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
56
  * \brief Predtion for one record without sigmoid transformation
Guolin Ke's avatar
Guolin Ke committed
57
58
59
  * \param feature_values Feature value on this record
  * \return Prediction result for this record
  */
60
  double PredictRaw(const double* feature_values) const override;
Guolin Ke's avatar
Guolin Ke committed
61
62

  /*!
Qiwei Ye's avatar
Qiwei Ye committed
63
  * \brief Predtion for one record with sigmoid transformation if enabled
Guolin Ke's avatar
Guolin Ke committed
64
65
66
  * \param feature_values Feature value on this record
  * \return Prediction result for this record
  */
67
  double Predict(const double* feature_values) const override;
wxchan's avatar
wxchan committed
68
  
69
70
71
72
73
  /*!
  * \brief Predtion for multiclass classification
  * \param feature_values Feature value on this record
  * \return Prediction result, num_class numbers per line
  */
74
  std::vector<double> PredictMulticlass(const double* value) const override;
75
  
wxchan's avatar
wxchan committed
76
77
78
79
80
  /*!
  * \brief Predtion for one record with leaf index
  * \param feature_values Feature value on this record
  * \return Predicted leaf index for this record
  */
81
  std::vector<int> PredictLeafIndex(const double* value) const override;
wxchan's avatar
wxchan committed
82
  
Guolin Ke's avatar
Guolin Ke committed
83
84
85
86
  /*!
  * \brief Serialize models by string
  * \return String output of tranined model
  */
87
  void SaveModelToFile(bool is_finish, const char* filename) override;
Guolin Ke's avatar
Guolin Ke committed
88
89
90
  /*!
  * \brief Restore from a serialized string
  */
91
  void ModelsFromString(const std::string& model_str) override;
Guolin Ke's avatar
Guolin Ke committed
92
93
94
95
96
  /*!
  * \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
97
98
99
100
101
102
103

  /*!
  * \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
104
105
106
107
108
109
  /*!
  * \brief Get number of weak sub-models
  * \return Number of weak sub-models
  */
  inline int NumberOfSubModels() const override { return static_cast<int>(models_.size()); }

110
111
112
113
114
  /*!
  * \brief Get number of classes
  * \return Number of classes
  */
  inline int NumberOfClass() const override { return num_class_; }
115
116
117
118
119
120
121
122
123
124

  /*!
  * \brief Set number of used model for prediction
  */
  inline void SetNumUsedModel(int num_used_model) {
    if (num_used_model >= 0) {
      num_used_model_ = static_cast<int>(num_used_model / num_class_);
    }
  }

125
  
126
127
128
129
130
  /*!
  * \brief Get Type name of this boosting object
  */
  const char* Name() const override { return "gbdt"; }

Guolin Ke's avatar
Guolin Ke committed
131
132
133
134
private:
  /*!
  * \brief Implement bagging logic
  * \param iter Current interation
135
  * \param curr_class Current class for multiclass training
Guolin Ke's avatar
Guolin Ke committed
136
  */
137
  void Bagging(int iter, const int curr_class);
Guolin Ke's avatar
Guolin Ke committed
138
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
139
140
  * \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
141
  * \param tree Trained tree of this iteration
142
  * \param curr_class Current class for multiclass training
Guolin Ke's avatar
Guolin Ke committed
143
  */
144
  void UpdateScoreOutOfBag(const Tree* tree, const int curr_class);
Guolin Ke's avatar
Guolin Ke committed
145
146
147
148
149
  /*!
  * \brief calculate the object function
  */
  void Boosting();
  /*!
Qiwei Ye's avatar
Qiwei Ye committed
150
  * \brief updating score after tree was trained
Guolin Ke's avatar
Guolin Ke committed
151
  * \param tree Trained tree of this iteration
152
  * \param curr_class Current class for multiclass training
Guolin Ke's avatar
Guolin Ke committed
153
  */
154
  void UpdateScore(const Tree* tree, const int curr_class);
Guolin Ke's avatar
Guolin Ke committed
155
  /*!
Hui Xue's avatar
Hui Xue committed
156
  * \brief Print metric result of current iteration
Guolin Ke's avatar
Guolin Ke committed
157
158
  * \param iter Current interation
  */
wxchan's avatar
wxchan committed
159
  bool OutputMetric(int iter);
wxchan's avatar
wxchan committed
160
161
162
163
  /*!
  * \brief Calculate feature importances
  * \param last_iter Last tree use to calculate
  */
164
165
166
  std::string FeatureImportance() const;
  /*! \brief current iteration */
  int iter_;
Guolin Ke's avatar
Guolin Ke committed
167
168
169
170
  /*! \brief Pointer to training data */
  const Dataset* train_data_;
  /*! \brief Config of gbdt */
  const GBDTConfig* gbdt_config_;
Hui Xue's avatar
Hui Xue committed
171
  /*! \brief Tree learner, will use this class to learn trees */
172
  std::vector<TreeLearner*> tree_learner_;
Guolin Ke's avatar
Guolin Ke committed
173
174
  /*! \brief Objective function */
  const ObjectiveFunction* object_function_;
Hui Xue's avatar
Hui Xue committed
175
  /*! \brief Store and update training data's score */
Guolin Ke's avatar
Guolin Ke committed
176
177
178
179
180
181
182
  ScoreUpdater* train_score_updater_;
  /*! \brief Metrics for training data */
  std::vector<const Metric*> training_metrics_;
  /*! \brief Store and update validation data's scores */
  std::vector<ScoreUpdater*> valid_score_updater_;
  /*! \brief Metric for validation data */
  std::vector<std::vector<const Metric*>> valid_metrics_;
wxchan's avatar
wxchan committed
183
184
  /*! \brief Number of rounds for early stopping */
  int early_stopping_round_;
wxchan's avatar
wxchan committed
185
186
  /*! \brief Best score(s) for early stopping */
  std::vector<std::vector<int>> best_iter_;
187
  std::vector<std::vector<double>> best_score_;
Guolin Ke's avatar
Guolin Ke committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
  /*! \brief Trained models(trees) */
  std::vector<Tree*> models_;
  /*! \brief Max feature index of training data*/
  int max_feature_idx_;
  /*! \brief First order derivative of training data */
  score_t* gradients_;
  /*! \brief Secend order derivative of training data */
  score_t* hessians_;
  /*! \brief Store the data indices of out-of-bag */
  data_size_t* out_of_bag_data_indices_;
  /*! \brief Number of out-of-bag data */
  data_size_t out_of_bag_data_cnt_;
  /*! \brief Store the indices of in-bag data */
  data_size_t* bag_data_indices_;
  /*! \brief Number of in-bag data */
  data_size_t bag_data_cnt_;
  /*! \brief Number of traning data */
  data_size_t num_data_;
206
207
  /*! \brief Number of classes */
  int num_class_;
Guolin Ke's avatar
Guolin Ke committed
208
209
210
211
212
213
  /*! \brief Random generator, used for bagging */
  Random random_;
  /*!
  *   \brief Sigmoid parameter, used for prediction.
  *          if > 0 meas output score will transform by sigmoid function
  */
214
  double sigmoid_;
Guolin Ke's avatar
Guolin Ke committed
215
216
  /*! \brief Index of label column */
  data_size_t label_idx_;
217
  /*! \brief Saved number of models */
218
  int saved_model_size_;
219
220
  /*! \brief File to write models */
  std::ofstream model_output_file_;
221
222
  /*! \brief number of used model */
  int num_used_model_;
Guolin Ke's avatar
Guolin Ke committed
223
224
225
};

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