Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix load parameter #1495

Merged
merged 1 commit into from
Jul 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/boosting/gbdt.h
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,10 @@ class GBDT : public GBDTBase {
std::unique_ptr<ObjectiveFunction> loaded_objective_;
bool average_output_;
bool need_re_bagging_;
std::string loaded_parameter_;

Json forced_splits_json_;

};

} // namespace LightGBM
Expand Down
29 changes: 27 additions & 2 deletions src/boosting/gbdt_model_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ std::string GBDT::SaveModelToString(int num_iteration) const {
ss << tree_strs[i];
tree_strs[i].clear();
}
ss << "end of trees" << "\n";

std::vector<double> feature_importances = FeatureImportance(num_iteration, 0);
// store the importance first
Expand All @@ -301,8 +302,13 @@ std::string GBDT::SaveModelToString(int num_iteration) const {
ss << pairs[i].second << "=" << std::to_string(pairs[i].first) << '\n';
}
if (config_ != nullptr) {
ss << "parameters:" << '\n';
ss << "\nparameters:" << '\n';
ss << config_->ToString() << "\n";
ss << "end of parameters" << '\n';
} else if (!loaded_parameter_.empty()) {
ss << "\nparameters:" << '\n';
ss << loaded_parameter_ << "\n";
ss << "end of parameters" << '\n';
}
return ss.str();
}
Expand Down Expand Up @@ -465,7 +471,26 @@ bool GBDT::LoadModelFromString(const char* buffer, size_t len) {
num_iteration_for_pred_ = static_cast<int>(models_.size()) / num_tree_per_iteration_;
num_init_iteration_ = num_iteration_for_pred_;
iter_ = 0;

bool is_inparameter = false;
std::stringstream ss;
while (p < end) {
auto line_len = Common::GetLine(p);
std::string cur_line(p, line_len);
if (line_len > 0) {
if (cur_line == std::string("parameters:")) {
is_inparameter = true;
} else if (cur_line == std::string("end of parameters")) {
break;
} else if (is_inparameter) {
ss << cur_line << "\n";
}
}
p += line_len;
p = Common::SkipNewLine(p);
}
if (!ss.str().empty()) {
loaded_parameter_ = ss.str();
}
return true;
}

Expand Down