8000 FIX Corrects negative gradient of AdaBoost loss in GBDT by glemaitre · Pull Request #22050 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/whats_new/v1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ Changelog
:class:`ensemble.ExtraTreesClassifier`.
:pr:`20803` by :user:`Brian Sun <bsun94>`.

- |Fix| Solve a bug in :class:`ensemble.GradientBoostingClassifier` where the
exponential loss was computing the positive gradient instead of the
negative one.
:pr:`22050` by :user:`Guillaume Lemaitre <glemaitre>`.

:mod:`sklearn.feature_extraction.text`
......................................

Expand Down
4 changes: 2 additions & 2 deletions sklearn/ensemble/_gb_losses.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,8 +935,8 @@ def negative_gradient(self, y, raw_predictions, **kargs):
The raw predictions (i.e. values from the tree leaves) of the
tree ensemble at iteration ``i - 1``.
"""
y_ = -(2.0 * y - 1.0)
return y_ * np.exp(y_ * raw_predictions.ravel())
y_ = 2.0 * y - 1.0
return y_ * np.exp(-y_ * raw_predictions.ravel())

def _update_terminal_region(
self,
Expand Down
15 changes: 15 additions & 0 deletions sklearn/ensemble/tests/test_gradient_boosting_loss_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 😉

"""Check that we compute the negative gradient of the exponential loss.

Non-regression test for:
https://github.com/scikit-learn/scikit-learn/issues/9666
"""
loss = ExponentialLoss(n_classes=2)
y_true = np.array([0])
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
assert_allclose(loss.negative_gradient(y_true, y_pred), -1)
0