config.h 11.7 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
20
21
22
#ifndef LIGHTGBM_CONFIG_H_
#define LIGHTGBM_CONFIG_H_

#include <LightGBM/utils/common.h>
#include <LightGBM/utils/log.h>

#include <vector>
#include <string>
#include <unordered_map>
#include <algorithm>

namespace LightGBM {

/*!
* \brief The interface for Config
*/
struct ConfigBase {
public:
  /*! \brief virtual destructor */
  virtual ~ConfigBase() {}

  /*!
Hui Xue's avatar
Hui Xue committed
23
  * \brief Set current config object by params
Guolin Ke's avatar
Guolin Ke committed
24
25
26
27
28
29
30
31
32
  * \param params Store the key and value for params
  */
  virtual void Set(
    const std::unordered_map<std::string, std::string>& params) = 0;

  /*!
  * \brief Get string value by specific name of key
  * \param params Store the key and value for params
  * \param name Name of key
Hui Xue's avatar
Hui Xue committed
33
  * \param out Value will assign to out if key exists
Guolin Ke's avatar
Guolin Ke committed
34
35
36
37
38
39
40
41
42
43
  * \return True if key exists
  */
  inline bool GetString(
    const std::unordered_map<std::string, std::string>& params,
    const std::string& name, std::string* out);

  /*!
  * \brief Get int value by specific name of key
  * \param params Store the key and value for params
  * \param name Name of key
Hui Xue's avatar
Hui Xue committed
44
  * \param out Value will assign to out if key exists
Guolin Ke's avatar
Guolin Ke committed
45
46
47
48
49
50
51
  * \return True if key exists
  */
  inline bool GetInt(
    const std::unordered_map<std::string, std::string>& params,
    const std::string& name, int* out);

  /*!
52
  * \brief Get float value by specific name of key
Guolin Ke's avatar
Guolin Ke committed
53
54
  * \param params Store the key and value for params
  * \param name Name of key
Hui Xue's avatar
Hui Xue committed
55
  * \param out Value will assign to out if key exists
Guolin Ke's avatar
Guolin Ke committed
56
57
  * \return True if key exists
  */
58
  inline bool GetFloat(
Guolin Ke's avatar
Guolin Ke committed
59
    const std::unordered_map<std::string, std::string>& params,
60
    const std::string& name, float* out);
Guolin Ke's avatar
Guolin Ke committed
61
62
63
64
65

  /*!
  * \brief Get bool value by specific name of key
  * \param params Store the key and value for params
  * \param name Name of key
Hui Xue's avatar
Hui Xue committed
66
  * \param out Value will assign to out if key exists
Guolin Ke's avatar
Guolin Ke committed
67
68
69
70
71
72
73
74
75
  * \return True if key exists
  */
  inline bool GetBool(
    const std::unordered_map<std::string, std::string>& params,
    const std::string& name, bool* out);
};

/*! \brief Types of boosting */
enum BoostingType {
76
  kGBDT, kUnknow
Guolin Ke's avatar
Guolin Ke committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
};


/*! \brief Types of tasks */
enum TaskType {
  kTrain, kPredict
};

/*! \brief Config for input and output files */
struct IOConfig: public ConfigBase {
public:
  int max_bin = 255;
  int data_random_seed = 1;
  std::string data_filename = "";
  std::vector<std::string> valid_data_filenames;
  std::string output_model = "LightGBM_model.txt";
  std::string output_result = "LightGBM_predict_result.txt";
  std::string input_model = "";
  std::string input_init_score = "";
Guolin Ke's avatar
Guolin Ke committed
96
  int verbosity = 1;
Guolin Ke's avatar
Guolin Ke committed
97
98
99
100
101
102
  int num_model_predict = -1;
  bool is_pre_partition = false;
  bool is_enable_sparse = true;
  bool use_two_round_loading = false;
  bool is_save_binary_file = false;
  bool is_sigmoid = true;
Guolin Ke's avatar
Guolin Ke committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116

  bool has_header = false;
  /*! \brief Index or column name of label, default is the first column
   * And add an prefix "name:" while using column name */
  std::string label_column = "";
  /*! \brief Index or column name of weight, < 0 means not used
  * And add an prefix "name:" while using column name */
  std::string weight_column = "";
  /*! \brief Index or column name of group, < 0 means not used */
  std::string group_column = "";
  /*! \brief ignored features, separate by ','
  * e.g. name:column_name1,column_name2  */
  std::string ignore_column = "";

Guolin Ke's avatar
Guolin Ke committed
117
118
119
120
121
122
123
  void Set(const std::unordered_map<std::string, std::string>& params) override;
};

/*! \brief Config for objective function */
struct ObjectiveConfig: public ConfigBase {
public:
  virtual ~ObjectiveConfig() {}
124
  float sigmoid = 1.0f;
Guolin Ke's avatar
Guolin Ke committed
125
  // for lambdarank
126
  std::vector<float> label_gain;
Guolin Ke's avatar
Guolin Ke committed
127
128
129
130
  // for lambdarank
  int max_position = 20;
  // for binary
  bool is_unbalance = false;
131
132
  // for multiclass
  int num_class = 1;
Guolin Ke's avatar
Guolin Ke committed
133
134
135
136
137
138
139
  void Set(const std::unordered_map<std::string, std::string>& params) override;
};

/*! \brief Config for metrics interface*/
struct MetricConfig: public ConfigBase {
public:
  virtual ~MetricConfig() {}
140
  int num_class = 1;
141
142
  float sigmoid = 1.0f;
  std::vector<float> label_gain;
Guolin Ke's avatar
Guolin Ke committed
143
144
145
146
147
148
149
150
151
  std::vector<int> eval_at;
  void Set(const std::unordered_map<std::string, std::string>& params) override;
};


/*! \brief Config for tree model */
struct TreeConfig: public ConfigBase {
public:
  int min_data_in_leaf = 100;
152
  float min_sum_hessian_in_leaf = 10.0f;
Guolin Ke's avatar
Guolin Ke committed
153
  // should > 1, only one leaf means not need to learning
Guolin Ke's avatar
Guolin Ke committed
154
155
  int num_leaves = 127;
  int feature_fraction_seed = 2;
156
  float feature_fraction = 1.0f;
Guolin Ke's avatar
Guolin Ke committed
157
  // max cache size(unit:MB) for historical histogram. < 0 means not limit
158
  float histogram_pool_size = -1.0f;
Guolin Ke's avatar
Guolin Ke committed
159
160
161
162
163
  // max depth of tree model. 
  // Still grow tree by leaf-wise, but limit the max depth to avoid over-fitting
  // And the max leaves will be min(num_leaves, pow(2, max_depth - 1)) 
  // max_depth < 0 means not limit
  int max_depth = -1;
Guolin Ke's avatar
Guolin Ke committed
164
165
166
167
168
169
170
171
172
173
174
175
176
  void Set(const std::unordered_map<std::string, std::string>& params) override;
};

/*! \brief Types of tree learning algorithms */
enum TreeLearnerType {
  kSerialTreeLearner, kFeatureParallelTreelearner,
  kDataParallelTreeLearner
};

/*! \brief Config for Boosting */
struct BoostingConfig: public ConfigBase {
public:
  virtual ~BoostingConfig() {}
177
178
  int output_freq = 1;
  bool is_provide_training_metric = false;
Guolin Ke's avatar
Guolin Ke committed
179
  int num_iterations = 10;
180
181
  float learning_rate = 0.1f;
  float bagging_fraction = 1.0f;
Guolin Ke's avatar
Guolin Ke committed
182
183
  int bagging_seed = 3;
  int bagging_freq = 0;
wxchan's avatar
wxchan committed
184
  int early_stopping_round = 0;
185
  int num_class = 1;
Guolin Ke's avatar
Guolin Ke committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
  void Set(const std::unordered_map<std::string, std::string>& params) override;
};

/*! \brief Config for GBDT */
struct GBDTConfig: public BoostingConfig {
public:
  TreeLearnerType tree_learner_type = TreeLearnerType::kSerialTreeLearner;
  TreeConfig tree_config;
  void Set(const std::unordered_map<std::string, std::string>& params) override;

private:
  void GetTreeLearnerType(const std::unordered_map<std::string,
                                         std::string>& params);
};

/*! \brief Config for Network */
struct NetworkConfig: public ConfigBase {
public:
  int num_machines = 1;
  int local_listen_port = 12400;
  int time_out = 120;  // in minutes
  std::string machine_list_filename = "";
  void Set(const std::unordered_map<std::string, std::string>& params) override;
};


/*! \brief Overall config, all configs will put on this class */
struct OverallConfig: public ConfigBase {
public:
  TaskType task_type = TaskType::kTrain;
  NetworkConfig network_config;
  int num_threads = 0;
  bool is_parallel = false;
  bool is_parallel_find_bin = false;
wxchan's avatar
wxchan committed
220
  bool predict_leaf_index = false;
Guolin Ke's avatar
Guolin Ke committed
221
222
223
224
225
226
227
228
229
230
231
  IOConfig io_config;
  BoostingType boosting_type = BoostingType::kGBDT;
  BoostingConfig* boosting_config;
  std::string objective_type = "regression";
  ObjectiveConfig objective_config;
  std::vector<std::string> metric_types;
  MetricConfig metric_config;
  ~OverallConfig() {
    delete boosting_config;
  }
  void Set(const std::unordered_map<std::string, std::string>& params) override;
232
  void LoadFromString(const char* str);
Guolin Ke's avatar
Guolin Ke committed
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
private:
  void GetBoostingType(const std::unordered_map<std::string, std::string>& params);

  void GetObjectiveType(const std::unordered_map<std::string, std::string>& params);

  void GetMetricType(const std::unordered_map<std::string, std::string>& params);

  void GetTaskType(const std::unordered_map<std::string, std::string>& params);

  void CheckParamConflict();
};


inline bool ConfigBase::GetString(
  const std::unordered_map<std::string, std::string>& params,
  const std::string& name, std::string* out) {
  if (params.count(name) > 0) {
    *out = params.at(name);
    return true;
  }
  return false;
}

inline bool ConfigBase::GetInt(
  const std::unordered_map<std::string, std::string>& params,
  const std::string& name, int* out) {
  if (params.count(name) > 0) {
260
261
262
263
    if (!Common::AtoiAndCheck(params.at(name).c_str(), out)) {
      Log::Fatal("Parameter %s should be int type, passed is [%s]", 
        name.c_str(), params.at(name).c_str());
    }
Guolin Ke's avatar
Guolin Ke committed
264
265
266
267
268
    return true;
  }
  return false;
}

269
inline bool ConfigBase::GetFloat(
Guolin Ke's avatar
Guolin Ke committed
270
  const std::unordered_map<std::string, std::string>& params,
271
  const std::string& name, float* out) {
Guolin Ke's avatar
Guolin Ke committed
272
  if (params.count(name) > 0) {
273
274
275
276
    if (!Common::AtofAndCheck(params.at(name).c_str(), out)) {
      Log::Fatal("Parameter %s should be float type, passed is [%s]",
        name.c_str(), params.at(name).c_str());
    }
Guolin Ke's avatar
Guolin Ke committed
277
278
279
280
281
282
283
284
285
286
287
    return true;
  }
  return false;
}

inline bool ConfigBase::GetBool(
  const std::unordered_map<std::string, std::string>& params,
  const std::string& name, bool* out) {
  if (params.count(name) > 0) {
    std::string value = params.at(name);
    std::transform(value.begin(), value.end(), value.begin(), ::tolower);
288
    if (value == std::string("false") || value == std::string("-")) {
Guolin Ke's avatar
Guolin Ke committed
289
      *out = false;
290
    } else if (value == std::string("true") || value == std::string("+")) {
Guolin Ke's avatar
Guolin Ke committed
291
      *out = true;
292
293
294
    } else {
      Log::Fatal("Parameter %s should be \"true\"/\"+\" or \"false\"/\"-\", passed is [%s]",
        name.c_str(), params.at(name).c_str());
Guolin Ke's avatar
Guolin Ke committed
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
    }
    return true;
  }
  return false;
}

struct ParameterAlias {
  static void KeyAliasTransform(std::unordered_map<std::string, std::string>* params) {
    std::unordered_map<std::string, std::string> alias_table(
    {
      { "config", "config_file" },
      { "nthread", "num_threads" },
      { "num_thread", "num_threads" },
      { "boosting", "boosting_type" },
      { "boost", "boosting_type" },
      { "application", "objective" },
      { "app", "objective" },
      { "train_data", "data" },
      { "train", "data" },
      { "model_output", "output_model" },
      { "model_out", "output_model" },
      { "model_input", "input_model" },
      { "model_in", "input_model" },
      { "init_score", "input_init_score"},
      { "predict_result", "output_result" },
      { "prediction_result", "output_result" },
      { "valid", "valid_data" },
      { "test_data", "valid_data" },
      { "test", "valid_data" },
      { "is_sparse", "is_enable_sparse" },
      { "tranining_metric", "is_training_metric" },
      { "train_metric", "is_training_metric" },
      { "ndcg_at", "ndcg_eval_at" },
      { "min_data_per_leaf", "min_data_in_leaf" },
      { "min_data", "min_data_in_leaf" },
      { "min_sum_hessian_per_leaf", "min_sum_hessian_in_leaf" },
      { "min_sum_hessian", "min_sum_hessian_in_leaf" },
      { "min_hessian", "min_sum_hessian_in_leaf" },
      { "num_leaf", "num_leaves" },
      { "sub_feature", "feature_fraction" },
      { "num_iteration", "num_iterations" },
      { "num_tree", "num_iterations" },
      { "num_round", "num_iterations" },
      { "num_trees", "num_iterations" },
      { "num_rounds", "num_iterations" },
      { "sub_row", "bagging_fraction" },
      { "shrinkage_rate", "learning_rate" },
      { "tree", "tree_learner" },
      { "num_machine", "num_machines" },
      { "local_port", "local_listen_port" },
      { "two_round_loading", "use_two_round_loading"},
      { "two_round", "use_two_round_loading" },
      { "mlist", "machine_list_file" },
      { "is_save_binary", "is_save_binary_file" },
Qiwei Ye's avatar
Qiwei Ye committed
349
      { "save_binary", "is_save_binary_file" },
wxchan's avatar
wxchan committed
350
      { "early_stopping_rounds", "early_stopping_round"},
351
      { "early_stopping", "early_stopping_round"},
Guolin Ke's avatar
Guolin Ke committed
352
353
354
355
356
357
358
359
360
      { "verbosity", "verbose" },
      { "header", "has_header" },
      { "label", "label_column" },
      { "weight", "weight_column" },
      { "group", "group_column" },
      { "query", "group_column" },
      { "query_column", "group_column" },
      { "ignore_feature", "ignore_column" },
      { "blacklist", "ignore_column" }
Guolin Ke's avatar
Guolin Ke committed
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
    });
    std::unordered_map<std::string, std::string> tmp_map;
    for (const auto& pair : *params) {
      if (alias_table.count(pair.first) > 0) {
        tmp_map[alias_table[pair.first]] = pair.second;
      }
    }
    for (const auto& pair : tmp_map) {
      if (params->count(pair.first) == 0) {
        params->insert(std::make_pair(pair.first, pair.second));
      }
    }
  }
};

}   // namespace LightGBM

Guolin Ke's avatar
Guolin Ke committed
378
#endif   // LightGBM_CONFIG_H_