Repository navigation
MNT Enable stopping LogisticRegression by a callback - #35334
Conversation
| # lr = LogisticRegression(solver="lbfgs").set_callbacks(cb) | ||
| # lr.fit(X, y) | ||
| # assert lr.n_iter_ == 1 | ||
| @ignore_warnings(category=ConvergenceWarning) |
There was a problem hiding this comment.
Should LR raise a convergence warning when the callback stops the fit?
There was a problem hiding this comment.
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 :-/
There was a problem hiding this comment.
At least for the second case, as a user I don't want to get a convergence warning.
There was a problem hiding this comment.
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.
|
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: The first The begin/end count assertion would also be worth adding to Something about "The two hardest things in software: cache invalidation, naming, and off by one errors" :D |
|
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 The merge feels a bit rushed. My review comment about the warning is over a week old and not addressed. |
|
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) |
|
I think we should revert the change and remove the To me this new feature is a very nice to have but not a 1.10 release blocker. |
|
I will open the revert PR. EDIT: done in #35449. |
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 |
|
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 🙄🤔. |
Last part of and closes #35252.
The minimum version of scipy is now able to handle StopIteration in the callback for LBFGS.
ping @betatim