Skip to content

Commit

Permalink
Use high precision conversion from double to string in Tree::ToString…
Browse files Browse the repository at this point in the history
…() for new linear tree members (#3938)

* Fix index out-of-range exception generated by BaggingHelper on small datasets.

Prior to this change, the line "score_t threshold = tmp_gradients[top_k - 1];" would generate an exception, since tmp_gradients would be empty when the cnt input value to the function is zero.

* Update goss.hpp

* Update goss.hpp

* Add API method LGBM_BoosterPredictForMats which runs prediction on a data set given as of array of pointers to rows (as opposed to existing method LGBM_BoosterPredictForMat which requires data given as contiguous array)

* Fix incorrect upstream merge

* Add link to LightGBM.NET

* Fix indenting to 2 spaces

* Dummy edit to trigger CI

* Dummy edit to trigger CI

* remove duplicate functions from merge

* In Tree::ToString() method, print double values for linear tree models with high precision, so that the tree may be accurately reproduced elsewhere (LightGBM.Net in particular)

* Need to use more precise StringToArray instead of StringToArrayFast when parsing double valued arrays for linear trees, to ensure models round-trip via string or file correctly.

Co-authored-by: matthew-peacock <[email protected]>
Co-authored-by: Guolin Ke <[email protected]>
  • Loading branch information
3 people authored Feb 19, 2021
1 parent 7880b79 commit 7f91dc6
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/io/tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ std::string Tree::ToString() const {

if (is_linear_) {
str_buf << "leaf_const="
<< ArrayToString(leaf_const_, num_leaves_) << '\n';
<< ArrayToString<true>(leaf_const_, num_leaves_) << '\n';
std::vector<int> num_feat(num_leaves_);
for (int i = 0; i < num_leaves_; ++i) {
num_feat[i] = static_cast<int>(leaf_coeff_[i].size());
Expand All @@ -394,7 +394,7 @@ std::string Tree::ToString() const {
str_buf << "leaf_coeff=";
for (int i = 0; i < num_leaves_; ++i) {
if (num_feat[i] > 0) {
str_buf << ArrayToString(leaf_coeff_[i], leaf_coeff_[i].size()) << ' ';
str_buf << ArrayToString<true>(leaf_coeff_[i], leaf_coeff_[i].size()) << ' ';
}
str_buf << ' ';
}
Expand Down Expand Up @@ -769,7 +769,7 @@ Tree::Tree(const char* str, size_t* used_len) {

if (is_linear_) {
if (key_vals.count("leaf_const")) {
leaf_const_ = Common::StringToArrayFast<double>(key_vals["leaf_const"], num_leaves_);
leaf_const_ = Common::StringToArray<double>(key_vals["leaf_const"], num_leaves_);
} else {
leaf_const_.resize(num_leaves_);
}
Expand All @@ -791,7 +791,7 @@ Tree::Tree(const char* str, size_t* used_len) {
}
std::vector<double> all_leaf_coeff;
if (key_vals.count("leaf_coeff")) {
all_leaf_coeff = Common::StringToArrayFast<double>(key_vals["leaf_coeff"], total_num_feat);
all_leaf_coeff = Common::StringToArray<double>(key_vals["leaf_coeff"], total_num_feat);
}
int sum_num_feat = 0;
for (int i = 0; i < num_leaves_; ++i) {
Expand Down

0 comments on commit 7f91dc6

Please sign in to comment.