| 405 | } |
| 406 | |
| 407 | bool GBDT::LoadModelFromString(const char* buffer, size_t len) { |
| 408 | // use serialized string to restore this object |
| 409 | models_.clear(); |
| 410 | auto c_str = buffer; |
| 411 | auto p = c_str; |
| 412 | auto end = p + len; |
| 413 | std::unordered_map<std::string, std::string> key_vals; |
| 414 | while (p < end) { |
| 415 | auto line_len = Common::GetLine(p); |
| 416 | if (line_len > 0) { |
| 417 | std::string cur_line(p, line_len); |
| 418 | if (!Common::StartsWith(cur_line, "Tree=")) { |
| 419 | auto strs = Common::Split(cur_line.c_str(), '='); |
| 420 | if (strs.size() == 1) { |
| 421 | key_vals[strs[0]] = ""; |
| 422 | } else if (strs.size() == 2) { |
| 423 | key_vals[strs[0]] = strs[1]; |
| 424 | } else if (strs.size() > 2) { |
| 425 | if (strs[0] == "feature_names") { |
| 426 | key_vals[strs[0]] = cur_line.substr(std::strlen("feature_names=")); |
| 427 | } else if (strs[0] == "monotone_constraints") { |
| 428 | key_vals[strs[0]] = cur_line.substr(std::strlen("monotone_constraints=")); |
| 429 | } else { |
| 430 | // Use first 128 chars to avoid exceed the message buffer. |
| 431 | Log::Fatal("Wrong line at model file: %s", cur_line.substr(0, std::min<size_t>(128, cur_line.size())).c_str()); |
| 432 | } |
| 433 | } |
| 434 | } else { |
| 435 | break; |
| 436 | } |
| 437 | } |
| 438 | p += line_len; |
| 439 | p = Common::SkipNewLine(p); |
| 440 | } |
| 441 | |
| 442 | // get number of classes |
| 443 | if (key_vals.count("num_class")) { |
| 444 | Common::Atoi(key_vals["num_class"].c_str(), &num_class_); |
| 445 | } else { |
| 446 | Log::Fatal("Model file doesn't specify the number of classes"); |
| 447 | return false; |
| 448 | } |
| 449 | |
| 450 | if (key_vals.count("num_tree_per_iteration")) { |
| 451 | Common::Atoi(key_vals["num_tree_per_iteration"].c_str(), &num_tree_per_iteration_); |
| 452 | } else { |
| 453 | num_tree_per_iteration_ = num_class_; |
| 454 | } |
| 455 | |
| 456 | // get index of label |
| 457 | if (key_vals.count("label_index")) { |
| 458 | Common::Atoi(key_vals["label_index"].c_str(), &label_idx_); |
| 459 | } else { |
| 460 | Log::Fatal("Model file doesn't specify the label index"); |
| 461 | return false; |
| 462 | } |
| 463 | |
| 464 | // get max_feature_idx first |
no test coverage detected