Repository navigation
FIX instability of _RidgeGCV - #33020
Conversation
ogrisel
left a comment
There was a problem hiding this comment.
I haven't checked the code and the math yet, but the fact that the tests pass and the examples still look good is very promising.
I also ran some quick interactive timeit experiments and the performance seems good (including with torch/MPS) compared to main.
However, I found a performance regression for the linear regression panel of the examples/ensemble/plot_stack_predictors.py example:
in main, it runs in less than 0.01s: https://scikit-learn.org/dev/auto_examples/ensemble/plot_stack_predictors.html
Maybe this is because of a bad "auto" policy for that case?
Please also find a first pass of feedback below.
There was a problem hiding this comment.
Could you please extend the test test_ridge_cv_results_predictions to check that it works both for n_samples > n_features and n_features < n_samples with the auto/eigen solver?
EDIT: and similarly for test_ridge_cv_multioutput_sample_weight and test_ridge_cv_custom_multioutput_scorer.
Co-authored-by: Christian Lorentzen <[email protected]>
OmarManzoor
left a comment
There was a problem hiding this comment.
Code LGTM now. Thank you for the updates @antoinebaker
I just need to review the tests
| # All other cases ("auto", "eigen", "svd" with sparse X) | ||
| # fallbacks to gram (n <= p) or cov (p < n) | ||
| n, p = X.shape | ||
| return "gram" if n <= p else "cov" |
There was a problem hiding this comment.
Should the intercept play a role for p <=n?
There was a problem hiding this comment.
Hum, do you mean a) to choose between gram vs cov for the gcv_mode or b) in the computations afterwards ?
a) gram passes test_regularization_limits_ridge_gcv for the edge case n=p with/without intercept but cov doesn't.
b) In all cases (svd, cov, gram), the intercept is handled by centering X, y
|
|
||
| loov = (KG^-1Y - diag(KG^-1)Y) / diag(I-KG^-1) | ||
| The solution of the regularized least square (fitted `coef_`) is given by: | ||
| w = H^-1 X^T y = X^T c where c = G^-1 y. |
There was a problem hiding this comment.
Maybe small c os not the best name here.
There was a problem hiding this comment.
I agree, I used the variable name of the previous code. Maybe something like w -> coef, and c -> dual_coef, and d -> diag_Ginv could work ? But on the other hand it's nice to keep a one letter variable name (the equations and code are left aligned and easier to read)
There was a problem hiding this comment.
Maybe don't overthink it. It is not that important. I like single latter variables.
| References | ||
| ---------- | ||
| [1] http://cbcl.mit.edu/publications/ps/MIT-CSAIL-TR-2007-025.pdf | ||
| [1] https://dspace.mit.edu/bitstream/handle/1721.1/37318/MIT-CSAIL-TR-2007-025.pdf |
There was a problem hiding this comment.
Could you properly cite this reference, name and title and so on?
Co-authored-by: Christian Lorentzen <[email protected]>
Co-authored-by: Christian Lorentzen <[email protected]> Co-authored-by: Omar Salman <[email protected]>
OmarManzoor
left a comment
There was a problem hiding this comment.
This looks good from my side. Thank you for the PR @antoinebaker
Co-authored-by: Christian Lorentzen <[email protected]>
|
@antoinebaker This was quite an endeavor, from a "small" numerical" fix to almost a rewrite of RidgeCV. Thanks for your endurance. |
|
Thanks for the careful review @OmarManzoor and @lorentzenchr ! |
Reference Issues/PRs
Fixes #32459 and supersedes #32506
What does this implement/fix? Explain your changes.
This fixes numerical errors in the small alpha regime by refactoring the solve/decompose methods:
looeand the primal coeffcientcoefinstead of thecandG_inverse_diag_compute_gramand_compute_covreuse the precomputedX_meanlooeandcoefis adapted to each method for best numerical stabilitycovto force use eigendecomposition of the covariance matrixgramto force use eigendecomposition of the gram matrixThe
gcv_modeoptions have been modified to always pick the cheaper optioneigennow switches to the cheaper option:gramif n < p andcovifp < nsvd, not implemented for sparse X, fallbacks toeigenwhen X is sparse.eigenalways picksgramandsvdalways fallbacks tocovfor sparse X (whatever the shape of X), which can explain the poor benchmark observed in FIX instability of gcv_mode="svd" in RidgeCV #32506.TODO
_RidgeGCVdocstring, especially math explanationsfloat32inputs: if results are bad, consider re-add the float32 to float64 upcast...gcv_modeand problem shapes are capable of recovering the minimum norm solution in the small alpha limit.test_regularization_limits_ridge_gcv, but there are still things to investigate in a follow-up issue/PR FIX instability of_RidgeGCV#33020 (comment)