Repository navigation
ENH Newton-CD part 8: add multinomial NewtonCDSolver - #34681
Conversation
b620d49 to
39e7fd7
Compare
47dafb8 to
c6ddfbb
Compare
c6ddfbb to
dccdd01
Compare
|
@virchan @OmarManzoor friendly ping. This is the final piece of the "newton coordinate descent" project. |
867cc87 to
59be645
Compare
virchan
left a comment
There was a problem hiding this comment.
Thank you for the PR, @lorentzenchr.
Here is my first round of review.
| # sqrt(samples_weight / sw_sum) = sqrt_sw, i.e. as if sqrt(D) would have | ||
| # been multiplied by sqrt_sw. | ||
| LDL = Multinomial_LDL_Decomposition(proba=proba) | ||
| XtA = np.zeros((n_classes, n_features), dtype=dtype, order="F") # TODO: best order? |
There was a problem hiding this comment.
I think C-order is better here.
F-order does help the sparse-filling loop in gap_enet_multinomial, but I believe the performance difference is negligible after switching to C, since that loop only runs on convergence checks.
Another point: XtA has the same shape and for-loop structure as norm2_cols_X, which is already C-ordered. So it might be better to match them.
There was a problem hiding this comment.
Can we leave it as is for this PR? The fit_intercept in gap_enet_multinomial would get quite tricky with a different memory layout.
virchan
left a comment
There was a problem hiding this comment.
LGTM. I don't have anything else to add beyond my previous comments.
Thanks, @lorentzenchr!
|
@antoinebaker could you kindly help with reviewing this PR? |
antoinebaker
left a comment
There was a problem hiding this comment.
Thanks for the great work @lorentzenchr on this PR and the preceding ones. I'll still reviewing the bulk of this PR (enet_coordinate_descent_multinomial) but LGTM so far !
|
@antoinebaker @OmarManzoor With one more approval we could merge. |
OmarManzoor
left a comment
There was a problem hiding this comment.
Thank you for this amazing PR @lorentzenchr. I left some comments. I have not reviewed the whole _cd_fast.pyx file properly particularly the complete enet_coordinate_descent_multinomial function because of the length and heavy concentration of linear algebra. I have also not reviewed the linear loss tests.
I'll leave these main items to @antoinebaker
I find the test |
24ff546 to
ee48450
Compare
|
Once this PR is merge, I will tackle the |
| # H0 = H[-n_classes:-1, :-n_classes] = Hessian part mixing intercepts with | ||
| # features | ||
| H0 = np.zeros((n_classes - 1, n_classes * n_features), dtype=dtype) | ||
| H0_coef = np.zeros((n_classes - 1,), dtype=dtype) # 1' LDL raw_prediction |
There was a problem hiding this comment.
Hum, following the docstring H0_coef = 1' LDL X coef. Do we also have H0_coef = 1' LDL raw_prediction as suggested by the comment ?
There was a problem hiding this comment.
raw_prediction = X @ coef
I added the full parameter list to the docstring where raw_prediction is explained. Does that answer your question?
There was a problem hiding this comment.
Hum, I thought raw_predictions = X @ coef + intercepts ?
There was a problem hiding this comment.
Ah according to #34681 (comment) coef also contains the intercepts, and I guess X is for the extended X with all ones column. But could you add a little comment above to clarify the meaning of H0_coef (eg Here coef stands for the weights and intercepts, so X coef = X weights + intercepts = raw_predictions)
There was a problem hiding this comment.
But what's confusing (when reading the code) is that coef means the weights only in the Intercepts section of the docstring.
There was a problem hiding this comment.
What do you want me to do?
You have 2 options of reading it:
- Ignore the intercept, coef is just all coefficients. The intercept will always get special treatment anyway.
- Think of
Xas including a column of ones at the end, such thatX @ coef = X[:, :-1] @ coef[:-1] + coef[-1] = raw_predictions(but onlycoef[:-1]is penalized).
Copilot with Claude Sonnet 5 did not complain about the code comments.
There was a problem hiding this comment.
Read the Intercepts section, and then read the code fit_intercept=True, I think there is a contradiction, or at least an ambiguity on coef (sometimes it's just the weights, sometimes it's weights and intercepts). It's a bit confusing when reading the code after reading the Intercepts section and checking that the two are aligned. I'm sure Claude has no problem, but as a poor human, I did have a hard time.
There is also a leap between what the Intercept section says (replace A and b) and what the code actually does (replace raw_prediction and g). Adding one just sentence in the Intercept section would help I think.
There was a problem hiding this comment.
When I was developping, I had the full code with 2 windows on screen. This is much easier than reading the diff in github review mode.
I guess one possible misunderstanding is the distinction between
- w: the coefficients to be updated
- coef: the current coefficients (of the last Newton iteration). At function entry the same as w (an only used for precomputations).
I‘ll improve on that.
| # memory buffers for temporaries | ||
| x_k = np.empty(n_samples, dtype=dtype) | ||
| xx = np.empty((n_samples, n_classes), dtype=dtype, order="F") | ||
| t_k = np.empty(n_samples, dtype=dtype) | ||
| if fit_intercept: | ||
| tt = np.empty((n_samples, n_classes), dtype=dtype, order="F") |
There was a problem hiding this comment.
Array names are not very explicit :)
There was a problem hiding this comment.
We reuse them. They really are just memory buffers for temporary storage of computations.
| # Centering X as | ||
| # X -= (H00_pinv_H0)[None, :] | ||
| # is not an option because it would mean n_classes^2 copies of X. | ||
| # Center raw_prediction: += -intercepts - 1 H00^(-1) H0) coef |
There was a problem hiding this comment.
The docstring above only mention replacing A and b when taking into account the intercept. Could you explain why raw_prediction are centered here, and why it's equivalent in the end to replacing A and b ?
There was a problem hiding this comment.
It is not equivalent at all. It only contains part of the "centering" to speed up computation (do what you can as early as possible). If you look at the residual update with intercept, this most intricate computation is because of the "centering", in its full beauty.
- Reason: it is a test for the data, not for the estimator
- remove timing in Cython (already commented out) - set multiclass support to yes in LR docstring - some more improvements
f27b071 to
4411d82
Compare
antoinebaker
left a comment
There was a problem hiding this comment.
Thanks again @lorentzenchr for the PR !
OmarManzoor
left a comment
There was a problem hiding this comment.
LGTM. Thank you for this work @lorentzenchr and thank you for the reviews @antoinebaker and @virchan
@lorentzenchr please merge after the remaining improvement is added.
1ba0084 to
8730cbd
Compare
|
🥳🥳🥳 A great thank you to all the reviewers, in particular @OmarManzoor @virchan and @antoinebaker. This PR finishes the Newton Coordinate Descent solvers which took ~2 years of development (#33160, and before that #25462 to research for and implementation of the analytic LDL decomposition, it was the missing piece in #23507 (comment)). I leave it to one of the reviewers to merge as it is custom in scikit-learn. (We do not need to wait for #32042, updating that one will be easy enough.) |

Reference Issues/PRs
Part 8 of #33160, follow-up of #34561.
What does this implement/fix? Explain your changes.
#34561 introduced the
solver="newton-cd"which uses coordinate descent without gram matrix (=Hessian), i.e. likeElasticNet(precompute=False).It only uses the full hessian implicitly. This works by taking the square root of the Hessian (X'WX) and formulating the update step (one Newton iteration) as a least squares problem with penalty.Important
This does not work out of the box for the multinomial/multiclass setting because the pointwise hessian (W) is not diagonal.
Several attempts of approximations that e.g. glmnet uses don't converge on simple problems.
This PR extends #34561 by introducing a dedicated coordinate descent solver for the multinomial. It uses an analytic LDL decomposition of the pointwise hessian (aka covariance of the multinomial) in order to take the square root of X'WX.
This is a very good solver for
n_features > n_samples, thus closing this gap in solver options (currently only saga, but it often has convergence issues).AI usage disclosure
Github Copilot (Claude Sonnet 5) for
Any other comments?
TODO: