Skip to content

FIX properly report n_iter_ in case of fallback from Newton-Cholesky to LBFGS #30100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Oct 21, 2024

Conversation

ogrisel
Copy link
Member

@ogrisel ogrisel commented Oct 18, 2024

Fix for a small bug discovered while reviewing #28840: whenever the Newton-Cholesky solver of LogisticRegression would fall back to LFGS, the reported n_iter_ attribute would be left to zero.

I made it such that any completed iteration from the Newton-Cholesky solver would be subtracted from max_iter before calling LBFGS, and then report the sum of the numbers of iterations completed by the two solvers in the end. In practice, this does not seem to change anything because when Hessian conditioning problem always happens during the first iteration in my experiments with low regularized, rank deficient problems that typically trigger the LBFGS fallback mechanism.

Note that I find that the LBFGS fallback warning quite annoying whenever it is triggered while tuning the regularization level (e.g. using LogisticRegressionCV or RandomizedSearchCV). I have the feeling that this should be a regular verbose print instead, but we can tackle that in a separate PR.

Copy link

github-actions bot commented Oct 18, 2024

✔️ Linting Passed

All linting checks passed. Your pull request is in excellent shape! ☀️

Generated for commit: e0ec69a. Link to the linter CI: here

@ogrisel ogrisel changed the title FIX properly report n_iter in case of Newton-Cholesky to LBFGS fallback FIX properly report n_iter_ in case of fallback from Newton-Cholesky to LBFGS Oct 18, 2024
Copy link
Member

@lorentzenchr lorentzenchr left a comment

Choose a reason for hiding this comment

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

Thanks for spotting and fixing this bug.
I also would like to see improvements for the fallback case.

@lorentzenchr lorentzenchr added this to the 1.6 milestone Oct 18, 2024
ogrisel and others added 2 commits October 18, 2024 17:34
Co-authored-by: Christian Lorentzen <lorentzen.ch@gmail.com>
test_newton_cholesky_fallback_to_lbfgs
@ogrisel
Copy link
Member Author

ogrisel commented Oct 18, 2024

The global_random_seed parametrized test pass for all seeds locally.

Unfortunately this is not the case on our macOS pylatest_conda_mkl_no_openmp runner, even after having decreased the regularization and increased the data width.

There is a failure with global_random_seed=6 on that host.

        # Trying to fit the same model again with a small iteration budget should
        # therefore raise a ConvergenceWarning:
        lr_nc_limited = LogisticRegression(
            solver="newton-cholesky", C=C, max_iter=n_iter_lbgs - 1
        )
>       with pytest.warns(LinAlgWarning, match="ill-conditioned Hessian matrix"):
E       Failed: DID NOT WARN. No warnings of type (<class 'scipy.linalg._misc.LinAlgWarning'>,) were emitted.
E       The list of emitted warnings is: []

I pushed a new commit with the [all random seeds] commit message for that test to see there is only a problem with this CI environment or if the test is also brittle on other environment.

@ogrisel
Copy link
Member Author

ogrisel commented Oct 18, 2024

Ok, so the warning is never raised for macOS pylatest_conda_mkl_no_openmp whatever the choice of the random seed, while it is always raised (as expected) for all the seeds in all the other testing environments. So the problem is not related to the statistical properties of the test problem.

- use ignore_warnings(category=LinAlgWarning) for now;
- store n_iter values in local variables to help CI-log debugging;
- fix final assertion.
@ogrisel
Copy link
Member Author

ogrisel commented Oct 18, 2024

I tried to look into the scipy version / BLAS version and so, but I could not find anything that would explain why the warning was not raised only in macOS pylatest_conda_mkl_no_openmp while it works for other environments (with or without MKL, with older and newer versions of scipy and so on).

@ogrisel
Copy link
Member Author

ogrisel commented Oct 18, 2024

Ok, so it seems to be a bug in the propagation of LinAlgWarning but the iteration logic is still somehow respected...

I will push another commit to clean-up the commented out code and keep the ignore_warnings thing for now.

@ogrisel ogrisel added the Quick Review For PRs that are quick to review label Oct 18, 2024
@adrinjalali adrinjalali enabled auto-merge (squash) October 21, 2024 09:50
@adrinjalali adrinjalali merged commit 39e1cc1 into scikit-learn:main Oct 21, 2024
28 checks passed
@ogrisel ogrisel deleted the newton-cholesky-to-lbfgs-n_iter branch October 21, 2024 12:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module:linear_model Quick Review For PRs that are quick to review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants