Skip to content

ENH Newton-CD part 8: add multinomial NewtonCDSolver - #34681

Merged
OmarManzoor merged 15 commits into
scikit-learn:mainfrom
lorentzenchr:logistic_lasso_newton_part_8
Sep 30, 2026
Merged

OmarManzoor merged 15 commits into
scikit-learn:mainfrom
lorentzenchr:logistic_lasso_newton_part_8

Conversation

@lorentzenchr

@lorentzenchr lorentzenchr commented Aug 8, 2026 •

Copy link
Copy Markdown
Member

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. like ElasticNet(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

  • an improvement of the residual update
  • code review

Any other comments?

TODO:

  • remove redundant Python CD algorithm
  • update user guide on logistic regression solvers
  • update changelog

@lorentzenchr
lorentzenchr force-pushed the logistic_lasso_newton_part_8 branch 2 times, most recently from b620d49 to 39e7fd7 Compare August 8, 2026 16:03
@github-actions github-actions Bot added the CI:Linter failure The linter CI is failing on this PR label Aug 13, 2026
@lorentzenchr
lorentzenchr force-pushed the logistic_lasso_newton_part_8 branch from 47dafb8 to c6ddfbb Compare August 13, 2026 20:06
@github-actions github-actions Bot removed the CI:Linter failure The linter CI is failing on this PR label Aug 13, 2026
@lorentzenchr
lorentzenchr force-pushed the logistic_lasso_newton_part_8 branch from c6ddfbb to dccdd01 Compare August 13, 2026 20:19
@lorentzenchr

Copy link
Copy Markdown
Member Author

@virchan @OmarManzoor friendly ping. This is the final piece of the "newton coordinate descent" project.

@virchan virchan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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 virchan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. I don't have anything else to add beyond my previous comments.

Thanks, @lorentzenchr!

@virchan virchan added the Waiting for Second Reviewer First reviewer is done, need a second one! label Aug 18, 2026
@OmarManzoor

Copy link
Copy Markdown
Contributor

@antoinebaker could you kindly help with reviewing this PR?

@antoinebaker antoinebaker moved this to In progress in Labs Aug 24, 2026
@antoinebaker antoinebaker mentioned this pull request Sep 1, 2026
2 tasks done

@antoinebaker antoinebaker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 !

Comment thread sklearn/linear_model/_cd_fast.pyx Resolved
Comment thread sklearn/linear_model/_cd_fast.pyx Resolved
@antoinebaker

Copy link
Copy Markdown
Member

The docstrings of LogisticRegression and LogisticRegressionCV need to be updated.
Capture d’écran 2026-09-02 à 17 51 55

newton-cd does not appear in the solver options of LogisticRegressionCV.

@lorentzenchr

Copy link
Copy Markdown
Member Author

@antoinebaker @OmarManzoor With one more approval we could merge.

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Comment thread sklearn/linear_model/_linear_loss.py Resolved
Comment thread sklearn/linear_model/_linear_loss.py Resolved
Comment thread sklearn/linear_model/_linear_loss.py Resolved
Comment thread sklearn/linear_model/_linear_loss.py Resolved
Comment thread sklearn/linear_model/_linear_loss.py Resolved
Comment thread sklearn/linear_model/_linear_loss.py Resolved
Comment thread sklearn/linear_model/_cd_fast.pyx Resolved
Comment thread sklearn/linear_model/_cd_fast.pyx Resolved
Comment thread sklearn/linear_model/_cd_fast.pyx Resolved
Comment thread sklearn/linear_model/_cd_fast.pyx Resolved
@lorentzenchr

Copy link
Copy Markdown
Member Author

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 find the test test_newton_solver.py::test_cd_multinomial pretty convincing.

@lorentzenchr
lorentzenchr force-pushed the logistic_lasso_newton_part_8 branch 2 times, most recently from 24ff546 to ee48450 Compare September 18, 2026 12:24
@lorentzenchr

Copy link
Copy Markdown
Member Author

Once this PR is merge, I will tackle the solver="auto" option (and deprecate the liblinear solver).

Comment thread sklearn/linear_model/_cd_fast.pyx Resolved
Comment thread sklearn/linear_model/_cd_fast.pyx Resolved
# 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 ?

@lorentzenchr lorentzenchr Sep 25, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

raw_prediction = X @ coef

I added the full parameter list to the docstring where raw_prediction is explained. Does that answer your question?

@antoinebaker antoinebaker Sep 25, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hum, I thought raw_predictions = X @ coef + intercepts ?

@antoinebaker antoinebaker Sep 25, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

But what's confusing (when reading the code) is that coef means the weights only in the Intercepts section of the docstring.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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 X as including a column of ones at the end, such that X @ coef = X[:, :-1] @ coef[:-1] + coef[-1] = raw_predictions (but only coef[:-1] is penalized).

Copilot with Claude Sonnet 5 did not complain about the code comments.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@lorentzenchr lorentzenchr Sep 29, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@lorentzenchr lorentzenchr Sep 30, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

See 8730cbd.

Comment on lines +2608 to +2613
# 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")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Array names are not very explicit :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We reuse them. They really are just memory buffers for temporary storage of computations.

Comment thread sklearn/linear_model/_cd_fast.pyx Resolved
Comment thread sklearn/linear_model/_cd_fast.pyx Resolved
Comment thread sklearn/linear_model/_cd_fast.pyx Resolved
# 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 ?

@lorentzenchr lorentzenchr Sep 25, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Comment thread sklearn/linear_model/_cd_fast.pyx Resolved
Comment thread sklearn/linear_model/_cd_fast.pyx Resolved
@lorentzenchr
lorentzenchr force-pushed the logistic_lasso_newton_part_8 branch from f27b071 to 4411d82 Compare September 25, 2026 13:34
Comment thread sklearn/linear_model/_cd_fast.pyx Resolved
Comment thread sklearn/linear_model/_cd_fast.pyx Resolved
Comment thread sklearn/linear_model/_cd_fast.pyx Resolved
Comment thread sklearn/linear_model/_cd_fast.pyx Resolved

@antoinebaker antoinebaker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks again @lorentzenchr for the PR !

@OmarManzoor OmarManzoor removed the Waiting for Second Reviewer First reviewer is done, need a second one! label Sep 30, 2026

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread sklearn/linear_model/_cd_fast.pyx Resolved
@lorentzenchr
lorentzenchr force-pushed the logistic_lasso_newton_part_8 branch from 1ba0084 to 8730cbd Compare September 30, 2026 08:57
@lorentzenchr

lorentzenchr commented Sep 30, 2026 •

Copy link
Copy Markdown
Member Author

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

@OmarManzoor
OmarManzoor merged commit bbf8863 into scikit-learn:main Sep 30, 2026
37 checks passed
@lorentzenchr
lorentzenchr deleted the logistic_lasso_newton_part_8 branch September 30, 2026 13:32
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.

4 participants