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 calculation of weighted gamma loss (fixes #4174) #4283

Merged
merged 8 commits into from
May 21, 2021
Merged

fix calculation of weighted gamma loss (fixes #4174) #4283

merged 8 commits into from
May 21, 2021

Conversation

mayer79
Copy link
Contributor

@mayer79 mayer79 commented May 13, 2021

Fixes #4174

@mayer79 mayer79 changed the title fixes https://github.com/microsoft/LightGBM/issues/4174 fixes issue #4174 May 13, 2021
@jameslamb jameslamb added the fix label May 13, 2021
@jameslamb jameslamb changed the title fixes issue #4174 fix calculation of weighted gamma loss (fixes #4174) May 13, 2021
@jameslamb
Copy link
Collaborator

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).

@jameslamb
Copy link
Collaborator

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

>       from distributed.protocol.core import dumps_msgpack
E       ImportError: cannot import name 'dumps_msgpack' from 'distributed.protocol.core' (/root/miniconda/envs/test-env/lib/python3.7/site-packages/distributed/protocol/core.py)

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.

Copy link
Contributor

@lorentzenchr lorentzenchr left a 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]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Collaborator

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

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]);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in #4289.

@mayer79 mayer79 requested review from jameslamb and Laurae2 as code owners May 14, 2021 08:24
@jameslamb
Copy link
Collaborator

Ok @mayer79 , now that we've merged #4288, if you update to the latest master I think the failing checks should pass. Sorry for the inconvenience!

Copy link
Collaborator

@jameslamb jameslamb left a 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")
Copy link
Collaborator

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).

@lorentzenchr
Copy link
Contributor

@shiyu1994 After having reviewed #4289, maybe you'd like to have another look here as it fixes an actual bug.

@jameslamb jameslamb mentioned this pull request May 20, 2021
21 tasks
Copy link
Collaborator

@shiyu1994 shiyu1994 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes LGTM.

@StrikerRUS StrikerRUS merged commit 4b1b412 into microsoft:master May 21, 2021
@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression Gamma Loss Gradient calculation is incorrect with weight
5 participants