"include/vscode:/vscode.git/clone" did not exist on "e50a9151ed0409183bee0dc8272e83b56f1f9e5a"
config.cpp 13.3 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
#include <LightGBM/config.h>

#include <LightGBM/utils/common.h>
Guolin Ke's avatar
Guolin Ke committed
4
#include <LightGBM/utils/random.h>
Guolin Ke's avatar
Guolin Ke committed
5
6
7
8
#include <LightGBM/utils/log.h>

#include <vector>
#include <string>
Guolin Ke's avatar
Guolin Ke committed
9
#include <unordered_set>
Guolin Ke's avatar
Guolin Ke committed
10
#include <algorithm>
Guolin Ke's avatar
Guolin Ke committed
11
#include <limits>
Guolin Ke's avatar
Guolin Ke committed
12
13
14

namespace LightGBM {

15
std::unordered_map<std::string, std::string> ConfigBase::Str2Map(const char* parameters) {
16
  std::unordered_map<std::string, std::string> params;
17
  auto args = Common::Split(parameters, " \t\n\r");
18
19
20
21
22
23
24
25
26
  for (auto arg : args) {
    std::vector<std::string> tmp_strs = Common::Split(arg.c_str(), '=');
    if (tmp_strs.size() == 2) {
      std::string key = Common::RemoveQuotationSymbol(Common::Trim(tmp_strs[0]));
      std::string value = Common::RemoveQuotationSymbol(Common::Trim(tmp_strs[1]));
      if (key.size() <= 0) {
        continue;
      }
      params[key] = value;
27
    } else if (Common::Trim(arg).size() > 0) {
Qiwei Ye's avatar
Qiwei Ye committed
28
      Log::Warning("Unknown parameter %s", arg.c_str());
29
30
31
    }
  }
  ParameterAlias::KeyAliasTransform(&params);
32
  return params;
33
34
}

Guolin Ke's avatar
Guolin Ke committed
35
36
37
void OverallConfig::Set(const std::unordered_map<std::string, std::string>& params) {
  // load main config types
  GetInt(params, "num_threads", &num_threads);
Guolin Ke's avatar
Guolin Ke committed
38
39
40
41
42
43
44
45
46
47

  // generate seeds by seed.
  if (GetInt(params, "seed", &seed)) {
    Random rand(seed);
    int int_max = std::numeric_limits<int>::max();
    io_config.data_random_seed = static_cast<int>(rand.NextInt(0, int_max));
    boosting_config.bagging_seed = static_cast<int>(rand.NextInt(0, int_max));
    boosting_config.drop_seed = static_cast<int>(rand.NextInt(0, int_max));
    boosting_config.tree_config.feature_fraction_seed = static_cast<int>(rand.NextInt(0, int_max));
  }
Guolin Ke's avatar
Guolin Ke committed
48
49
50
51
52
53
54
55
56
  GetTaskType(params);
  GetBoostingType(params);
  GetObjectiveType(params);
  GetMetricType(params);

  // sub-config setup
  network_config.Set(params);
  io_config.Set(params);

Guolin Ke's avatar
Guolin Ke committed
57
  boosting_config.Set(params);
Guolin Ke's avatar
Guolin Ke committed
58
59
60
61
  objective_config.Set(params);
  metric_config.Set(params);
  // check for conflicts
  CheckParamConflict();
Qiwei Ye's avatar
Qiwei Ye committed
62

Guolin Ke's avatar
Guolin Ke committed
63
  if (io_config.verbosity == 1) {
Qiwei Ye's avatar
Qiwei Ye committed
64
    LightGBM::Log::ResetLogLevel(LightGBM::LogLevel::Info);
65
  } else if (io_config.verbosity == 0) {
Qiwei Ye's avatar
Qiwei Ye committed
66
    LightGBM::Log::ResetLogLevel(LightGBM::LogLevel::Warning);
67
  } else if (io_config.verbosity >= 2) {
Qiwei Ye's avatar
Qiwei Ye committed
68
    LightGBM::Log::ResetLogLevel(LightGBM::LogLevel::Debug);
69
  } else {
Qiwei Ye's avatar
Qiwei Ye committed
70
    LightGBM::Log::ResetLogLevel(LightGBM::LogLevel::Fatal);
Guolin Ke's avatar
Guolin Ke committed
71
  }
Guolin Ke's avatar
Guolin Ke committed
72
73
74
75
76
}

void OverallConfig::GetBoostingType(const std::unordered_map<std::string, std::string>& params) {
  std::string value;
  if (GetString(params, "boosting_type", &value)) {
Guolin Ke's avatar
Guolin Ke committed
77
    std::transform(value.begin(), value.end(), value.begin(), Common::tolower);
Guolin Ke's avatar
Guolin Ke committed
78
79
    if (value == std::string("gbdt") || value == std::string("gbrt")) {
      boosting_type = BoostingType::kGBDT;
80
81
    } else if (value == std::string("dart")) {
      boosting_type = BoostingType::kDART;
Guolin Ke's avatar
Guolin Ke committed
82
    } else {
83
      Log::Fatal("Unknown boosting type %s", value.c_str());
Guolin Ke's avatar
Guolin Ke committed
84
85
86
87
88
89
90
    }
  }
}

void OverallConfig::GetObjectiveType(const std::unordered_map<std::string, std::string>& params) {
  std::string value;
  if (GetString(params, "objective", &value)) {
Guolin Ke's avatar
Guolin Ke committed
91
    std::transform(value.begin(), value.end(), value.begin(), Common::tolower);
Guolin Ke's avatar
Guolin Ke committed
92
93
94
95
96
97
98
99
100
101
    objective_type = value;
  }
}

void OverallConfig::GetMetricType(const std::unordered_map<std::string, std::string>& params) {
  std::string value;
  if (GetString(params, "metric", &value)) {
    // clear old metrics
    metric_types.clear();
    // to lower
Guolin Ke's avatar
Guolin Ke committed
102
    std::transform(value.begin(), value.end(), value.begin(), Common::tolower);
Guolin Ke's avatar
Guolin Ke committed
103
104
105
    // split
    std::vector<std::string> metrics = Common::Split(value.c_str(), ',');
    // remove dumplicate
Guolin Ke's avatar
Guolin Ke committed
106
    std::unordered_set<std::string> metric_sets;
Guolin Ke's avatar
Guolin Ke committed
107
    for (auto& metric : metrics) {
Guolin Ke's avatar
Guolin Ke committed
108
      std::transform(metric.begin(), metric.end(), metric.begin(), Common::tolower);
Guolin Ke's avatar
Guolin Ke committed
109
110
      if (metric_sets.count(metric) <= 0) {
        metric_sets.insert(metric);
Guolin Ke's avatar
Guolin Ke committed
111
112
      }
    }
Guolin Ke's avatar
Guolin Ke committed
113
114
    for (auto& metric : metric_sets) {
      metric_types.push_back(metric);
Guolin Ke's avatar
Guolin Ke committed
115
    }
Guolin Ke's avatar
Guolin Ke committed
116
    metric_types.shrink_to_fit();
Guolin Ke's avatar
Guolin Ke committed
117
118
119
120
121
122
123
  }
}


void OverallConfig::GetTaskType(const std::unordered_map<std::string, std::string>& params) {
  std::string value;
  if (GetString(params, "task", &value)) {
Guolin Ke's avatar
Guolin Ke committed
124
    std::transform(value.begin(), value.end(), value.begin(), Common::tolower);
Guolin Ke's avatar
Guolin Ke committed
125
126
127
128
129
130
    if (value == std::string("train") || value == std::string("training")) {
      task_type = TaskType::kTrain;
    } else if (value == std::string("predict") || value == std::string("prediction")
      || value == std::string("test")) {
      task_type = TaskType::kPredict;
    } else {
131
      Log::Fatal("Unknown task type %s", value.c_str());
Guolin Ke's avatar
Guolin Ke committed
132
133
134
135
136
    }
  }
}

void OverallConfig::CheckParamConflict() {
137

138
139
  // check if objective_type, metric_type, and num_class match
  bool objective_type_multiclass = (objective_type == std::string("multiclass"));
Guolin Ke's avatar
Guolin Ke committed
140
  int num_class_check = boosting_config.num_class;
141
142
143
144
145
146
147
148
  if (objective_type_multiclass) {
    if (num_class_check <= 2) {
      Log::Fatal("Number of classes should be specified and greater than 2 for multiclass training");
    }
  } else {
    if (task_type == TaskType::kTrain && num_class_check != 1) {
      Log::Fatal("Number of classes must be 1 for non-multiclass training");
    }
149
  }
150
151
152
153
154
155
  for (std::string metric_type : metric_types) {
    bool metric_type_multiclass = (metric_type == std::string("multi_logloss") || metric_type == std::string("multi_error"));
    if ((objective_type_multiclass && !metric_type_multiclass)
      || (!objective_type_multiclass && metric_type_multiclass)) {
      Log::Fatal("Objective and metrics don't match");
    }
156
  }
157

Guolin Ke's avatar
Guolin Ke committed
158
159
160
161
  if (network_config.num_machines > 1) {
    is_parallel = true;
  } else {
    is_parallel = false;
Guolin Ke's avatar
Guolin Ke committed
162
    boosting_config.tree_learner_type = TreeLearnerType::kSerialTreeLearner;
Guolin Ke's avatar
Guolin Ke committed
163
164
  }

Guolin Ke's avatar
Guolin Ke committed
165
  if (boosting_config.tree_learner_type == TreeLearnerType::kSerialTreeLearner) {
Guolin Ke's avatar
Guolin Ke committed
166
167
168
169
    is_parallel = false;
    network_config.num_machines = 1;
  }

Guolin Ke's avatar
Guolin Ke committed
170
171
  if (boosting_config.tree_learner_type == TreeLearnerType::kSerialTreeLearner ||
    boosting_config.tree_learner_type == TreeLearnerType::kFeatureParallelTreelearner) {
Guolin Ke's avatar
Guolin Ke committed
172
    is_parallel_find_bin = false;
Guolin Ke's avatar
Guolin Ke committed
173
  } else if (boosting_config.tree_learner_type == TreeLearnerType::kDataParallelTreeLearner) {
Guolin Ke's avatar
Guolin Ke committed
174
    is_parallel_find_bin = true;
Guolin Ke's avatar
Guolin Ke committed
175
    if (boosting_config.tree_config.histogram_pool_size >= 0) {
176
      Log::Warning("Histogram LRU queue was enabled (histogram_pool_size=%f). Will disable this to reduce communication costs"
177
        , boosting_config.tree_config.histogram_pool_size);
178
      // Change pool size to -1 (not limit) when using data parallel to reduce communication costs
Guolin Ke's avatar
Guolin Ke committed
179
      boosting_config.tree_config.histogram_pool_size = -1;
180
181
    }

Guolin Ke's avatar
Guolin Ke committed
182
183
184
185
186
187
  }
}

void IOConfig::Set(const std::unordered_map<std::string, std::string>& params) {
  GetInt(params, "max_bin", &max_bin);
  CHECK(max_bin > 0);
188
  GetInt(params, "num_class", &num_class);
Guolin Ke's avatar
Guolin Ke committed
189
  GetInt(params, "data_random_seed", &data_random_seed);
190
  GetString(params, "data", &data_filename);
Qiwei Ye's avatar
Qiwei Ye committed
191
  GetInt(params, "verbose", &verbosity);
Guolin Ke's avatar
Guolin Ke committed
192
  GetInt(params, "num_iteration_predict", &num_iteration_predict);
Guolin Ke's avatar
Guolin Ke committed
193
  GetInt(params, "bin_construct_sample_cnt", &bin_construct_sample_cnt);
Guolin Ke's avatar
Guolin Ke committed
194
195
196
197
  GetBool(params, "is_pre_partition", &is_pre_partition);
  GetBool(params, "is_enable_sparse", &is_enable_sparse);
  GetBool(params, "use_two_round_loading", &use_two_round_loading);
  GetBool(params, "is_save_binary_file", &is_save_binary_file);
Guolin Ke's avatar
Guolin Ke committed
198
  GetBool(params, "enable_load_from_binary_file", &enable_load_from_binary_file);
Guolin Ke's avatar
Guolin Ke committed
199
200
  GetBool(params, "is_predict_raw_score", &is_predict_raw_score);
  GetBool(params, "is_predict_leaf_index", &is_predict_leaf_index);
Guolin Ke's avatar
Guolin Ke committed
201
202
203
204
205
206
207
  GetString(params, "output_model", &output_model);
  GetString(params, "input_model", &input_model);
  GetString(params, "output_result", &output_result);
  std::string tmp_str = "";
  if (GetString(params, "valid_data", &tmp_str)) {
    valid_data_filenames = Common::Split(tmp_str.c_str(), ',');
  }
Guolin Ke's avatar
Guolin Ke committed
208
209
210
211
212
  GetBool(params, "has_header", &has_header);
  GetString(params, "label_column", &label_column);
  GetString(params, "weight_column", &weight_column);
  GetString(params, "group_column", &group_column);
  GetString(params, "ignore_column", &ignore_column);
Guolin Ke's avatar
Guolin Ke committed
213
  GetString(params, "categorical_column", &categorical_column);
Guolin Ke's avatar
Guolin Ke committed
214
215
216
217
218
}


void ObjectiveConfig::Set(const std::unordered_map<std::string, std::string>& params) {
  GetBool(params, "is_unbalance", &is_unbalance);
219
  GetDouble(params, "sigmoid", &sigmoid);
Guolin Ke's avatar
Guolin Ke committed
220
221
  GetInt(params, "max_position", &max_position);
  CHECK(max_position > 0);
222
223
  GetInt(params, "num_class", &num_class);
  CHECK(num_class >= 1);
Guolin Ke's avatar
Guolin Ke committed
224
  GetDouble(params, "scale_pos_weight", &scale_pos_weight);
Guolin Ke's avatar
Guolin Ke committed
225
226
  std::string tmp_str = "";
  if (GetString(params, "label_gain", &tmp_str)) {
Guolin Ke's avatar
Guolin Ke committed
227
    label_gain = Common::StringToArray<double>(tmp_str, ',');
Guolin Ke's avatar
Guolin Ke committed
228
229
230
  } else {
    // label_gain = 2^i - 1, may overflow, so we use 31 here
    const int max_label = 31;
231
    label_gain.push_back(0.0f);
Guolin Ke's avatar
Guolin Ke committed
232
    for (int i = 1; i < max_label; ++i) {
233
      label_gain.push_back(static_cast<double>((1 << i) - 1));
Guolin Ke's avatar
Guolin Ke committed
234
235
    }
  }
Guolin Ke's avatar
Guolin Ke committed
236
  label_gain.shrink_to_fit();
Guolin Ke's avatar
Guolin Ke committed
237
238
239
240
}


void MetricConfig::Set(const std::unordered_map<std::string, std::string>& params) {
241
  GetDouble(params, "sigmoid", &sigmoid);
242
  GetInt(params, "num_class", &num_class);
Guolin Ke's avatar
Guolin Ke committed
243
244
  std::string tmp_str = "";
  if (GetString(params, "label_gain", &tmp_str)) {
Guolin Ke's avatar
Guolin Ke committed
245
    label_gain = Common::StringToArray<double>(tmp_str, ',');
Guolin Ke's avatar
Guolin Ke committed
246
247
248
  } else {
    // label_gain = 2^i - 1, may overflow, so we use 31 here
    const int max_label = 31;
249
    label_gain.push_back(0.0f);
Guolin Ke's avatar
Guolin Ke committed
250
    for (int i = 1; i < max_label; ++i) {
251
      label_gain.push_back(static_cast<double>((1 << i) - 1));
Guolin Ke's avatar
Guolin Ke committed
252
253
    }
  }
Guolin Ke's avatar
Guolin Ke committed
254
  label_gain.shrink_to_fit();
Guolin Ke's avatar
Guolin Ke committed
255
  if (GetString(params, "ndcg_eval_at", &tmp_str)) {
Guolin Ke's avatar
Guolin Ke committed
256
    eval_at = Common::StringToArray<int>(tmp_str, ',');
Guolin Ke's avatar
Guolin Ke committed
257
258
259
260
261
262
263
264
265
266
    std::sort(eval_at.begin(), eval_at.end());
    for (size_t i = 0; i < eval_at.size(); ++i) {
      CHECK(eval_at[i] > 0);
    }
  } else {
    // default eval ndcg @[1-5]
    for (int i = 1; i <= 5; ++i) {
      eval_at.push_back(i);
    }
  }
Guolin Ke's avatar
Guolin Ke committed
267
  eval_at.shrink_to_fit();
Guolin Ke's avatar
Guolin Ke committed
268
269
270
271
272
}


void TreeConfig::Set(const std::unordered_map<std::string, std::string>& params) {
  GetInt(params, "min_data_in_leaf", &min_data_in_leaf);
273
  GetDouble(params, "min_sum_hessian_in_leaf", &min_sum_hessian_in_leaf);
274
  CHECK(min_sum_hessian_in_leaf > 1.0f || min_data_in_leaf > 0);
275
276
  GetDouble(params, "lambda_l1", &lambda_l1);
  CHECK(lambda_l1 >= 0.0f)
277
    GetDouble(params, "lambda_l2", &lambda_l2);
278
  CHECK(lambda_l2 >= 0.0f)
279
    GetDouble(params, "min_gain_to_split", &min_gain_to_split);
280
  CHECK(min_gain_to_split >= 0.0f)
281
    GetInt(params, "num_leaves", &num_leaves);
282
  CHECK(num_leaves > 1);
Guolin Ke's avatar
Guolin Ke committed
283
  GetInt(params, "feature_fraction_seed", &feature_fraction_seed);
284
  GetDouble(params, "feature_fraction", &feature_fraction);
285
  CHECK(feature_fraction > 0.0f && feature_fraction <= 1.0f);
286
  GetDouble(params, "histogram_pool_size", &histogram_pool_size);
Guolin Ke's avatar
Guolin Ke committed
287
  GetInt(params, "max_depth", &max_depth);
Guolin Ke's avatar
Guolin Ke committed
288
  GetInt(params, "top_k", &top_k);
Guolin Ke's avatar
Guolin Ke committed
289
  CHECK(max_depth > 1 || max_depth < 0);
Guolin Ke's avatar
Guolin Ke committed
290
291
292
293
294
}


void BoostingConfig::Set(const std::unordered_map<std::string, std::string>& params) {
  GetInt(params, "num_iterations", &num_iterations);
Guolin Ke's avatar
Guolin Ke committed
295
  GetDouble(params, "sigmoid", &sigmoid);
Guolin Ke's avatar
Guolin Ke committed
296
297
298
299
  CHECK(num_iterations >= 0);
  GetInt(params, "bagging_seed", &bagging_seed);
  GetInt(params, "bagging_freq", &bagging_freq);
  CHECK(bagging_freq >= 0);
300
  GetDouble(params, "bagging_fraction", &bagging_fraction);
301
  CHECK(bagging_fraction > 0.0f && bagging_fraction <= 1.0f);
302
  GetDouble(params, "learning_rate", &learning_rate);
303
  CHECK(learning_rate > 0.0f);
wxchan's avatar
wxchan committed
304
305
  GetInt(params, "early_stopping_round", &early_stopping_round);
  CHECK(early_stopping_round >= 0);
306
307
308
  GetInt(params, "metric_freq", &output_freq);
  CHECK(output_freq >= 0);
  GetBool(params, "is_training_metric", &is_provide_training_metric);
309
  GetInt(params, "num_class", &num_class);
Guolin Ke's avatar
Guolin Ke committed
310
  GetInt(params, "drop_seed", &drop_seed);
311
  GetDouble(params, "drop_rate", &drop_rate);
312
313
314
315
  GetDouble(params, "skip_drop", &skip_drop);
  GetInt(params, "max_drop", &max_drop);
  GetBool(params, "xgboost_dart_mode", &xgboost_dart_mode);
  GetBool(params, "uniform_drop", &uniform_drop);
316
  CHECK(drop_rate <= 1.0 && drop_rate >= 0.0);
317
  CHECK(skip_drop <= 1.0 && skip_drop >= 0.0);
Guolin Ke's avatar
Guolin Ke committed
318
319
  GetTreeLearnerType(params);
  tree_config.Set(params);
Guolin Ke's avatar
Guolin Ke committed
320
321
}

Guolin Ke's avatar
Guolin Ke committed
322
void BoostingConfig::GetTreeLearnerType(const std::unordered_map<std::string, std::string>& params) {
Guolin Ke's avatar
Guolin Ke committed
323
324
  std::string value;
  if (GetString(params, "tree_learner", &value)) {
Guolin Ke's avatar
Guolin Ke committed
325
    std::transform(value.begin(), value.end(), value.begin(), Common::tolower);
Guolin Ke's avatar
Guolin Ke committed
326
327
328
329
330
331
    if (value == std::string("serial")) {
      tree_learner_type = TreeLearnerType::kSerialTreeLearner;
    } else if (value == std::string("feature") || value == std::string("feature_parallel")) {
      tree_learner_type = TreeLearnerType::kFeatureParallelTreelearner;
    } else if (value == std::string("data") || value == std::string("data_parallel")) {
      tree_learner_type = TreeLearnerType::kDataParallelTreeLearner;
Guolin Ke's avatar
Guolin Ke committed
332
333
334
    } else if (value == std::string("voting") || value == std::string("voting_parallel")) {
      tree_learner_type = TreeLearnerType::KVotingParallelTreeLearner;
    } else {
335
      Log::Fatal("Unknown tree learner type %s", value.c_str());
Guolin Ke's avatar
Guolin Ke committed
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
    }
  }
}

void NetworkConfig::Set(const std::unordered_map<std::string, std::string>& params) {
  GetInt(params, "num_machines", &num_machines);
  CHECK(num_machines >= 1);
  GetInt(params, "local_listen_port", &local_listen_port);
  CHECK(local_listen_port > 0);
  GetInt(params, "time_out", &time_out);
  CHECK(time_out > 0);
  GetString(params, "machine_list_file", &machine_list_filename);
}

}  // namespace LightGBM