Repository navigation
PERF: GradientBoosting* optimize line-search from O(n * n_leaves) to O(n) for classification losses - #32911
Conversation
GradientBoosting* optimize line-search from O(n * n_leaves) to O(n).GradientBoosting* optimize line-search from O(n * n_leaves) to O(n) for classification losses.
GradientBoosting* optimize line-search from O(n * n_leaves) to O(n) for classification losses.GradientBoosting* optimize line-search from O(n * n_leaves) to O(n) for classification losses
|
I saw @ogrisel put this PR in "in progress" so I finished preparing it for a first review ^^ Also here is benchmark of the "best improvement scenario" for this PR: import numpy as np
from sklearn.ensemble import GradientBoostingClassifier
n, d = 100_000, 4
X = np.random.geometric(0.05, size=(n, d))
y = np.random.choice(3, n)
gb = GradientBoostingClassifier(n_estimators=10, max_depth=None)
gb.fit(X, y)Before this PR: 240s |
|
I opened a separate PR on top of this one: cakedev0#4. For the same optimization but on regression losses (AE, pinball, huber). The speed-up is similarly good, but it's +300 loc 😅 |
| assert np.all(gbt_huber_predictions <= gbt_squared_error.predict(X)) | ||
|
|
||
|
|
||
| def test_safe_divide(): |
There was a problem hiding this comment.
I removed this test because we still cover for "division by zero doesn’t emit warnings" in leaf updates with the existing test_gb_denominator_zero.
OmarManzoor
left a comment
There was a problem hiding this comment.
Thank you for the PR @cakedev0
…cy.rst Co-authored-by: Omar Salman <[email protected]>
OmarManzoor
left a comment
There was a problem hiding this comment.
This looks nice! Thank you @cakedev0
…v0/scikit-learn into optim/gbt_update_terminal_regions
thomasjpfan
left a comment
There was a problem hiding this comment.
Minor comment, otherwise LGTM
…o O(n) for classification losses (scikit-learn#32911) Co-authored-by: Omar Salman <[email protected]>
Why current complexity is O(n * n_leaves)
Currently the line-search is implemented this way:
np.nonzero(masked_terminal_regions == leaf)is O(n_sample) and we do itn_leavestimes. This can quickly become prohibitively slow for trees with many leaves: typically, if trees are grown fully, it becomes O(n^2).Why this can be damaging
While trees are usually shallow for GB, nothing prevents users from making deep trees, and especially nothing prevents them from doing some hyper-parameter search that will in some cases create deep trees. Such a search could suffer a lot from this O(n^2) in some part of the search-space.
What does this implement/fix? Explain your changes.
For the classification losses, it was possible to rewrite the calculation without a loop using
np.bincount(leaves_idx, weights=w)to efficiently compute sums and averages grouped by leaves. This is a classic trick to do a "group by" in numpy.It can result in a 10x speed-up on the total training duration when trees are grown fully.
For the MSE, line search is not needed, so it doesn't need optimization
For the other regression losses (AE, huber, pinball),
loss.fit_intercept_onlyis called for each leaf; hence it's more complicated:loss.fit_intercept_only_by_idx. It would require aweighted_quantile_per_idx, I tried implementing that, and it's very fast: see PR PRF: Optimize GB line search regression (RFC: is it worth it?) cakedev0/scikit-learn#4 quite a lot of code, and so I've put it in a separate PR.AI usage
Not that I remember (but I promise I do use AI sometimes).