[Callbacks] FEA Add callbacks when cloning an estimator#33490
[Callbacks] FEA Add callbacks when cloning an estimator#33490StefanieSenger wants to merge 3 commits intoscikit-learn:callbacksfrom
Conversation
|
Closing, because I have only now seen that we had already done this change - new_object._skl_callbacks = clone(estimator._skl_callbacks, safe=False)
+ new_object._skl_callbacks = estimator._skl_callbacksin https://github.com/scikit-learn/scikit-learn/pull/33394/changes#r2845852984 (Obviously I have worked on an older version of the callback branch for a few days without realising that it had updated. 🫤 Still, the question is: why not simply leave it be like it is. |
The current behavior of clone is temporary. In the mean time we decided that clone needs to not keep the callbacks to prevent crashes when an estimator with a callback is passed to a meta-estimator (or function) that doesn't support callbacks. |
|
Thanks, @jeremiedbb.
I'm sorry but I don't know what you mean. To my understanding, the goal of changing the cloning behaviour is preventing from sklearn.datasets import make_classification
from sklearn.base import clone
from sklearn.callback import ProgressBar
from sklearn.callback.tests._utils import MaxIterEstimator
X, y = make_classification(n_samples=1000, random_state=42)
est = MaxIterEstimator().set_callbacks(ProgressBar()).fit(X, y)
clone(est)Also, #33421 does allow the behavior of your fix in #33394 by passing estimator = MaxIterEstimator().set_callbacks(TestingCallback())
def fit_one(estimator):
estimator = clone(estimator, keep_callbacks=True)
estimator.fit()
def my_function(estimator, n_fits=4):
"""A function cloning and fitting an estimator in parallel."""
Parallel(n_jobs=1)(delayed(fit_one)(estimator) for _ in range(n_fits))
my_function(estimator)Everything works fine and does not crash, even though an estimator with a callback is passed to function that doesn't support callbacks. This is why I believe - new_object._skl_callbacks = clone(estimator._skl_callbacks, safe=False)
+ new_object._skl_callbacks = estimator._skl_callbacksand maybe adding some documentation and testing is all we need. |
maybe it doesn't crash for TestingCallback, but it doesn't mean that it worked (and it may crash with a different callback). For instance, if you use a progressbar instead, I expect that (if it doesn't crash) |
|
Thank you, @jeremiedbb. I appreciate your answer.
It also doesn't crash for
I need to think more about this. Without callback support in No rush, but we should know what exactly would crash / not work as expected before merging a solution, because we need to add this as a test. It doesn't need to be before the vote on the SLEP or even before the release. |
Reference Issues/PRs
Towards #33323
Alternative to #33340 and #33421.
What does this implement/fix? Explain your changes.
Fixes
clone()if an estimator comes with a callback.Opening this PR for completeness, because we had forgotten to discuss this simple alternative.
AI usage disclosure
I used AI assistance for: