config.cpp 10.3 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
#include <LightGBM/config.h>

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

Guolin Ke's avatar
Guolin Ke committed
7
#include <limits>
Guolin Ke's avatar
Guolin Ke committed
8
9
10

namespace LightGBM {

Guolin Ke's avatar
Guolin Ke committed
11
void Config::KV2Map(std::unordered_map<std::string, std::string>& params, const char* kv) {
wxchan's avatar
wxchan committed
12
13
14
15
16
17
  std::vector<std::string> tmp_strs = Common::Split(kv, '=');
  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) {
      auto value_search = params.find(key);
18
      if (value_search == params.end()) {  // not set
wxchan's avatar
wxchan committed
19
20
        params.emplace(key, value);
      } else {
21
        Log::Warning("%s is set=%s, %s=%s will be ignored. Current value: %s=%s",
wxchan's avatar
wxchan committed
22
23
24
25
26
27
28
29
30
          key.c_str(), value_search->second.c_str(), key.c_str(), value.c_str(),
          key.c_str(), value_search->second.c_str());
      }
    }
  } else {
    Log::Warning("Unknown parameter %s", kv);
  }
}

Guolin Ke's avatar
Guolin Ke committed
31
std::unordered_map<std::string, std::string> Config::Str2Map(const char* parameters) {
32
  std::unordered_map<std::string, std::string> params;
33
  auto args = Common::Split(parameters, " \t\n\r");
34
  for (auto arg : args) {
wxchan's avatar
wxchan committed
35
    KV2Map(params, Common::Trim(arg).c_str());
36
37
  }
  ParameterAlias::KeyAliasTransform(&params);
38
  return params;
39
40
}

Guolin Ke's avatar
Guolin Ke committed
41
void GetBoostingType(const std::unordered_map<std::string, std::string>& params, std::string* boosting) {
Guolin Ke's avatar
Guolin Ke committed
42
  std::string value;
Guolin Ke's avatar
Guolin Ke committed
43
  if (Config::GetString(params, "boosting", &value)) {
Guolin Ke's avatar
Guolin Ke committed
44
    std::transform(value.begin(), value.end(), value.begin(), Common::tolower);
Guolin Ke's avatar
Guolin Ke committed
45
    if (value == std::string("gbdt") || value == std::string("gbrt")) {
Guolin Ke's avatar
Guolin Ke committed
46
      *boosting = "gbdt";
47
    } else if (value == std::string("dart")) {
Guolin Ke's avatar
Guolin Ke committed
48
      *boosting = "dart";
Guolin Ke's avatar
Guolin Ke committed
49
    } else if (value == std::string("goss")) {
Guolin Ke's avatar
Guolin Ke committed
50
      *boosting = "goss";
51
    } else if (value == std::string("rf") || value == std::string("random_forest")) {
Guolin Ke's avatar
Guolin Ke committed
52
      *boosting = "rf";
Guolin Ke's avatar
Guolin Ke committed
53
    } else {
54
      Log::Fatal("Unknown boosting type %s", value.c_str());
Guolin Ke's avatar
Guolin Ke committed
55
56
57
58
    }
  }
}

Guolin Ke's avatar
Guolin Ke committed
59
void GetObjectiveType(const std::unordered_map<std::string, std::string>& params, std::string* objective) {
Guolin Ke's avatar
Guolin Ke committed
60
  std::string value;
Guolin Ke's avatar
Guolin Ke committed
61
  if (Config::GetString(params, "objective", &value)) {
Guolin Ke's avatar
Guolin Ke committed
62
    std::transform(value.begin(), value.end(), value.begin(), Common::tolower);
Guolin Ke's avatar
Guolin Ke committed
63
    *objective = value;
Guolin Ke's avatar
Guolin Ke committed
64
65
66
  }
}

Guolin Ke's avatar
Guolin Ke committed
67
void GetMetricType(const std::unordered_map<std::string, std::string>& params, std::vector<std::string>* metric) {
Guolin Ke's avatar
Guolin Ke committed
68
  std::string value;
Guolin Ke's avatar
Guolin Ke committed
69
  if (Config::GetString(params, "metric", &value)) {
Guolin Ke's avatar
Guolin Ke committed
70
    // clear old metrics
Guolin Ke's avatar
Guolin Ke committed
71
    metric->clear();
Guolin Ke's avatar
Guolin Ke committed
72
    // to lower
Guolin Ke's avatar
Guolin Ke committed
73
    std::transform(value.begin(), value.end(), value.begin(), Common::tolower);
Guolin Ke's avatar
Guolin Ke committed
74
75
    // split
    std::vector<std::string> metrics = Common::Split(value.c_str(), ',');
76
    // remove duplicate
Guolin Ke's avatar
Guolin Ke committed
77
    std::unordered_set<std::string> metric_sets;
Guolin Ke's avatar
Guolin Ke committed
78
79
80
81
    for (auto& met : metrics) {
      std::transform(met.begin(), met.end(), met.begin(), Common::tolower);
      if (metric_sets.count(met) <= 0) {
        metric_sets.insert(met);
Guolin Ke's avatar
Guolin Ke committed
82
83
      }
    }
Guolin Ke's avatar
Guolin Ke committed
84
85
    for (auto& met : metric_sets) {
      metric->push_back(met);
Guolin Ke's avatar
Guolin Ke committed
86
    }
Guolin Ke's avatar
Guolin Ke committed
87
    metric->shrink_to_fit();
Guolin Ke's avatar
Guolin Ke committed
88
  }
89
  // add names of objective function if not providing metric
Guolin Ke's avatar
Guolin Ke committed
90
91
  if (metric->empty() && value.size() == 0) {
    if (Config::GetString(params, "objective", &value)) {
92
      std::transform(value.begin(), value.end(), value.begin(), Common::tolower);
Guolin Ke's avatar
Guolin Ke committed
93
      metric->push_back(value);
94
95
    }
  }
Guolin Ke's avatar
Guolin Ke committed
96
97
}

Guolin Ke's avatar
Guolin Ke committed
98
void GetTaskType(const std::unordered_map<std::string, std::string>& params, TaskType* task) {
Guolin Ke's avatar
Guolin Ke committed
99
  std::string value;
Guolin Ke's avatar
Guolin Ke committed
100
  if (Config::GetString(params, "task", &value)) {
Guolin Ke's avatar
Guolin Ke committed
101
    std::transform(value.begin(), value.end(), value.begin(), Common::tolower);
Guolin Ke's avatar
Guolin Ke committed
102
    if (value == std::string("train") || value == std::string("training")) {
Guolin Ke's avatar
Guolin Ke committed
103
      *task = TaskType::kTrain;
Guolin Ke's avatar
Guolin Ke committed
104
    } else if (value == std::string("predict") || value == std::string("prediction")
Guolin Ke's avatar
Guolin Ke committed
105
               || value == std::string("test")) {
Guolin Ke's avatar
Guolin Ke committed
106
      *task = TaskType::kPredict;
107
    } else if (value == std::string("convert_model")) {
Guolin Ke's avatar
Guolin Ke committed
108
      *task = TaskType::kConvertModel;
109
    } else if (value == std::string("refit") || value == std::string("refit_tree")) {
Guolin Ke's avatar
Guolin Ke committed
110
      *task = TaskType::KRefitTree;
Guolin Ke's avatar
Guolin Ke committed
111
    } else {
112
      Log::Fatal("Unknown task type %s", value.c_str());
Guolin Ke's avatar
Guolin Ke committed
113
114
115
116
    }
  }
}

wxchan's avatar
wxchan committed
117
void GetDeviceType(const std::unordered_map<std::string, std::string>& params, std::string* device_type) {
Guolin Ke's avatar
Guolin Ke committed
118
  std::string value;
119
  if (Config::GetString(params, "device_type", &value)) {
Guolin Ke's avatar
Guolin Ke committed
120
121
    std::transform(value.begin(), value.end(), value.begin(), Common::tolower);
    if (value == std::string("cpu")) {
wxchan's avatar
wxchan committed
122
      *device_type = "cpu";
Guolin Ke's avatar
Guolin Ke committed
123
    } else if (value == std::string("gpu")) {
wxchan's avatar
wxchan committed
124
      *device_type = "gpu";
Guolin Ke's avatar
Guolin Ke committed
125
126
127
128
129
130
    } else {
      Log::Fatal("Unknown device type %s", value.c_str());
    }
  }
}

Guolin Ke's avatar
Guolin Ke committed
131
void GetTreeLearnerType(const std::unordered_map<std::string, std::string>& params, std::string* tree_learner) {
Guolin Ke's avatar
Guolin Ke committed
132
  std::string value;
Guolin Ke's avatar
Guolin Ke committed
133
  if (Config::GetString(params, "tree_learner", &value)) {
Guolin Ke's avatar
Guolin Ke committed
134
135
    std::transform(value.begin(), value.end(), value.begin(), Common::tolower);
    if (value == std::string("serial")) {
Guolin Ke's avatar
Guolin Ke committed
136
      *tree_learner = "serial";
Guolin Ke's avatar
Guolin Ke committed
137
    } else if (value == std::string("feature") || value == std::string("feature_parallel")) {
Guolin Ke's avatar
Guolin Ke committed
138
      *tree_learner = "feature";
Guolin Ke's avatar
Guolin Ke committed
139
    } else if (value == std::string("data") || value == std::string("data_parallel")) {
Guolin Ke's avatar
Guolin Ke committed
140
      *tree_learner = "data";
Guolin Ke's avatar
Guolin Ke committed
141
    } else if (value == std::string("voting") || value == std::string("voting_parallel")) {
Guolin Ke's avatar
Guolin Ke committed
142
      *tree_learner = "voting";
Guolin Ke's avatar
Guolin Ke committed
143
144
145
146
147
148
    } else {
      Log::Fatal("Unknown tree learner type %s", value.c_str());
    }
  }
}

Guolin Ke's avatar
Guolin Ke committed
149
void Config::Set(const std::unordered_map<std::string, std::string>& params) {
Guolin Ke's avatar
Guolin Ke committed
150
151
152
153
  // generate seeds by seed.
  if (GetInt(params, "seed", &seed)) {
    Random rand(seed);
    int int_max = std::numeric_limits<short>::max();
Guolin Ke's avatar
Guolin Ke committed
154
155
156
157
    data_random_seed = static_cast<int>(rand.NextShort(0, int_max));
    bagging_seed = static_cast<int>(rand.NextShort(0, int_max));
    drop_seed = static_cast<int>(rand.NextShort(0, int_max));
    feature_fraction_seed = static_cast<int>(rand.NextShort(0, int_max));
Guolin Ke's avatar
Guolin Ke committed
158
159
  }

Guolin Ke's avatar
Guolin Ke committed
160
161
162
163
164
165
  GetTaskType(params, &task);
  GetBoostingType(params, &boosting);
  GetMetricType(params, &metric);
  GetObjectiveType(params, &objective);
  GetDeviceType(params, &device_type);
  GetTreeLearnerType(params, &tree_learner);
Guolin Ke's avatar
Guolin Ke committed
166

Guolin Ke's avatar
Guolin Ke committed
167
  GetMembersFromString(params);
Guolin Ke's avatar
Guolin Ke committed
168

Guolin Ke's avatar
Guolin Ke committed
169
170
171
  if (valid_data_initscores.size() == 0 && valid.size() > 0) {
    valid_data_initscores = std::vector<std::string>(valid.size(), "");
  }
172
  CHECK(valid.size() == valid_data_initscores.size());
Guolin Ke's avatar
Guolin Ke committed
173
174
175

  // check for conflicts
  CheckParamConflict();
176

Guolin Ke's avatar
Guolin Ke committed
177
  if (verbosity == 1) {
Guolin Ke's avatar
Guolin Ke committed
178
    LightGBM::Log::ResetLogLevel(LightGBM::LogLevel::Info);
Guolin Ke's avatar
Guolin Ke committed
179
  } else if (verbosity == 0) {
Guolin Ke's avatar
Guolin Ke committed
180
    LightGBM::Log::ResetLogLevel(LightGBM::LogLevel::Warning);
Guolin Ke's avatar
Guolin Ke committed
181
  } else if (verbosity >= 2) {
Guolin Ke's avatar
Guolin Ke committed
182
183
184
185
186
187
    LightGBM::Log::ResetLogLevel(LightGBM::LogLevel::Debug);
  } else {
    LightGBM::Log::ResetLogLevel(LightGBM::LogLevel::Fatal);
  }
}

Guolin Ke's avatar
Guolin Ke committed
188
189
190
191
192
193
194
bool CheckMultiClassObjective(const std::string& objective) {
  return (objective == std::string("multiclass")
          || objective == std::string("multiclassova")
          || objective == std::string("softmax")
          || objective == std::string("multiclass_ova")
          || objective == std::string("ova")
          || objective == std::string("ovr"));
195
196
}

Guolin Ke's avatar
Guolin Ke committed
197
198
199
void Config::CheckParamConflict() {
  // check if objective, metric, and num_class match
  int num_class_check = num_class;
200
  bool objective_custom = objective == std::string("none") || objective == std::string("null")
201
                                       || objective == std::string("custom") || objective == std::string("na");
Guolin Ke's avatar
Guolin Ke committed
202
  bool objective_type_multiclass = CheckMultiClassObjective(objective) || (objective_custom && num_class_check > 1);
203

204
  if (objective_type_multiclass) {
Guolin Ke's avatar
Guolin Ke committed
205
206
    if (num_class_check <= 1) {
      Log::Fatal("Number of classes should be specified and greater than 1 for multiclass training");
207
208
    }
  } else {
Guolin Ke's avatar
Guolin Ke committed
209
    if (task == TaskType::kTrain && num_class_check != 1) {
210
211
      Log::Fatal("Number of classes must be 1 for non-multiclass training");
    }
212
  }
Guolin Ke's avatar
Guolin Ke committed
213
  for (std::string metric_type : metric) {
214
    bool metric_custom_or_none = metric_type == std::string("none") || metric_type == std::string("null")
215
216
217
218
219
                                 || metric_type == std::string("custom") || metric_type == std::string("na");
    bool metric_type_multiclass = (CheckMultiClassObjective(metric_type)
                                   || metric_type == std::string("multi_logloss")
                                   || metric_type == std::string("multi_error")
                                   || (metric_custom_or_none && num_class_check > 1));
Guolin Ke's avatar
Guolin Ke committed
220
    if ((objective_type_multiclass && !metric_type_multiclass)
221
222
        || (!objective_type_multiclass && metric_type_multiclass)) {
      Log::Fatal("Multiclass objective and metrics don't match");
223
    }
224
  }
225

Guolin Ke's avatar
Guolin Ke committed
226
  if (num_machines > 1) {
Guolin Ke's avatar
Guolin Ke committed
227
228
229
    is_parallel = true;
  } else {
    is_parallel = false;
Guolin Ke's avatar
Guolin Ke committed
230
    tree_learner = "serial";
Guolin Ke's avatar
Guolin Ke committed
231
232
  }

Guolin Ke's avatar
Guolin Ke committed
233
  bool is_single_tree_learner = tree_learner == std::string("serial");
Guolin Ke's avatar
Guolin Ke committed
234
235

  if (is_single_tree_learner) {
Guolin Ke's avatar
Guolin Ke committed
236
    is_parallel = false;
Guolin Ke's avatar
Guolin Ke committed
237
    num_machines = 1;
Guolin Ke's avatar
Guolin Ke committed
238
239
  }

Guolin Ke's avatar
Guolin Ke committed
240
  if (is_single_tree_learner || tree_learner == std::string("feature")) {
Guolin Ke's avatar
Guolin Ke committed
241
    is_parallel_find_bin = false;
Guolin Ke's avatar
Guolin Ke committed
242
243
  } else if (tree_learner == std::string("data")
             || tree_learner == std::string("voting")) {
Guolin Ke's avatar
Guolin Ke committed
244
    is_parallel_find_bin = true;
Guolin Ke's avatar
Guolin Ke committed
245
246
    if (histogram_pool_size >= 0
        && tree_learner == std::string("data")) {
247
248
      Log::Warning("Histogram LRU queue was enabled (histogram_pool_size=%f).\n"
                   "Will disable this to reduce communication costs",
Guolin Ke's avatar
Guolin Ke committed
249
                   histogram_pool_size);
tks's avatar
tks committed
250
      // Change pool size to -1 (no limit) when using data parallel to reduce communication costs
Guolin Ke's avatar
Guolin Ke committed
251
      histogram_pool_size = -1;
252
    }
Guolin Ke's avatar
Guolin Ke committed
253
  }
254
  // Check max_depth and num_leaves
Guolin Ke's avatar
Guolin Ke committed
255
256
  if (max_depth > 0) {
    int full_num_leaves = static_cast<int>(std::pow(2, max_depth));
257
    if (full_num_leaves > num_leaves
Guolin Ke's avatar
Guolin Ke committed
258
        && num_leaves == kDefaultNumLeaves) {
259
      Log::Warning("Accuracy may be bad since you didn't set num_leaves and 2^max_depth > num_leaves");
260
261
    }
  }
Guolin Ke's avatar
Guolin Ke committed
262
263
}

Guolin Ke's avatar
Guolin Ke committed
264
265
266
267
268
269
270
271
272
std::string Config::ToString() const {
  std::stringstream str_buf;
  str_buf << "[boosting: " << boosting << "]\n";
  str_buf << "[objective: " << objective << "]\n";
  str_buf << "[metric: " << Common::Join(metric, ",") << "]\n";
  str_buf << "[tree_learner: " << tree_learner << "]\n";
  str_buf << "[device_type: " << device_type << "]\n";
  str_buf << SaveMembersToString();
  return str_buf.str();
Guolin Ke's avatar
Guolin Ke committed
273
274
275
}

}  // namespace LightGBM