config.h 15.4 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
6
#ifndef LIGHTGBM_CONFIG_H_
#define LIGHTGBM_CONFIG_H_

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

Guolin Ke's avatar
Guolin Ke committed
7
#include <LightGBM/meta.h>
8
#include <LightGBM/export.h>
Guolin Ke's avatar
Guolin Ke committed
9

Guolin Ke's avatar
Guolin Ke committed
10
11
12
13
#include <vector>
#include <string>
#include <unordered_map>
#include <algorithm>
Guolin Ke's avatar
Guolin Ke committed
14
#include <memory>
Guolin Ke's avatar
Guolin Ke committed
15
16
17
18
19
20
21
22
23
24
25
26

namespace LightGBM {

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

  /*!
Hui Xue's avatar
Hui Xue committed
27
  * \brief Set current config object by params
Guolin Ke's avatar
Guolin Ke committed
28
29
30
31
32
33
34
35
36
  * \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
37
  * \param out Value will assign to out if key exists
Guolin Ke's avatar
Guolin Ke committed
38
39
40
41
42
43
44
45
46
47
  * \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
48
  * \param out Value will assign to out if key exists
Guolin Ke's avatar
Guolin Ke committed
49
50
51
52
53
54
55
  * \return True if key exists
  */
  inline bool GetInt(
    const std::unordered_map<std::string, std::string>& params,
    const std::string& name, int* out);

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

  /*!
  * \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
70
  * \param out Value will assign to out if key exists
Guolin Ke's avatar
Guolin Ke committed
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);
76
77

  static std::unordered_map<std::string, std::string> Str2Map(const char* parameters);
Guolin Ke's avatar
Guolin Ke committed
78
79
80
81
};

/*! \brief Types of tasks */
enum TaskType {
82
  kTrain, kPredict, kConvertModel
Guolin Ke's avatar
Guolin Ke committed
83
84
85
86
87
};

/*! \brief Config for input and output files */
struct IOConfig: public ConfigBase {
public:
88
  int max_bin = 255;
89
  int num_class = 1;
Guolin Ke's avatar
Guolin Ke committed
90
91
92
  int data_random_seed = 1;
  std::string data_filename = "";
  std::vector<std::string> valid_data_filenames;
Guolin Ke's avatar
Guolin Ke committed
93
  int snapshot_freq = 100;
Guolin Ke's avatar
Guolin Ke committed
94
95
  std::string output_model = "LightGBM_model.txt";
  std::string output_result = "LightGBM_predict_result.txt";
96
  std::string convert_model = "gbdt_prediction.cpp";
Guolin Ke's avatar
Guolin Ke committed
97
  std::string input_model = "";
Guolin Ke's avatar
Guolin Ke committed
98
  int verbosity = 1;
99
  int num_iteration_predict = -1;
Guolin Ke's avatar
Guolin Ke committed
100
101
  bool is_pre_partition = false;
  bool is_enable_sparse = true;
102
103
104
105
106
  /*! \brief The threshold of zero elements precentage for treating a feature as a sparse feature.
   *  Default is 0.8, where a feature is treated as a sparse feature when there are over 80% zeros.
   *  When setting to 1.0, all features are processed as dense features.
   */
  double sparse_threshold = 0.8;
Guolin Ke's avatar
Guolin Ke committed
107
108
  bool use_two_round_loading = false;
  bool is_save_binary_file = false;
Guolin Ke's avatar
Guolin Ke committed
109
  bool enable_load_from_binary_file = true;
Guolin Ke's avatar
Guolin Ke committed
110
  int bin_construct_sample_cnt = 200000;
Guolin Ke's avatar
Guolin Ke committed
111
112
  bool is_predict_leaf_index = false;
  bool is_predict_raw_score = false;
113
  int min_data_in_leaf = 20;
Guolin Ke's avatar
Guolin Ke committed
114
  int min_data_in_bin = 5;
115
  double max_conflict_rate = 0.0f;
Guolin Ke's avatar
Guolin Ke committed
116
  bool enable_bundle = true;
Guolin Ke's avatar
Guolin Ke committed
117
118
119
120
121
  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
Guolin Ke's avatar
Guolin Ke committed
122
  * And add an prefix "name:" while using column name 
123
  * Note: when using Index, it doesn't count the label index */
Guolin Ke's avatar
Guolin Ke committed
124
  std::string weight_column = "";
Guolin Ke's avatar
Guolin Ke committed
125
126
  /*! \brief Index or column name of group/query id, < 0 means not used
  * And add an prefix "name:" while using column name
127
  * Note: when using Index, it doesn't count the label index */
Guolin Ke's avatar
Guolin Ke committed
128
129
  std::string group_column = "";
  /*! \brief ignored features, separate by ','
Guolin Ke's avatar
Guolin Ke committed
130
  * And add an prefix "name:" while using column name
131
  * Note: when using Index, it doesn't count the label index */
Guolin Ke's avatar
Guolin Ke committed
132
  std::string ignore_column = "";
133
134
  /*! \brief specific categorical columns, Note:only support for integer type categorical
  * And add an prefix "name:" while using column name
135
  * Note: when using Index, it doesn't count the label index */
136
  std::string categorical_column = "";
Guolin Ke's avatar
Guolin Ke committed
137
  std::string device_type = "cpu";
138
139
140
141
142
143
144
145

  /*! \brief Set to true if want to use early stop for the prediction */
  bool pred_early_stop = false;
  /*! \brief Frequency of checking the pred_early_stop */
  int pred_early_stop_freq = 10;
  /*! \brief Threshold of margin of pred_early_stop */
  double pred_early_stop_margin = 10.0f;

146
  LIGHTGBM_EXPORT void Set(const std::unordered_map<std::string, std::string>& params) override;
Guolin Ke's avatar
Guolin Ke committed
147
148
149
private:
  void GetDeviceType(const std::unordered_map<std::string,
                     std::string>& params);
Guolin Ke's avatar
Guolin Ke committed
150
151
152
153
154
155
};

/*! \brief Config for objective function */
struct ObjectiveConfig: public ConfigBase {
public:
  virtual ~ObjectiveConfig() {}
156
  double sigmoid = 1.0f;
Tsukasa OMOTO's avatar
Tsukasa OMOTO committed
157
  double huber_delta = 1.0f;
Tsukasa OMOTO's avatar
Tsukasa OMOTO committed
158
  double fair_c = 1.0f;
159
  // for Approximate Hessian With Gaussian
160
  double gaussian_eta = 1.0f;
161
  double poisson_max_delta_step = 0.7f;
Guolin Ke's avatar
Guolin Ke committed
162
  // for lambdarank
163
  std::vector<double> label_gain;
Guolin Ke's avatar
Guolin Ke committed
164
165
166
167
  // for lambdarank
  int max_position = 20;
  // for binary
  bool is_unbalance = false;
168
169
  // for multiclass
  int num_class = 1;
Guolin Ke's avatar
Guolin Ke committed
170
171
  // Balancing of positive and negative weights
  double scale_pos_weight = 1.0f;
172
  LIGHTGBM_EXPORT void Set(const std::unordered_map<std::string, std::string>& params) override;
Guolin Ke's avatar
Guolin Ke committed
173
174
175
176
177
178
};

/*! \brief Config for metrics interface*/
struct MetricConfig: public ConfigBase {
public:
  virtual ~MetricConfig() {}
179
  int num_class = 1;
180
  double sigmoid = 1.0f;
Tsukasa OMOTO's avatar
Tsukasa OMOTO committed
181
  double huber_delta = 1.0f;
Tsukasa OMOTO's avatar
Tsukasa OMOTO committed
182
  double fair_c = 1.0f;
183
  std::vector<double> label_gain;
Guolin Ke's avatar
Guolin Ke committed
184
  std::vector<int> eval_at;
185
  LIGHTGBM_EXPORT void Set(const std::unordered_map<std::string, std::string>& params) override;
Guolin Ke's avatar
Guolin Ke committed
186
187
188
189
190
191
};


/*! \brief Config for tree model */
struct TreeConfig: public ConfigBase {
public:
192
193
  int min_data_in_leaf = 20;
  double min_sum_hessian_in_leaf = 1e-3f;
194
195
196
  double lambda_l1 = 0.0f;
  double lambda_l2 = 0.0f;
  double min_gain_to_split = 0.0f;
197
198
  // should > 1
  int num_leaves = 31;
Guolin Ke's avatar
Guolin Ke committed
199
  int feature_fraction_seed = 2;
200
  double feature_fraction = 1.0f;
tks's avatar
tks committed
201
  // max cache size(unit:MB) for historical histogram. < 0 means no limit
202
  double histogram_pool_size = -1.0f;
203
  // max depth of tree model.
Guolin Ke's avatar
Guolin Ke committed
204
  // Still grow tree by leaf-wise, but limit the max depth to avoid over-fitting
tks's avatar
tks committed
205
206
  // And the max leaves will be min(num_leaves, pow(2, max_depth))
  // max_depth < 0 means no limit
207
  int max_depth = -1;
Guolin Ke's avatar
Guolin Ke committed
208
  int top_k = 20;
209
210
211
212
213
214
215
216
217
218
  /*! \brief OpenCL platform ID. Usually each GPU vendor exposes one OpenCL platform.
   *  Default value is -1, using the system-wide default platform
   */
  int gpu_platform_id = -1;
  /*! \brief OpenCL device ID in the specified platform. Each GPU in the selected platform has a
   *  unique device ID. Default value is -1, using the default device in the selected platform
   */
  int gpu_device_id = -1;
  /*! \brief Set to true to use double precision math on GPU (default using single precision) */
  bool gpu_use_dp = false;
219
220
  /*! \brief Set to false to disable the handle of missing values */
  bool use_missing = true;
221
  LIGHTGBM_EXPORT void Set(const std::unordered_map<std::string, std::string>& params) override;
Guolin Ke's avatar
Guolin Ke committed
222
223
224
225
226
227
};

/*! \brief Config for Boosting */
struct BoostingConfig: public ConfigBase {
public:
  virtual ~BoostingConfig() {}
Guolin Ke's avatar
Guolin Ke committed
228
  double sigmoid = 1.0f;
229
230
  int output_freq = 1;
  bool is_provide_training_metric = false;
231
  int num_iterations = 100;
232
233
  double learning_rate = 0.1f;
  double bagging_fraction = 1.0f;
Guolin Ke's avatar
Guolin Ke committed
234
235
  int bagging_seed = 3;
  int bagging_freq = 0;
wxchan's avatar
wxchan committed
236
  int early_stopping_round = 0;
237
  int num_class = 1;
238
239
240
241
242
  double drop_rate = 0.1;
  int max_drop = 50;
  double skip_drop = 0.5;
  bool xgboost_dart_mode = false;
  bool uniform_drop = false;
Guolin Ke's avatar
Guolin Ke committed
243
  int drop_seed = 4;
Guolin Ke's avatar
Guolin Ke committed
244
245
  double top_rate = 0.2f;
  double other_rate = 0.1f;
246
  // only used for the regression. Will boost from the average labels.
247
  bool boost_from_average = true;
248
  std::string tree_learner_type = "serial";
249
  std::string device_type = "cpu";
Guolin Ke's avatar
Guolin Ke committed
250
  TreeConfig tree_config;
251
  LIGHTGBM_EXPORT void Set(const std::unordered_map<std::string, std::string>& params) override;
Guolin Ke's avatar
Guolin Ke committed
252
253
private:
  void GetTreeLearnerType(const std::unordered_map<std::string,
Guolin Ke's avatar
Guolin Ke committed
254
    std::string>& params);
255
256
  void GetDeviceType(const std::unordered_map<std::string,
    std::string>& params);
Guolin Ke's avatar
Guolin Ke committed
257
258
259
260
261
262
263
264
265
};

/*! \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 = "";
266
  LIGHTGBM_EXPORT void Set(const std::unordered_map<std::string, std::string>& params) override;
Guolin Ke's avatar
Guolin Ke committed
267
268
269
270
271
272
273
274
};


/*! \brief Overall config, all configs will put on this class */
struct OverallConfig: public ConfigBase {
public:
  TaskType task_type = TaskType::kTrain;
  NetworkConfig network_config;
Guolin Ke's avatar
Guolin Ke committed
275
  int seed = 0;
Guolin Ke's avatar
Guolin Ke committed
276
277
278
279
  int num_threads = 0;
  bool is_parallel = false;
  bool is_parallel_find_bin = false;
  IOConfig io_config;
Guolin Ke's avatar
Guolin Ke committed
280
  std::string boosting_type = "gbdt";
Guolin Ke's avatar
Guolin Ke committed
281
  BoostingConfig boosting_config;
Guolin Ke's avatar
Guolin Ke committed
282
283
284
285
  std::string objective_type = "regression";
  ObjectiveConfig objective_config;
  std::vector<std::string> metric_types;
  MetricConfig metric_config;
286
  std::string convert_model_language = "";
Guolin Ke's avatar
Guolin Ke committed
287

288
  LIGHTGBM_EXPORT void Set(const std::unordered_map<std::string, std::string>& params) override;
289

Guolin Ke's avatar
Guolin Ke committed
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
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) {
317
    if (!Common::AtoiAndCheck(params.at(name).c_str(), out)) {
318
      Log::Fatal("Parameter %s should be of type int, got \"%s\"",
319
320
        name.c_str(), params.at(name).c_str());
    }
Guolin Ke's avatar
Guolin Ke committed
321
322
323
324
325
    return true;
  }
  return false;
}

326
inline bool ConfigBase::GetDouble(
Guolin Ke's avatar
Guolin Ke committed
327
  const std::unordered_map<std::string, std::string>& params,
328
  const std::string& name, double* out) {
Guolin Ke's avatar
Guolin Ke committed
329
  if (params.count(name) > 0) {
330
    if (!Common::AtofAndCheck(params.at(name).c_str(), out)) {
331
      Log::Fatal("Parameter %s should be of type double, got \"%s\"",
332
333
        name.c_str(), params.at(name).c_str());
    }
Guolin Ke's avatar
Guolin Ke committed
334
335
336
337
338
339
340
341
342
343
    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);
Guolin Ke's avatar
Guolin Ke committed
344
    std::transform(value.begin(), value.end(), value.begin(), Common::tolower);
345
    if (value == std::string("false") || value == std::string("-")) {
Guolin Ke's avatar
Guolin Ke committed
346
      *out = false;
347
    } else if (value == std::string("true") || value == std::string("+")) {
Guolin Ke's avatar
Guolin Ke committed
348
      *out = true;
349
    } else {
350
      Log::Fatal("Parameter %s should be \"true\"/\"+\" or \"false\"/\"-\", got \"%s\"",
351
        name.c_str(), params.at(name).c_str());
Guolin Ke's avatar
Guolin Ke committed
352
353
354
355
356
357
358
359
360
361
362
363
    }
    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" },
Guolin Ke's avatar
Guolin Ke committed
364
      { "random_seed", "seed" },
Guolin Ke's avatar
Guolin Ke committed
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
      { "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" },
      { "predict_result", "output_result" },
      { "prediction_result", "output_result" },
      { "valid", "valid_data" },
      { "test_data", "valid_data" },
      { "test", "valid_data" },
      { "is_sparse", "is_enable_sparse" },
Guolin Ke's avatar
Guolin Ke committed
382
383
      { "enable_sparse", "is_enable_sparse" },
      { "pre_partition", "is_pre_partition" },
Guolin Ke's avatar
Guolin Ke committed
384
385
386
      { "tranining_metric", "is_training_metric" },
      { "train_metric", "is_training_metric" },
      { "ndcg_at", "ndcg_eval_at" },
Guolin Ke's avatar
Guolin Ke committed
387
      { "eval_at", "ndcg_eval_at" },
Guolin Ke's avatar
Guolin Ke committed
388
389
      { "min_data_per_leaf", "min_data_in_leaf" },
      { "min_data", "min_data_in_leaf" },
Guolin Ke's avatar
Guolin Ke committed
390
      { "min_child_samples", "min_data_in_leaf" },
Guolin Ke's avatar
Guolin Ke committed
391
392
393
      { "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" },
Guolin Ke's avatar
Guolin Ke committed
394
      { "min_child_weight", "min_sum_hessian_in_leaf" },
Guolin Ke's avatar
Guolin Ke committed
395
396
      { "num_leaf", "num_leaves" },
      { "sub_feature", "feature_fraction" },
Guolin Ke's avatar
Guolin Ke committed
397
      { "colsample_bytree", "feature_fraction" },
Guolin Ke's avatar
Guolin Ke committed
398
399
400
401
402
403
      { "num_iteration", "num_iterations" },
      { "num_tree", "num_iterations" },
      { "num_round", "num_iterations" },
      { "num_trees", "num_iterations" },
      { "num_rounds", "num_iterations" },
      { "sub_row", "bagging_fraction" },
Guolin Ke's avatar
Guolin Ke committed
404
405
      { "subsample", "bagging_fraction" },
      { "subsample_freq", "bagging_freq" },
Guolin Ke's avatar
Guolin Ke committed
406
407
408
409
410
411
412
413
      { "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
414
      { "save_binary", "is_save_binary_file" },
wxchan's avatar
wxchan committed
415
      { "early_stopping_rounds", "early_stopping_round"},
416
      { "early_stopping", "early_stopping_round"},
Guolin Ke's avatar
Guolin Ke committed
417
418
419
420
421
422
423
424
      { "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" },
Guolin Ke's avatar
Guolin Ke committed
425
      { "blacklist", "ignore_column" },
426
427
428
      { "categorical_feature", "categorical_column" },
      { "cat_column", "categorical_column" },
      { "cat_feature", "categorical_column" },
Guolin Ke's avatar
Guolin Ke committed
429
      { "predict_raw_score", "is_predict_raw_score" },
Guolin Ke's avatar
Guolin Ke committed
430
      { "predict_leaf_index", "is_predict_leaf_index" }, 
Guolin Ke's avatar
Guolin Ke committed
431
432
      { "raw_score", "is_predict_raw_score" },
      { "leaf_index", "is_predict_leaf_index" },
Guolin Ke's avatar
Guolin Ke committed
433
      { "min_split_gain", "min_gain_to_split" },
Guolin Ke's avatar
Guolin Ke committed
434
      { "topk", "top_k" },
Guolin Ke's avatar
Guolin Ke committed
435
436
      { "reg_alpha", "lambda_l1" },
      { "reg_lambda", "lambda_l2" },
437
438
      { "num_classes", "num_class" },
      { "unbalanced_sets", "is_unbalance" }
Guolin Ke's avatar
Guolin Ke committed
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
    });
    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
456
#endif   // LightGBM_CONFIG_H_