Skip to content

FIX Corrects negative gradient of AdaBoost loss in GBDT #22050

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

Merged
merged 7 commits into from
Dec 23, 2021

Conversation

glemaitre
Copy link
Member

closes #9666

Indeed, this seems fishy. We used the following as a reference: https://pbil.univ-lyon1.fr/CRAN/web/packages/gbm/vignettes/gbm.pdf

The loss for a given point is given by:

u = -(2.0 * y_true - 1.0)
np.exp(u * y_pred)

Thus taking the derivative respect to y_pred, we will have:

u = -(2.0 * y_true - 1.0)
u * np.exp(u * y_pred)

Thus, the negative gradient is indeed:

u = -(2.0 * y_true - 1.0)
- u * np.exp(u * y_pred)

All gradient reported in the document seems to be the negative gradient but not for the AdaBoost loss.

@lorentzenchr
Copy link
Member

Whaaaat? Why did no other test fire up? And why do LeastSquaresError and BinomialDeviance compute only half of the negative gradient in negative_gradient?

@glemaitre Thanks for this fix.
I think the long term solution is #20567. This would make maintenance easier (and I dare so say🤞, such a bug would trigger a test to fail).

Copy link
Member

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

LGTM with or without the proposed more general test (maybe better without).

@@ -320,3 +320,18 @@ def test_lad_equals_quantiles(seed, alpha):
y_true, raw_predictions, sample_weight=weights, alpha=alpha
)
assert pbl_weighted_loss == approx(ql_weighted_loss)


def test_exponential_loss():
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer to compare the gradients to numerical differentiation for all losses, but this certainly is a regression test for this bug.

Copy link
Member Author

Choose a reason for hiding this comment

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

We can have add these tests for the common losses I hope :)

Copy link
Member

Choose a reason for hiding this comment

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

That the point: they are already there 😉

@glemaitre
Copy link
Member Author

Why did no other test fire up? And why do LeastSquaresError and BinomialDeviance

It is what I saw as well. It is indeed written in the gbm package but this is not correct.

Why did no other test fire up

From the original issue, it seems that the way that we update the leaves made that we were fine but I am not absolutely sure since we don't have a good integration test that uses the AdaBoost loss. Now we have a unit test at least.

I think the long term solution is #20567. This would make maintenance easier (and I dare so say🤞, such a bug would trigger a test to fail).

I agree :) You can have a weird numerical precision bug in 32 bits that you don't expect but it less likely to take the negative of the gradient when you should be :)

Co-authored-by: Christian Lorentzen <lorentzen.ch@gmail.com>
y_pred = np.array([0])
# we expect to have loss = exp(0) = 1
assert loss(y_true, y_pred) == pytest.approx(1)
# we expect to have negative gradient = -1 * (1 * exp(0)) = 1
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# we expect to have negative gradient = -1 * (1 * exp(0)) = 1
# we expect to have negative gradient = -1 * (1 * exp(0)) = -1

@@ -936,7 +936,7 @@ def negative_gradient(self, y, raw_predictions, **kargs):
tree ensemble at iteration ``i - 1``.
"""
y_ = -(2.0 * y - 1.0)
return y_ * np.exp(y_ * raw_predictions.ravel())
return -y_ * np.exp(y_ * raw_predictions.ravel())
Copy link
Member

Choose a reason for hiding this comment

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

I think the negatives make a copy and we can prevent an extra copy by:

y_ = 2.0 * y - 1.0
return y_ * np.exp(-y_ * raw_predictions.ravel())

Copy link
Member

@thomasjpfan thomasjpfan left a comment

Choose a reason for hiding this comment

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

LGTM

@thomasjpfan thomasjpfan changed the title FIX compute correctly the negative gradient of AdaBoost loss in GBDT FIX Corrects negative gradient of AdaBoost loss in GBDT Dec 23, 2021
@thomasjpfan thomasjpfan merged commit 5bb1816 into scikit-learn:main Dec 23, 2021
glemaitre added a commit to glemaitre/scikit-learn that referenced this pull request Dec 24, 2021
…#22050)

Co-authored-by: Christian Lorentzen <lorentzen.ch@gmail.com>
glemaitre added a commit that referenced this pull request Dec 25, 2021
Co-authored-by: Christian Lorentzen <lorentzen.ch@gmail.com>
venkyyuvy pushed a commit to venkyyuvy/scikit-learn that referenced this pull request Jan 1, 2022
…#22050)

Co-authored-by: Christian Lorentzen <lorentzen.ch@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The negative_gradient method of ExponentialLoss returns the positive gradient
3 participants