Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

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 <[email protected]>
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

"""
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
glemaitre added a commit that referenced this pull request Dec 25, 2021
venkyyuvy pushed a commit to venkyyuvy/scikit-learn that referenced this pull request Jan 1, 2022
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