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

Preventing denormal leaf values #2724

Merged
merged 2 commits into from
Feb 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 20 additions & 3 deletions include/LightGBM/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ class Tree {

/*! \brief Set the output of one leaf */
inline void SetLeafOutput(int leaf, double output) {
leaf_value_[leaf] = output;
// Prevent denormal values because they can cause std::out_of_range exception when converting strings to doubles
if (IsZero(output)) {
leaf_value_[leaf] = 0;
} else {
leaf_value_[leaf] = output;
}
}

/*!
Expand Down Expand Up @@ -149,7 +154,13 @@ class Tree {
inline void Shrinkage(double rate) {
#pragma omp parallel for schedule(static, 1024) if (num_leaves_ >= 2048)
for (int i = 0; i < num_leaves_; ++i) {
leaf_value_[i] *= rate;
double new_leaf_value = leaf_value_[i] * rate;
// Prevent denormal values because they can cause std::out_of_range exception when converting strings to doubles
if (IsZero(new_leaf_value)) {
leaf_value_[i] = 0;
} else {
leaf_value_[i] = new_leaf_value;
}
}
shrinkage_ *= rate;
}
Expand All @@ -161,7 +172,13 @@ class Tree {
inline void AddBias(double val) {
#pragma omp parallel for schedule(static, 1024) if (num_leaves_ >= 2048)
for (int i = 0; i < num_leaves_; ++i) {
leaf_value_[i] = val + leaf_value_[i];
double new_leaf_value = val + leaf_value_[i];
// Prevent denormal values because they can cause std::out_of_range exception when converting strings to doubles
if (IsZero(new_leaf_value)) {
leaf_value_[i] = 0;
} else {
leaf_value_[i] = new_leaf_value;
}
}
// force to 1.0
shrinkage_ = 1.0f;
Expand Down