tree.cpp 16.2 KB
Newer Older
Guolin Ke's avatar
Guolin Ke committed
1
2
3
4
5
6
7
8
9
10
11
12
#include <LightGBM/tree.h>

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

#include <LightGBM/dataset.h>

#include <sstream>
#include <unordered_map>
#include <functional>
#include <vector>
#include <string>
Guolin Ke's avatar
Guolin Ke committed
13
#include <memory>
14
#include <iomanip>
Guolin Ke's avatar
Guolin Ke committed
15
16
17
18
19
20

namespace LightGBM {

Tree::Tree(int max_leaves)
  :max_leaves_(max_leaves) {

Guolin Ke's avatar
Guolin Ke committed
21
22
23
24
25
26
  left_child_.resize(max_leaves_ - 1);
  right_child_.resize(max_leaves_ - 1);
  split_feature_inner_.resize(max_leaves_ - 1);
  split_feature_.resize(max_leaves_ - 1);
  threshold_in_bin_.resize(max_leaves_ - 1);
  threshold_.resize(max_leaves_ - 1);
Guolin Ke's avatar
Guolin Ke committed
27
  decision_type_.resize(max_leaves_ - 1, 0);
Guolin Ke's avatar
Guolin Ke committed
28
29
30
31
32
33
34
  split_gain_.resize(max_leaves_ - 1);
  leaf_parent_.resize(max_leaves_);
  leaf_value_.resize(max_leaves_);
  leaf_count_.resize(max_leaves_);
  internal_value_.resize(max_leaves_ - 1);
  internal_count_.resize(max_leaves_ - 1);
  leaf_depth_.resize(max_leaves_);
Guolin Ke's avatar
Guolin Ke committed
35
36
  // root is in the depth 0
  leaf_depth_[0] = 0;
Guolin Ke's avatar
Guolin Ke committed
37
  num_leaves_ = 1;
38
  leaf_value_[0] = 0.0f;
Guolin Ke's avatar
Guolin Ke committed
39
  leaf_parent_[0] = -1;
Guolin Ke's avatar
Guolin Ke committed
40
  shrinkage_ = 1.0f;
41
  num_cat_ = 0;
Guolin Ke's avatar
Guolin Ke committed
42
}
Guolin Ke's avatar
Guolin Ke committed
43

Guolin Ke's avatar
Guolin Ke committed
44
Tree::~Tree() {
Guolin Ke's avatar
Guolin Ke committed
45

Guolin Ke's avatar
Guolin Ke committed
46
47
}

48
49
50
51
int Tree::Split(int leaf, int feature, int real_feature, uint32_t threshold_bin,
                double threshold_double, double left_value, double right_value,
                data_size_t left_cnt, data_size_t right_cnt, double gain, MissingType missing_type, bool default_left) {
  Split(leaf, feature, real_feature, left_value, right_value, left_cnt, right_cnt, gain);
Guolin Ke's avatar
Guolin Ke committed
52
  int new_node_idx = num_leaves_ - 1;
Guolin Ke's avatar
Guolin Ke committed
53
  decision_type_[new_node_idx] = 0;
54
  SetDecisionType(&decision_type_[new_node_idx], false, kCategoricalMask);
Guolin Ke's avatar
Guolin Ke committed
55
56
57
58
59
60
61
  SetDecisionType(&decision_type_[new_node_idx], default_left, kDefaultLeftMask);
  if (missing_type == MissingType::None) {
    SetMissingType(&decision_type_[new_node_idx], 0);
  } else if (missing_type == MissingType::Zero) {
    SetMissingType(&decision_type_[new_node_idx], 1);
  } else if (missing_type == MissingType::NaN) {
    SetMissingType(&decision_type_[new_node_idx], 2);
62
  }
Guolin Ke's avatar
Guolin Ke committed
63
  threshold_in_bin_[new_node_idx] = threshold_bin;
64
  threshold_[new_node_idx] = Common::AvoidInf(threshold_double);
65
66
67
  ++num_leaves_;
  return num_leaves_ - 1;
}
Guolin Ke's avatar
Guolin Ke committed
68

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
int Tree::SplitCategorical(int leaf, int feature, int real_feature, uint32_t threshold_bin,
                           double threshold, double left_value, double right_value,
                           data_size_t left_cnt, data_size_t right_cnt, double gain, MissingType missing_type) {
  Split(leaf, feature, real_feature, left_value, right_value, left_cnt, right_cnt, gain);
  int new_node_idx = num_leaves_ - 1;
  decision_type_[new_node_idx] = 0;
  SetDecisionType(&decision_type_[new_node_idx], true, kCategoricalMask);
  if (missing_type == MissingType::None) {
    SetMissingType(&decision_type_[new_node_idx], 0);
  } else if (missing_type == MissingType::Zero) {
    SetMissingType(&decision_type_[new_node_idx], 1);
  } else if (missing_type == MissingType::NaN) {
    SetMissingType(&decision_type_[new_node_idx], 2);
  }
  threshold_in_bin_[new_node_idx] = threshold_bin;
  threshold_[new_node_idx] = threshold;
  ++num_cat_;
Guolin Ke's avatar
Guolin Ke committed
86
87
88
89
  ++num_leaves_;
  return num_leaves_ - 1;
}

90
91
92
93
94
95
96
97
98
99
100
101
102
103
#define PredictionFun(niter, fidx_in_iter, start_pos, decision_fun, iter_idx, data_idx) \
std::vector<std::unique_ptr<BinIterator>> iter((niter)); \
for (int i = 0; i < (niter); ++i) { \
  iter[i].reset(data->FeatureIterator((fidx_in_iter))); \
  iter[i]->Reset((start_pos)); \
}\
for (data_size_t i = start; i < end; ++i) {\
  int node = 0;\
  while (node >= 0) {\
    node = decision_fun(iter[(iter_idx)]->Get((data_idx)), node, default_bins[node], max_bins[node]);\
  }\
  score[(data_idx)] += static_cast<double>(leaf_value_[~node]);\
}\

104
void Tree::AddPredictionToScore(const Dataset* data, data_size_t num_data, double* score) const {
Guolin Ke's avatar
Guolin Ke committed
105
106
107
108
109
110
111
112
113
  if (num_leaves_ <= 1) {
    if (leaf_value_[0] != 0.0f) {
      #pragma omp parallel for schedule(static)
      for (data_size_t i = 0; i < num_data; ++i) {
        score[i] += leaf_value_[0];
      }
    }
    return;
  }
Guolin Ke's avatar
Guolin Ke committed
114
115
116
117
118
119
120
121
  std::vector<uint32_t> default_bins(num_leaves_ - 1);
  std::vector<uint32_t> max_bins(num_leaves_ - 1);
  for (int i = 0; i < num_leaves_ - 1; ++i) {
    const int fidx = split_feature_inner_[i];
    auto bin_mapper = data->FeatureBinMapper(fidx);
    default_bins[i] = bin_mapper->GetDefaultBin();
    max_bins[i] = bin_mapper->num_bin() - 1;
  }
122
  if (num_cat_ > 0) {
123
    if (data->num_features() > num_leaves_ - 1) {
124
125
126
      Threading::For<data_size_t>(0, num_data, [this, &data, score, &default_bins, &max_bins]
      (int, data_size_t start, data_size_t end) {
        PredictionFun(num_leaves_ - 1, split_feature_inner_[i], start, DecisionInner, node, i);
127
128
      });
    } else {
129
130
131
      Threading::For<data_size_t>(0, num_data, [this, &data, score, &default_bins, &max_bins]
      (int, data_size_t start, data_size_t end) {
        PredictionFun(data->num_features(), i, start, DecisionInner, split_feature_inner_[node], i);
132
133
      });
    }
Guolin Ke's avatar
Guolin Ke committed
134
  } else {
135
    if (data->num_features() > num_leaves_ - 1) {
136
137
138
      Threading::For<data_size_t>(0, num_data, [this, &data, score, &default_bins, &max_bins]
      (int, data_size_t start, data_size_t end) {
        PredictionFun(num_leaves_ - 1, split_feature_inner_[i], start, NumericalDecisionInner, node, i);
139
140
      });
    } else {
141
142
143
      Threading::For<data_size_t>(0, num_data, [this, &data, score, &default_bins, &max_bins]
      (int, data_size_t start, data_size_t end) {
        PredictionFun(data->num_features(), i, start, NumericalDecisionInner, split_feature_inner_[node], i);
144
145
      });
    }
Guolin Ke's avatar
Guolin Ke committed
146
  }
Guolin Ke's avatar
Guolin Ke committed
147
148
}

Guolin Ke's avatar
Guolin Ke committed
149
150
151
void Tree::AddPredictionToScore(const Dataset* data,
  const data_size_t* used_data_indices,
  data_size_t num_data, double* score) const {
Guolin Ke's avatar
Guolin Ke committed
152
153
154
155
156
157
158
159
160
  if (num_leaves_ <= 1) { 
    if (leaf_value_[0] != 0.0f) {
      #pragma omp parallel for schedule(static)
      for (data_size_t i = 0; i < num_data; ++i) {
        score[used_data_indices[i]] += leaf_value_[0];
      }
    }
    return; 
  }
Guolin Ke's avatar
Guolin Ke committed
161
162
163
164
165
166
167
168
  std::vector<uint32_t> default_bins(num_leaves_ - 1);
  std::vector<uint32_t> max_bins(num_leaves_ - 1);
  for (int i = 0; i < num_leaves_ - 1; ++i) {
    const int fidx = split_feature_inner_[i];
    auto bin_mapper = data->FeatureBinMapper(fidx);
    default_bins[i] = bin_mapper->GetDefaultBin();
    max_bins[i] = bin_mapper->num_bin() - 1;
  }
169
  if (num_cat_ > 0) {
170
    if (data->num_features() > num_leaves_ - 1) {
171
172
173
      Threading::For<data_size_t>(0, num_data, [this, &data, score, used_data_indices, &default_bins, &max_bins]
      (int, data_size_t start, data_size_t end) {
        PredictionFun(num_leaves_ - 1, split_feature_inner_[i], used_data_indices[start], DecisionInner, node, used_data_indices[i]);
174
175
      });
    } else {
176
177
178
      Threading::For<data_size_t>(0, num_data, [this, &data, score, used_data_indices, &default_bins, &max_bins]
      (int, data_size_t start, data_size_t end) {
        PredictionFun(data->num_features(), i, used_data_indices[start], DecisionInner, split_feature_inner_[node], used_data_indices[i]);
179
180
      });
    }
Guolin Ke's avatar
Guolin Ke committed
181
  } else {
182
    if (data->num_features() > num_leaves_ - 1) {
183
184
185
      Threading::For<data_size_t>(0, num_data, [this, &data, score, used_data_indices, &default_bins, &max_bins]
      (int, data_size_t start, data_size_t end) {
        PredictionFun(num_leaves_ - 1, split_feature_inner_[i], used_data_indices[start], NumericalDecisionInner, node, used_data_indices[i]);
186
187
      });
    } else {
188
189
190
      Threading::For<data_size_t>(0, num_data, [this, &data, score, used_data_indices, &default_bins, &max_bins]
      (int, data_size_t start, data_size_t end) {
        PredictionFun(data->num_features(), i, used_data_indices[start], NumericalDecisionInner, split_feature_inner_[node], used_data_indices[i]);
191
192
      });
    }
Guolin Ke's avatar
Guolin Ke committed
193
  }
Guolin Ke's avatar
Guolin Ke committed
194
195
}

196
197
#undef PredictionFun

Guolin Ke's avatar
Guolin Ke committed
198
std::string Tree::ToString() {
199
200
  std::stringstream str_buf;
  str_buf << "num_leaves=" << num_leaves_ << std::endl;
201
  str_buf << "num_cat=" << num_cat_ << std::endl;
202
  str_buf << "split_feature="
Guolin Ke's avatar
Guolin Ke committed
203
    << Common::ArrayToString<int>(split_feature_, num_leaves_ - 1, ' ') << std::endl;
204
  str_buf << "split_gain="
Guolin Ke's avatar
Guolin Ke committed
205
    << Common::ArrayToString<double>(split_gain_, num_leaves_ - 1, ' ') << std::endl;
206
  str_buf << "threshold="
Guolin Ke's avatar
Guolin Ke committed
207
    << Common::ArrayToString<double>(threshold_, num_leaves_ - 1, ' ') << std::endl;
208
209
  str_buf << "decision_type="
    << Common::ArrayToString<int>(Common::ArrayCast<int8_t, int>(decision_type_), num_leaves_ - 1, ' ') << std::endl;
210
  str_buf << "left_child="
Guolin Ke's avatar
Guolin Ke committed
211
    << Common::ArrayToString<int>(left_child_, num_leaves_ - 1, ' ') << std::endl;
212
  str_buf << "right_child="
Guolin Ke's avatar
Guolin Ke committed
213
    << Common::ArrayToString<int>(right_child_, num_leaves_ - 1, ' ') << std::endl;
214
  str_buf << "leaf_value="
Guolin Ke's avatar
Guolin Ke committed
215
    << Common::ArrayToString<double>(leaf_value_, num_leaves_, ' ') << std::endl;
216
  str_buf << "leaf_count="
Guolin Ke's avatar
Guolin Ke committed
217
    << Common::ArrayToString<data_size_t>(leaf_count_, num_leaves_, ' ') << std::endl;
218
  str_buf << "internal_value="
Guolin Ke's avatar
Guolin Ke committed
219
    << Common::ArrayToString<double>(internal_value_, num_leaves_ - 1, ' ') << std::endl;
220
  str_buf << "internal_count="
Guolin Ke's avatar
Guolin Ke committed
221
    << Common::ArrayToString<data_size_t>(internal_count_, num_leaves_ - 1, ' ') << std::endl;
Guolin Ke's avatar
Guolin Ke committed
222
  str_buf << "shrinkage=" << shrinkage_ << std::endl;
223
224
  str_buf << std::endl;
  return str_buf.str();
Guolin Ke's avatar
Guolin Ke committed
225
226
}

wxchan's avatar
wxchan committed
227
std::string Tree::ToJSON() {
228
  std::stringstream str_buf;
229
  str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 2);
230
  str_buf << "\"num_leaves\":" << num_leaves_ << "," << std::endl;
231
  str_buf << "\"num_cat\":" << num_cat_ << "," << std::endl;
Guolin Ke's avatar
Guolin Ke committed
232
  str_buf << "\"shrinkage\":" << shrinkage_ << "," << std::endl;
wxchan's avatar
wxchan committed
233
  if (num_leaves_ == 1) {
234
    str_buf << "\"tree_structure\":{" << "\"leaf_value\":" << leaf_value_[0] << "}"  << std::endl;
wxchan's avatar
wxchan committed
235
236
237
  } else {
    str_buf << "\"tree_structure\":" << NodeToJSON(0) << std::endl;
  }
wxchan's avatar
wxchan committed
238

239
  return str_buf.str();
wxchan's avatar
wxchan committed
240
241
242
}

std::string Tree::NodeToJSON(int index) {
243
  std::stringstream str_buf;
244
  str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 2);
wxchan's avatar
wxchan committed
245
246
  if (index >= 0) {
    // non-leaf
247
248
    str_buf << "{" << std::endl;
    str_buf << "\"split_index\":" << index << "," << std::endl;
Guolin Ke's avatar
Guolin Ke committed
249
    str_buf << "\"split_feature\":" << split_feature_[index] << "," << std::endl;
250
    str_buf << "\"split_gain\":" << split_gain_[index] << "," << std::endl;
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
    if (GetDecisionType(decision_type_[index], kCategoricalMask)) {
      str_buf << "\"threshold\":" << static_cast<int>(threshold_[index]) << "," << std::endl;
      str_buf << "\"decision_type\":\"==\"," << std::endl;
    } else {
      str_buf << "\"threshold\":" << Common::AvoidInf(threshold_[index]) << "," << std::endl;
      str_buf << "\"decision_type\":\"<=\"," << std::endl;
    }
    if (GetDecisionType(decision_type_[index], kDefaultLeftMask)) {
      str_buf << "\"default_left\":true," << std::endl;
    } else {
      str_buf << "\"default_left\":false," << std::endl;
    }
    uint8_t missing_type = GetMissingType(decision_type_[index]);
    if (missing_type == 0) {
      str_buf << "\"missing_type\":\"None\"," << std::endl;
    } else if (missing_type == 1) {
      str_buf << "\"missing_type\":\"Zero\"," << std::endl;
    } else {
      str_buf << "\"missing_type\":\"NaN\"," << std::endl;
    }
271
272
273
274
275
    str_buf << "\"internal_value\":" << internal_value_[index] << "," << std::endl;
    str_buf << "\"internal_count\":" << internal_count_[index] << "," << std::endl;
    str_buf << "\"left_child\":" << NodeToJSON(left_child_[index]) << "," << std::endl;
    str_buf << "\"right_child\":" << NodeToJSON(right_child_[index]) << std::endl;
    str_buf << "}";
wxchan's avatar
wxchan committed
276
277
278
  } else {
    // leaf
    index = ~index;
279
280
281
282
283
    str_buf << "{" << std::endl;
    str_buf << "\"leaf_index\":" << index << "," << std::endl;
    str_buf << "\"leaf_value\":" << leaf_value_[index] << "," << std::endl;
    str_buf << "\"leaf_count\":" << leaf_count_[index] << std::endl;
    str_buf << "}";
wxchan's avatar
wxchan committed
284
285
  }

286
  return str_buf.str();
wxchan's avatar
wxchan committed
287
288
}

289
290
291
292
293
294
295
std::string Tree::ToIfElse(int index, bool is_predict_leaf_index) {
  std::stringstream str_buf;
  str_buf << "double PredictTree" << index;
  if (is_predict_leaf_index) {
    str_buf << "Leaf";
  }
  str_buf << "(const double* arr) { ";
296
297
  if (num_leaves_ <= 1) {
    str_buf << "return " << leaf_value_[0] << ";";
298
  } else {
299
300
301
302
303
    // use this for the missing value conversion
    str_buf << "double fval = 0.0f; ";
    if (num_cat_ > 0) {
      str_buf << "int int_fval = 0; ";
    }
304
305
306
307
308
309
310
311
312
313
314
    str_buf << NodeToIfElse(0, is_predict_leaf_index);
  }
  str_buf << " }" << std::endl;
  return str_buf.str();
}

std::string Tree::NodeToIfElse(int index, bool is_predict_leaf_index) {
  std::stringstream str_buf;
  str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 2);
  if (index >= 0) {
    // non-leaf
315
    str_buf << "fval = arr[" << split_feature_[index] << "];";
Guolin Ke's avatar
Guolin Ke committed
316
    if (GetDecisionType(decision_type_[index], kCategoricalMask) == 0) {
317
      str_buf << NumericalDecisionIfElse(index);
318
    } else {
319
      str_buf << CategoricalDecisionIfElse(index);
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
    }
    // left subtree
    str_buf << NodeToIfElse(left_child_[index], is_predict_leaf_index);
    str_buf << " } else { ";
    // right subtree
    str_buf << NodeToIfElse(right_child_[index], is_predict_leaf_index);
    str_buf << " }";
  } else {
    // leaf
    str_buf << "return ";
    if (is_predict_leaf_index) {
      str_buf << ~index;
    } else {
      str_buf << leaf_value_[~index];
    }
    str_buf << ";";
  }

  return str_buf.str();
}

Guolin Ke's avatar
Guolin Ke committed
341
Tree::Tree(const std::string& str) {
Guolin Ke's avatar
Guolin Ke committed
342
  std::vector<std::string> lines = Common::SplitLines(str.c_str());
Guolin Ke's avatar
Guolin Ke committed
343
344
345
346
347
348
349
350
351
352
353
  std::unordered_map<std::string, std::string> key_vals;
  for (const std::string& line : lines) {
    std::vector<std::string> tmp_strs = Common::Split(line.c_str(), '=');
    if (tmp_strs.size() == 2) {
      std::string key = Common::Trim(tmp_strs[0]);
      std::string val = Common::Trim(tmp_strs[1]);
      if (key.size() > 0 && val.size() > 0) {
        key_vals[key] = val;
      }
    }
  }
354
  if (key_vals.count("num_leaves") <= 0) {
Guolin Ke's avatar
Guolin Ke committed
355
    Log::Fatal("Tree model should contain num_leaves field.");
Guolin Ke's avatar
Guolin Ke committed
356
357
358
359
  }

  Common::Atoi(key_vals["num_leaves"].c_str(), &num_leaves_);

360
361
362
363
364
365
  if (key_vals.count("num_cat") <= 0) {
    Log::Fatal("Tree model should contain num_cat field.");
  }

  Common::Atoi(key_vals["num_cat"].c_str(), &num_cat_);

366
367
368
369
370
371
  if (key_vals.count("leaf_value")) {
    leaf_value_ = Common::StringToArray<double>(key_vals["leaf_value"], ' ', num_leaves_);
  } else {
    Log::Fatal("Tree model string format error, should contain leaf_value field");
  }

372
373
  if (num_leaves_ <= 1) { return; }

Guolin Ke's avatar
Guolin Ke committed
374
375
376
377
  if (key_vals.count("left_child")) {
    left_child_ = Common::StringToArray<int>(key_vals["left_child"], ' ', num_leaves_ - 1);
  } else {
    Log::Fatal("Tree model string format error, should contain left_child field");
378
379
  }

Guolin Ke's avatar
Guolin Ke committed
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
  if (key_vals.count("right_child")) {
    right_child_ = Common::StringToArray<int>(key_vals["right_child"], ' ', num_leaves_ - 1);
  } else {
    Log::Fatal("Tree model string format error, should contain right_child field");
  }

  if (key_vals.count("split_feature")) {
    split_feature_ = Common::StringToArray<int>(key_vals["split_feature"], ' ', num_leaves_ - 1);
  } else {
    Log::Fatal("Tree model string format error, should contain split_feature field");
  }

  if (key_vals.count("threshold")) {
    threshold_ = Common::StringToArray<double>(key_vals["threshold"], ' ', num_leaves_ - 1);
  } else {
    Log::Fatal("Tree model string format error, should contain threshold field");
  }

  if (key_vals.count("split_gain")) {
    split_gain_ = Common::StringToArray<double>(key_vals["split_gain"], ' ', num_leaves_ - 1);
  } else {
    split_gain_.resize(num_leaves_ - 1);
  }

  if (key_vals.count("internal_count")) {
    internal_count_ = Common::StringToArray<data_size_t>(key_vals["internal_count"], ' ', num_leaves_ - 1);
  } else {
    internal_count_.resize(num_leaves_ - 1);
  }

  if (key_vals.count("internal_value")) {
    internal_value_ = Common::StringToArray<double>(key_vals["internal_value"], ' ', num_leaves_ - 1);
  } else {
    internal_value_.resize(num_leaves_ - 1);
  }

  if (key_vals.count("leaf_count")) {
    leaf_count_ = Common::StringToArray<data_size_t>(key_vals["leaf_count"], ' ', num_leaves_);
  } else {
    leaf_count_.resize(num_leaves_);
  }

  if (key_vals.count("decision_type")) {
    decision_type_ = Common::StringToArray<int8_t>(key_vals["decision_type"], ' ', num_leaves_ - 1);
  } else {
    decision_type_ = std::vector<int8_t>(num_leaves_ - 1, 0);
  }

  if (key_vals.count("shrinkage")) {
    Common::Atof(key_vals["shrinkage"].c_str(), &shrinkage_);
  } else {
    shrinkage_ = 1.0f;
  }
Guolin Ke's avatar
Guolin Ke committed
433
434
435
}

}  // namespace LightGBM