boosting.h 6.94 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef LIGHTGBM_BOOSTING_H_
#define LIGHTGBM_BOOSTING_H_

#include <LightGBM/meta.h>
#include <LightGBM/config.h>

#include <vector>
#include <string>

namespace LightGBM {

/*! \brief forward declaration */
class Dataset;
class ObjectiveFunction;
class Metric;

/*!
* \brief The interface for Boosting
*/
20
class LIGHTGBM_EXPORT Boosting {
Guolin Ke's avatar
Guolin Ke committed
21
22
23
24
25
public:
  /*! \brief virtual destructor */
  virtual ~Boosting() {}

  /*!
Qiwei Ye's avatar
Qiwei Ye committed
26
27
  * \brief Initialization logic
  * \param config Configs for boosting
Guolin Ke's avatar
Guolin Ke committed
28
  * \param train_data Training data
29
  * \param objective_function Training objective function
Guolin Ke's avatar
Guolin Ke committed
30
31
  * \param training_metrics Training metric
  */
32
33
34
  virtual void Init(
    const BoostingConfig* config,
    const Dataset* train_data,
35
    const ObjectiveFunction* objective_function,
36
    const std::vector<const Metric*>& training_metrics) = 0;
Guolin Ke's avatar
Guolin Ke committed
37

wxchan's avatar
wxchan committed
38
39
40
41
42
43
44
45
46
47
48
  /*!
  * \brief Merge model from other boosting object
           Will insert to the front of current boosting object
  * \param other
  */
  virtual void MergeFrom(const Boosting* other) = 0;

  /*!
  * \brief Reset training data for current boosting
  * \param config Configs for boosting
  * \param train_data Training data
49
  * \param objective_function Training objective function
wxchan's avatar
wxchan committed
50
51
  * \param training_metrics Training metric
  */
52
  virtual void ResetTrainingData(const BoostingConfig* config, const Dataset* train_data, const ObjectiveFunction* objective_function, const std::vector<const Metric*>& training_metrics) = 0;
wxchan's avatar
wxchan committed
53

Guolin Ke's avatar
Guolin Ke committed
54
55
56
57
58
  /*!
  * \brief Add a validation data
  * \param valid_data Validation data
  * \param valid_metrics Metric for validation data
  */
wxchan's avatar
wxchan committed
59
  virtual void AddValidDataset(const Dataset* valid_data,
Guolin Ke's avatar
Guolin Ke committed
60
61
    const std::vector<const Metric*>& valid_metrics) = 0;

Guolin Ke's avatar
Guolin Ke committed
62
63
64
65
  /*!
  * \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
66
  * \param is_eval true if need evaluation or early stop
Guolin Ke's avatar
Guolin Ke committed
67
68
  * \return True if meet early stopping or cannot boosting
  */
Guolin Ke's avatar
Guolin Ke committed
69
  virtual bool TrainOneIter(const float* gradient, const float* hessian, bool is_eval) = 0;
70

wxchan's avatar
wxchan committed
71
72
73
74
75
76
77
78
79
80
81
82
83
  /*!
  * \brief Rollback one iteration
  */
  virtual void RollbackOneIter() = 0;

  /*!
  * \brief return current iteration
  */
  virtual int GetCurrentIteration() const = 0;

  /*!
  * \brief Eval metrics and check is met early stopping or not
  */
Guolin Ke's avatar
Guolin Ke committed
84
  virtual bool EvalAndCheckEarlyStopping() = 0;
Guolin Ke's avatar
Guolin Ke committed
85
86
87
88
89
  /*!
  * \brief Get evaluation result at data_idx data
  * \param data_idx 0: training data, 1: 1st validation data
  * \return evaluation result
  */
90
  virtual std::vector<double> GetEvalAt(int data_idx) const = 0;
91

Guolin Ke's avatar
Guolin Ke committed
92
93
  /*!
  * \brief Get current training score
Guolin Ke's avatar
Guolin Ke committed
94
  * \param out_len length of returned score
Guolin Ke's avatar
Guolin Ke committed
95
96
  * \return training score
  */
97
  virtual const double* GetTrainingScore(int64_t* out_len) = 0;
Guolin Ke's avatar
Guolin Ke committed
98

Guolin Ke's avatar
Guolin Ke committed
99
100
101
  /*!
  * \brief Get prediction result at data_idx data
  * \param data_idx 0: training data, 1: 1st validation data
102
  * \return out_len length of returned score
Guolin Ke's avatar
Guolin Ke committed
103
104
  */
  virtual int64_t GetNumPredictAt(int data_idx) const = 0;
Guolin Ke's avatar
Guolin Ke committed
105
106
107
108
  /*!
  * \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
109
  * \param out_len length of returned score
Guolin Ke's avatar
Guolin Ke committed
110
  */
Guolin Ke's avatar
Guolin Ke committed
111
  virtual void GetPredictAt(int data_idx, double* result, int64_t* out_len) = 0;
Guolin Ke's avatar
Guolin Ke committed
112

Guolin Ke's avatar
Guolin Ke committed
113
114
  virtual int NumPredictOneRow(int num_iteration, int is_pred_leaf) const = 0;

Guolin Ke's avatar
Guolin Ke committed
115
  /*!
Hui Xue's avatar
Hui Xue committed
116
  * \brief Prediction for one record, not sigmoid transform
Guolin Ke's avatar
Guolin Ke committed
117
  * \param feature_values Feature value on this record
Guolin Ke's avatar
Guolin Ke committed
118
  * \param output Prediction result for this record
Guolin Ke's avatar
Guolin Ke committed
119
  */
120
  virtual void PredictRaw(const double* features, double* output) const = 0;
Guolin Ke's avatar
Guolin Ke committed
121
122

  /*!
Qiwei Ye's avatar
Qiwei Ye committed
123
  * \brief Prediction for one record, sigmoid transformation will be used if needed
Guolin Ke's avatar
Guolin Ke committed
124
  * \param feature_values Feature value on this record
Guolin Ke's avatar
Guolin Ke committed
125
  * \param output Prediction result for this record
Guolin Ke's avatar
Guolin Ke committed
126
  */
127
  virtual void Predict(const double* features, double* output) const = 0;
wxchan's avatar
wxchan committed
128
129
  
  /*!
130
  * \brief Prediction for one record with leaf index
wxchan's avatar
wxchan committed
131
  * \param feature_values Feature value on this record
Guolin Ke's avatar
Guolin Ke committed
132
  * \param output Prediction result for this record
wxchan's avatar
wxchan committed
133
  */
Guolin Ke's avatar
Guolin Ke committed
134
  virtual void PredictLeafIndex(
135
    const double* features, double* output) const = 0;
136

Guolin Ke's avatar
Guolin Ke committed
137
  /*!
wxchan's avatar
wxchan committed
138
139
140
  * \brief Dump model to json format string
  * \return Json format string of model
  */
141
  virtual std::string DumpModel(int num_iteration) const = 0;
wxchan's avatar
wxchan committed
142
143
144
145
146
147

  /*!
  * \brief Save model to file
  * \param num_used_model Number of model that want to save, -1 means save all
  * \param is_finish Is training finished or not
  * \param filename Filename that want to save to
148
  * \return true if succeeded
Guolin Ke's avatar
Guolin Ke committed
149
  */
150
  virtual bool SaveModelToFile(int num_iterations, const char* filename) const = 0;
Guolin Ke's avatar
Guolin Ke committed
151

152
153
154
155
156
157
158
  /*!
  * \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
  */
  virtual std::string SaveModelToString(int num_iterations) const = 0;

Guolin Ke's avatar
Guolin Ke committed
159
160
161
  /*!
  * \brief Restore from a serialized string
  * \param model_str The string of model
162
  * \return true if succeeded
Guolin Ke's avatar
Guolin Ke committed
163
  */
164
  virtual bool LoadModelFromString(const std::string& model_str) = 0;
Guolin Ke's avatar
Guolin Ke committed
165
166
167
168
169
170
171

  /*!
  * \brief Get max feature index of this model
  * \return Max feature index of this model
  */
  virtual int MaxFeatureIdx() const = 0;

wxchan's avatar
wxchan committed
172
173
174
175
176
177
  /*!
  * \brief Get feature names of this model
  * \return Feature names of this model
  */
  virtual std::vector<std::string> FeatureNames() const = 0;

Guolin Ke's avatar
Guolin Ke committed
178
179
180
181
182
183
  /*!
  * \brief Get index of label column
  * \return index of label column
  */
  virtual int LabelIdx() const = 0;

Guolin Ke's avatar
Guolin Ke committed
184
185
186
187
  /*!
  * \brief Get number of weak sub-models
  * \return Number of weak sub-models
  */
wxchan's avatar
wxchan committed
188
  virtual int NumberOfTotalModel() const = 0;
189
  
Guolin Ke's avatar
Guolin Ke committed
190
191
192
193
194
195
  /*!
  * \brief Get number of trees per iteration
  * \return Number of trees per iteration
  */
  virtual int NumTreePerIteration() const = 0;

196
197
198
199
  /*!
  * \brief Get number of classes
  * \return Number of classes
  */
Guolin Ke's avatar
Guolin Ke committed
200
  virtual int NumberOfClasses() const = 0;
201
202

  /*!
Guolin Ke's avatar
Guolin Ke committed
203
204
  * \brief Initial work for the prediction
  * \param num_iteration number of used iteration
205
  */
206
  virtual void InitPredict(int num_iteration) = 0;
207
  
208
  /*!
Guolin Ke's avatar
Guolin Ke committed
209
  * \brief Name of submodel
210
  */
Guolin Ke's avatar
Guolin Ke committed
211
  virtual const char* SubModelName() const = 0;
212

Guolin Ke's avatar
Guolin Ke committed
213
214
215
216
217
218
  Boosting() = default;
  /*! \brief Disable copy */
  Boosting& operator=(const Boosting&) = delete;
  /*! \brief Disable copy */
  Boosting(const Boosting&) = delete;

219
  static bool LoadFileToBoosting(Boosting* boosting, const char* filename);
wxchan's avatar
wxchan committed
220

Guolin Ke's avatar
Guolin Ke committed
221
222
223
  /*!
  * \brief Create boosting object
  * \param type Type of boosting
224
225
  * \param config config for boosting
  * \param filename name of model file, if existing will continue to train from this model
Guolin Ke's avatar
Guolin Ke committed
226
227
  * \return The boosting object
  */
Guolin Ke's avatar
Guolin Ke committed
228
  static Boosting* CreateBoosting(const std::string& type, const char* filename);
229
230
231
232
233
234
235
236

  /*!
  * \brief Create boosting object from model file
  * \param filename name of model file
  * \return The boosting object
  */
  static Boosting* CreateBoosting(const char* filename);

Guolin Ke's avatar
Guolin Ke committed
237
238
239
240
};

}  // namespace LightGBM

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