Skip to content

MNT Enable stopping LogisticRegression by a callback - #35334

Merged
lesteve merged 1 commit into
scikit-learn:mainfrom
jeremiedbb:lr-callback-stop-fit
Nov 18, 2026
Merged

lesteve merged 1 commit into
scikit-learn:mainfrom
jeremiedbb:lr-callback-stop-fit

Conversation

@jeremiedbb

@jeremiedbb jeremiedbb commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Last part of and closes #35252.
The minimum version of scipy is now able to handle StopIteration in the callback for LBFGS.

ping @betatim

@jeremiedbb jeremiedbb added this to the 1.10 milestone Nov 6, 2026
@jeremiedbb jeremiedbb added the Quick Review For PRs that are quick to review label Nov 6, 2026

@AnneBeyer AnneBeyer 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 @jeremiedbb, LGTM.

@AnneBeyer AnneBeyer added the Waiting for Second Reviewer First reviewer is done, need a second one! label Nov 9, 2026
# lr = LogisticRegression(solver="lbfgs").set_callbacks(cb)
# lr.fit(X, y)
# assert lr.n_iter_ == 1
@ignore_warnings(category=ConvergenceWarning)

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.

Should LR raise a convergence warning when the callback stops the fit?

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.

Not sure. In a real world example I guess the early stopping would come because either a user specified number of iterations has been used up (in which case it would be good to know that things hadn't converged) or because some user defined quality/loss/foo criteria has been achieved (in which case I guess the user doesn't care that things didn't converge?).

So the answer is "sometimes"? But I am speculating :-/

@lorentzenchr lorentzenchr Nov 11, 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.

At least for the second case, as a user I don't want to get a convergence warning.

@ogrisel ogrisel Nov 16, 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.

I agree that if the iteration is stopped by the callback configuration to implement some early stopping logic, this is on purpose and therefore we should not raise ConvergenceWarning.

I think ConvergenceWarning should only be raised when we fail to converge to the requested tol level within a given max_iter budget and without any custom early-stopping logic enabled.

@betatim

betatim commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

My friend Claude pointed out this:

import warnings

from sklearn.callback.tests._common.callbacks import StopFitCallback
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression

X, y = load_iris(return_X_y=True)
cb = StopFitCallback()
with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    LogisticRegression(solver="lbfgs").set_callbacks(cb).fit(X, y)

for r in cb.record:
    print(f"{r['name']:<18} {r['context'].task_name:<11} ctx={id(r['context'])}")

# Every begin should have exactly one matching end.
assert cb.count_hooks("on_fit_task_begin") == cb.count_hooks("on_fit_task_end")

If I run this locally on this PR I indeed see two ends for the same context:

setup              fit         ctx=5369234832
on_fit_task_begin  fit         ctx=5369234832
on_fit_task_begin  lbfgs-iter  ctx=4426495824
on_fit_task_end    lbfgs-iter  ctx=4426495824
on_fit_task_end    lbfgs-iter  ctx=4426495824
on_fit_task_end    fit         ctx=5369234832
teardown           fit         ctx=5369234832

The first lbfgs-iter end comes from __call__, just before it raises StopIteration. The second comes from close(), which assumes _iter_ctx is always a fresh context that hasn't run yet. That stops being true once the fit is stopped. A minimal fix is to set self._iter_ctx = None before raising, and have close() return early when it's None.

The begin/end count assertion would also be worth adding to test_logistic_regression_fit_stopped_by_callback.

Something about "The two hardest things in software: cache invalidation, naming, and off by one errors" :D

@lesteve

lesteve commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

I am going to merge this one because this closes the deprecation for 1.11 issue. I haven't spent enough time trying to understand what the likely bug is about. I would encourage you to open a separate issue to track it 🙏. If you think this is important for 1.10, then add the 1.10 milestone to this new issue 😉.

@lesteve
lesteve enabled auto-merge (squash) November 18, 2026 07:44
@lesteve
lesteve merged commit a6ab448 into scikit-learn:main Nov 18, 2026
55 of 57 checks passed
@lorentzenchr

Copy link
Copy Markdown
Member

@lesteve The merge feels a bit rushed. My review comment about the warning is over a week old and not addressed.

@betatim

betatim commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

I agree. There are two problems that need addressing here that are related to making this feature work. It is a deprecation but not just. It adds new functionality, at least that is how I understand the PR.

One open question is Christian's comment about when there should/shouldn't be a warning. The second open question is a bug in the callback handling (there should only be one "on_fit_task_end" event for the bfgs context)

@ogrisel

ogrisel commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

I think we should revert the change and remove the TODO(1.10) marker in the comment since this is indeed a new feature and it should be properly reviewed, tested and documented in the changelog as such.

To me this new feature is a very nice to have but not a 1.10 release blocker.

@ogrisel

ogrisel commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

I will open the revert PR.

EDIT: done in #35449.

@ogrisel

ogrisel commented Nov 18, 2026

Copy link
Copy Markdown
Member

To me this new feature is a very nice to have but not a 1.10 release blocker.

Actually, I don't know if it's a new features or a fix, but it's not a release blocker in any case and I think that if we merge a fix we should merge it as a complete fix (without the ConvergenceWarning).

@betatim

betatim commented Nov 18, 2026

Copy link
Copy Markdown
Member

I'm not sure we need to revert this. For me it would be fine to ship this because it is useful.

We should merge #35447 which I think is "just" a bug fix.

Then we can either merge #35448 or an alternative that does something else with respect to the ConvergenceWarning.

@lesteve

lesteve commented Nov 19, 2026

Copy link
Copy Markdown
Member

Oops looks like I was a bit too trigger-happy on this one 😓 but then work happened and it seems like this is under control, so all in all may be a good thing 🙄🤔.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module:linear_model No Changelog Needed Quick Review For PRs that are quick to review size/S Waiting for Second Reviewer First reviewer is done, need a second one!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MNT Carry out deprecations for the 1.10 scikit-learn release

6 participants