-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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 calculation of weighted gamma loss (fixes #4174) #4283
Conversation
Thanks for this! I've edited the pull request title to be more descriptive. Pull request titles become changelog entries in this project (please see https://github.com/microsoft/LightGBM/releases/tag/v3.2.1). |
I don't think the CUDA build failures are related to these changes. All of the Dask tests are failing there with the following error
I can look into this in a few hours. I suspect that this is a result of some new version of a dependency being released, will have to check them later to see. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixed the bug. What about a unit test for this?
@@ -695,7 +695,7 @@ class RegressionGammaLoss : public RegressionPoissonLoss { | |||
} else { | |||
#pragma omp parallel for schedule(static) | |||
for (data_size_t i = 0; i < num_data_; ++i) { | |||
gradients[i] = static_cast<score_t>(1.0 - label_[i] / std::exp(score[i]) * weights_[i]); | |||
gradients[i] = static_cast<score_t>((1.0 - label_[i] / std::exp(score[i])) * weights_[i]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gradients[i] = static_cast<score_t>((1.0 - label_[i] / std::exp(score[i])) * weights_[i]); | |
gradients[i] = static_cast<score_t>((1.0 - label_[i] * std::exp(-score[i])) * weights_[i]); |
Could be a tiny little bit faster.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lorentzenchr
Regarding timing: the same pattern (i.e. y/exp(p) instead of y*exp(-p) appears multiple time in the same file. My suggestion would be to open a new issue/PR that would change this consistently, along with 1-2 experiments on timings.
Regarding unit tests: I added some in a new R script. On the current master, it fails. With the gamma weight fix it passes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the exp
dominates timing by far.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that we should make the formula in the same file consistent. If we want to change division into multiplication, we should modify all these
LightGBM/src/objective/regression_objective.hpp
Lines 689 to 700 in adf36d7
if (weights_ == nullptr) { | |
#pragma omp parallel for schedule(static) | |
for (data_size_t i = 0; i < num_data_; ++i) { | |
gradients[i] = static_cast<score_t>(1.0 - label_[i] / std::exp(score[i])); | |
hessians[i] = static_cast<score_t>(label_[i] / std::exp(score[i])); | |
} | |
} else { | |
#pragma omp parallel for schedule(static) | |
for (data_size_t i = 0; i < num_data_; ++i) { | |
gradients[i] = static_cast<score_t>(1.0 - label_[i] / std::exp(score[i]) * weights_[i]); | |
hessians[i] = static_cast<score_t>(label_[i] / std::exp(score[i]) * weights_[i]); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in #4289.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests and changes look good to me! But another maintainer who's more familiar with gamma loss should probably review as well, so I'm just leaving a "comment" review.
@@ -0,0 +1,66 @@ | |||
context("Case weights are respected") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test looks good to me, thank you! This might be the first unit test we've added that covers weighted training at all in the R package, so really appreciate it.
It's ok with me for this to live in a new test file as you've set it up. test_basic.R
(where most of the other lgb.train()
tests live) has gotten kind of big, and there are already uses of lgb.train()
outside of that file in their own files (such as https://github.com/microsoft/LightGBM/blob/c629cb0b6bd2830894b710cbd4d8241b82ac3105/R-package/tests/testthat/test_learning_to_rank.R or https://github.com/microsoft/LightGBM/blob/c629cb0b6bd2830894b710cbd4d8241b82ac3105/R-package/tests/testthat/test_custom_objective.R).
@shiyu1994 After having reviewed #4289, maybe you'd like to have another look here as it fixes an actual bug. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes LGTM.
This pull request has been automatically locked since there has not been any recent activity since it was closed. To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this. |
Fixes #4174