You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Array API support for LogisticRegression with LBFGS #32611
Even if scipy.optimize does not support array API yet, we can probably work around it to perform the computation of the gradient using the input data array namespace and convert the gradient values to numpy prior to feeding them back to the solver at each iteration.
Assuming n_samples >> n_features, this is likely to be a worthwhile way to get a large speed-up on GPU devices for that estimator despite back and forth conversion to and from device for the parameters array.
To compute the gradient we perform X @ coef + intercept which leads to "raw predictions". Those raw predictions are fed along with y to the Cython loss. This will be an (n_samples, n_classes) array. For a first iteration, we can convert that to NumPy and continue using Cython. Alternatively, we can write pure Python array API capable version of the gradient method of the loss functions. We should start with log loss for which we can already refactor and move the sklearn.utils._array_api._half_multinomial_loss function (introduced for metrics) into the sklearn._loss package to add its gradient/Hessian counterparts.
We can leave the other losses as raising NotImplementError for now.
For a start maybe I can try adding the gradient part of HalfMultinomialLoss as a dedicated PR? Then we can add array API support for LogisticRegression with lbfgs in another PR. What do you think?
But I think we would also need HalfBinomialLoss refactored with respect to the array API.
I find it weird to merge a PR that implements some private functions/methods that are never called in any public counterparts. I would rather introduce such new functions/methods only when actually needed. Otherwise we might not realize that they have a design defect of some kind.
@ogrisel How about first adding array API support for LogisticRegression with LBFGS without modifying the loss or gradient and simply converting to numpy when calling the optimization related code. Then maybe later we can see if we want to refactor the loss and gradient as well. This might help in working on this systematically.
It sounds like a plan (focusing on first array API support in sklearn/linear_model/_linear_loss.py without touching sklearn/_loss)! Once this first iteration is implemented, we should profile it with py-spy or similar to check how much we have to gain from array API compatible alternatives to our Cython loss function.
Activity
OmarManzoor commentedon Oct 30, 2025
For a start maybe I can try adding the
gradientpart ofHalfMultinomialLossas a dedicated PR? Then we can add array API support forLogisticRegressionwithlbfgsin another PR. What do you think?But I think we would also need
HalfBinomialLossrefactored with respect to the array API.ogrisel commentedon Oct 31, 2025
I find it weird to merge a PR that implements some private functions/methods that are never called in any public counterparts. I would rather introduce such new functions/methods only when actually needed. Otherwise we might not realize that they have a design defect of some kind.
OmarManzoor commentedon Oct 31, 2025
I understand and agree.
OmarManzoor commentedon Nov 3, 2025
@ogrisel How about first adding array API support for
LogisticRegressionwith LBFGS without modifying thelossorgradientand simplyconverting to numpywhen calling the optimization related code. Then maybe later we can see if we want to refactor thelossandgradientas well. This might help in working on this systematically.ogrisel commentedon Nov 3, 2025
It sounds like a plan (focusing on first array API support in
sklearn/linear_model/_linear_loss.pywithout touchingsklearn/_loss)! Once this first iteration is implemented, we should profile it with py-spy or similar to check how much we have to gain from array API compatible alternatives to our Cython loss function.lorentzenchr commentedon Feb 21, 2026
Shall we close as #32644 is merged, or should we first add it to LogisticRegressionCV, too?
OmarManzoor commentedon Feb 21, 2026
From my opinion I think we can close this and create a separate issue for LogisticRegressionCV when required.
lorentzenchr commentedon Feb 21, 2026
#33346 for
LogisticRegressionCV, so I close this one.