You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support early_stopping with custom validation_set #18748
Today in SGDClassifier, the parameter early_stopping uses a fraction of the data randomly, it would be useful to support a custom validation set chosen by the user.
I agree this could be valuable. (see Nicolas' comment)
Currently, the validation split is implemented as a mask array validation_mask, which is used to define X_val = X[validation_mask], and X_train = X[1 - validation_mask]. We could thus avoid adding a new parameters by accepting a predefined mask array in the parameter validation_fraction.
We would need to:
update _make_validation_split with an early return if self.validation_fraction is an array
validate the array: cast it to uint8 dtype, check that its length matches X's
I think we've tried to avoid sample-aligned init parameters so far: passing a sampled-aligned mask in __init__ would mean that the estimator wouldn't be usable in any cross-validation helper, like cross_val_score or GridSearchCV.
For ref there were related discussions in #15127 and #5915
The issue I have with passing validation_data to models' fit is that I'm not sure how we'll manage it when the model is in a pipeline. The validation_data needs to go through the same preprpocessing steps of a pipeline as the rest of the data. The user thus cannot set aside a part of the data and pass it directly to the model at the end of a pipeline.
In many of the discussions I see here and elsewhere, people seem to not have that requirement?
+1, would find this very useful across the different models that support early stopping validation.
TomDLT's idea of using indices seems like it could address adrinjalali's concern of preprocessing since it would still be split off from the training data once it gets to the model. That, or providing group labels and supporting GroupShuffleSplit if the user provides group labels to fit.
I have data where points from each group are highly correlated. I'm using group splitters to validate outside of training, but since the training data is randomly split for validation (in my case by HistGradientBoostingClassifier), early stopping doesn't work as intended.
Of course, understanding that this has a lot of consequences outside of just these models, since sklearn has a consistent API for fit, but it would be very useful :)
+1, would find this feature very useful. In my work we have hand crafted train and validation set, and ideally we want to stop training once performance on validation set becomes worse for some number of iters.
I guess we could think of an API where pipeline's fit accepts a validation_set, as well as many other estimators and all other meta-estimators, and handle that properly. But that's a quite a large project to pull off.
Reacted by Seth Bradford Wagenman and Christian Lorentzen
Could we start a PR and work on this collectively? I still need to understand how sklearn API works in general but would love to learn more and contribute!
Could we start a PR and work on this collectively? I still need to understand how sklearn API works in general but would love to learn more and contribute!
Given the complexity of the issue, I would start with proposing an API here first after learning about scikit-learn's API. Resolving this issue involves adding new API to estimators that has early stopping and requires thinking about how meta-estimators like pipeline interacts with this new API.
Side Note: skorch.net.NeuralNet, has a train_split__init__ parameter, which is a callable that splits the data into train & validation sets. For predefined splits, there is a predefined helper, which allows for custom splits. For this workflow to integrate nicely with pipelines, the Pipeline would also need a train_split parameter, do the correct preprocessing on the validation set, and pass that information into a train_split__init__ parameter in the last step.
Would we allow the same validation for transformers as well? Imagine an encoder at the first step of the pipeline, which would have its own early stopping criteria. Is the validation set used for that step the same as the validation set used for the last step of the pipeline?
But you can't put any preprocessing in a pipeline and pass that to the grid search, since in grid search the preprocessing steps are re-fit with new parameters if we're tuning on them, and then X_eval is not transformed the way it should be.
I think that's already a good step, but we'd need to also modify pipeline to handle things which are supposed to be transformed before feeding to next steps.
The consensus of a longer discussion at the drafting meeting 19.01.2024 was to go with passing splitter objects (option 2) as parameters to the estimator (e.g. HistGradientBoosting*).
To recap, discussed options were:
Number
Option
Pro
Con
1
CV-etsimators like LogisticRegressionCV
ugly & maintenance
2
Pass a splitter as constructor argument
works
some data leakage
3
Passing a pre-computed validation dataset X_val, y_val
works
X_val can't be processed/created in a pipeline
4
Use a callback
might work
not yet accepted/implemented
Problem
1
2
3
4
split on a column that should not be used for fitting
To be more precise regarding the above chart, callbacks are not an alternative to provide the validation set. In order to have interesting callbacks (early stopping, monitoring, ...), the validation set must but accessible when the callback is called. However the solution we chose for a unified API to provide a validation is kind of orthogonal to callbacks API.
Then, once this question is solved, an early stopping callback will be imo a good solution to have a consistent API for early stopping across all estimators, instead of each estimator implementing its own version of early stopping.
I disagree with any of the "works" above. If you have a pipeline that consists of feature selection and an estimator, the feature selection will include the validation data, leading to potentially very misleading results.
The only solution I can see is adding a mechanism to Pipeline that knows how to treat a validation set, potentially piggy-backing on a modified version of the meta-data routing.
That's captured in the "Problem" table, but "having some data leakage" in validation doesn't seem acceptable to me. ps I really wanted to make the draft meeting this week but I've been sick in and out of bed the whole week :(
The only solution I can see is adding a mechanism to Pipeline that knows how to treat a validation set, potentially piggy-backing on a modified version of the meta-data routing.
but "having some data leakage" in validation doesn't seem acceptable to me
Yet it's how it's been done in scikit-learn until now (HGBT, SGD, ...). But I agree that it does not prevent us from finding a better solution :)
However, the better solution involves an evolution of the API and a lot of work so it should be clearly motivated. That is show a concrete realistic example where the data leakage coming from the preprocessing is really detrimental. We discussed that irl with @glemaitre but I haven't started yet.
ps I really wanted to make the draft meeting this week but I've been sick in and out of bed the whole week :(
Let's discuss it in the next one on march 22nd. I'll send a mail on the mailing list to make others aware.
We could also take the opportunity to discuss the callback API with the support of the newly writen slep if you'd like.
@amueller Are we talking about the same problem? In my understanding, the issue here is early stopping in the final estimator of a pipeline, not ES in a preprocessing step before that, nor an unbiased estimate of the out-of-sample performance à la cross validation. Sure, the preprocessing has an effect on the final estimator, but ES should just avoid overfitting or spare resources of the final estimator. As @betatim stated, #28440 (comment), as long as the validation curve is only y-shifted, everything is fine.
Also sure, a mechanism to tell a pipeline to pass validation data through it is a missing piece in our API (even after metadata routing) and would solve this issue in the methodological soundest way (or not sound at all, we just put the burden on the user). It was proposed by @adrinjalali, #28440 (comment), and, IMHO, should be proposed in an own issue or even in a SLEP.
@lorentzenchr Sorry I was busy and missed the March 22nd draft meeting (which is also somewhat inconvenient for my 7am but I would have made it if I'd known this was the issue discussed).
And I don't see why the validation curve would be y-shifted only. I have a ICML rebuttal deadline this week, so I won't have time to develop the example this week, but in essence it's the pipeline that @thomasjpfan proposed. Early stopping is a hyper-parameter selection problem. Whether the validation set was considered in the feature selection or not creates completely different validation sets for the hyper-parameter selection problem, and I don't see how they would be related in this case.
Yet it's how it's been done in scikit-learn until now (HGBT, SGD, ...). But I agree that it does not prevent us from finding a better solution :)
It's been done that way in single estimators, and was impossible to do in pipelines. I.e. the issue we're talking about was absent because doing the wrong thing was impossible. Doing the right thing was also impossible, though.
After further longer discussions, we arrived at #27124, i.e. introduce new arguments to fit: X_val, y_val. In my opinion and understanding, this is now the way to go and should apply to all estimators with early stopping.
I think we are at risk of creating a muddled UX because multiple ways of (possibly) achieving the same thing are emerging. From my perspective callbacks are the way to do this. By providing a callback that decides "should we stop or not?" the user can implement what ever rule they want, including using a validation dataset. Something I've not quite figure out yet (but also not thought about for more than 2min) is how you determine what is in your validation dataset and how all this interacts with cross validation. But I assume someone who worked on callbacks has done this/made sure it is possible.
Adding more parameters to fit() feels ugly. More arguments is not pretty (for my definition of pretty) and I think you'd need more API somewhere to deal with deciding how the validation dataset should be use. I think wrapping this up in a callback is nicer.
Similarly, with metadata routing you can deliver the data, but then what? The stopping rule still needs configuring somehow.
Activity
TomDLT commentedon Nov 19, 2020
I agree this could be valuable.(see Nicolas' comment)Currently, the validation split is implemented as a mask array
validation_mask, which is used to defineX_val = X[validation_mask], andX_train = X[1 - validation_mask]. We could thus avoid adding a new parameters by accepting a predefined mask array in the parametervalidation_fraction.We would need to:
_make_validation_splitwith an early return ifself.validation_fractionis an arrayDo you want to give it a try ?
NicolasHug commentedon Nov 19, 2020
I think we've tried to avoid sample-aligned
initparameters so far: passing a sampled-aligned mask in__init__would mean that the estimator wouldn't be usable in any cross-validation helper, likecross_val_scoreorGridSearchCV.For ref there were related discussions in #15127 and #5915
TomDLT commentedon Nov 19, 2020
Makes sense, forgot about this consideration.
adrinjalali commentedon Nov 21, 2020
The issue I have with passing
validation_datato models'fitis that I'm not sure how we'll manage it when the model is in a pipeline. Thevalidation_dataneeds to go through the same preprpocessing steps of a pipeline as the rest of the data. The user thus cannot set aside a part of the data and pass it directly to the model at the end of a pipeline.In many of the discussions I see here and elsewhere, people seem to not have that requirement?
TomDLT commentedon Nov 21, 2020
Scikit-learn's API is centered around easy cross-validation, but I guess a lot of people use their own cross-validation tools, for better or worse.
docmarionum1 commentedon Sep 23, 2021
+1, would find this very useful across the different models that support early stopping validation.
TomDLT's idea of using indices seems like it could address adrinjalali's concern of preprocessing since it would still be split off from the training data once it gets to the model. That, or providing group labels and supporting
GroupShuffleSplitif the user provides group labels tofit.I have data where points from each group are highly correlated. I'm using group splitters to validate outside of training, but since the training data is randomly split for validation (in my case by
HistGradientBoostingClassifier), early stopping doesn't work as intended.Of course, understanding that this has a lot of consequences outside of just these models, since sklearn has a consistent API for
fit, but it would be very useful :)eddiewu01 commentedon Oct 21, 2021
+1, would find this feature very useful. In my work we have hand crafted train and validation set, and ideally we want to stop training once performance on validation set becomes worse for some number of iters.
adrinjalali commentedon Oct 22, 2021
I guess we could think of an API where pipeline's
fitaccepts avalidation_set, as well as many other estimators and all other meta-estimators, and handle that properly. But that's a quite a large project to pull off.eddiewu01 commentedon Oct 22, 2021
Could we start a PR and work on this collectively? I still need to understand how sklearn API works in general but would love to learn more and contribute!
thomasjpfan commentedon Oct 23, 2021
Given the complexity of the issue, I would start with proposing an API here first after learning about scikit-learn's API. Resolving this issue involves adding new API to estimators that has early stopping and requires thinking about how meta-estimators like pipeline interacts with this new API.
Side Note: skorch.net.NeuralNet, has a
train_split__init__parameter, which is a callable that splits the data into train & validation sets. For predefined splits, there is a predefined helper, which allows for custom splits. For this workflow to integrate nicely with pipelines, thePipelinewould also need atrain_splitparameter, do the correct preprocessing on the validation set, and pass that information into atrain_split__init__parameter in the last step.adrinjalali commentedon Oct 25, 2021
Would we allow the same validation for transformers as well? Imagine an encoder at the first step of the pipeline, which would have its own early stopping criteria. Is the validation set used for that step the same as the validation set used for the last step of the pipeline?
6 remaining items
adrinjalali commentedon Aug 3, 2023
So with metadata routing this would work:
But you can't put any preprocessing in a pipeline and pass that to the grid search, since in grid search the preprocessing steps are re-fit with new parameters if we're tuning on them, and then
X_evalis not transformed the way it should be.I think that's already a good step, but we'd need to also modify pipeline to handle things which are supposed to be transformed before feeding to next steps.
Does that make sense?
lorentzenchr commentedon Aug 8, 2023
@adrinjalali Thanks. Yes, that makes sense. If nobody else is working on it or intents to do so, I'll soon open a PR for HGBT.
lorentzenchr commentedon Feb 17, 2024
The consensus of a longer discussion at the drafting meeting 19.01.2024 was to go with passing splitter objects (option 2) as parameters to the estimator (e.g. HistGradientBoosting*).
To recap, discussed options were:
X_val, y_valX_valcan't be processed/created in a pipelineX_valrequires same preprocessing as "X_train"jeremiedbb commentedon Feb 29, 2024
To be more precise regarding the above chart, callbacks are not an alternative to provide the validation set. In order to have interesting callbacks (early stopping, monitoring, ...), the validation set must but accessible when the callback is called. However the solution we chose for a unified API to provide a validation is kind of orthogonal to callbacks API.
Then, once this question is solved, an early stopping callback will be imo a good solution to have a consistent API for early stopping across all estimators, instead of each estimator implementing its own version of early stopping.
amueller commentedon Mar 8, 2024
I disagree with any of the "works" above. If you have a pipeline that consists of feature selection and an estimator, the feature selection will include the validation data, leading to potentially very misleading results.
The only solution I can see is adding a mechanism to
Pipelinethat knows how to treat a validation set, potentially piggy-backing on a modified version of the meta-data routing.That's captured in the "Problem" table, but "having some data leakage" in validation doesn't seem acceptable to me. ps I really wanted to make the draft meeting this week but I've been sick in and out of bed the whole week :(
jeremiedbb commentedon Mar 8, 2024
This is proposed here #28440 (comment)
Yet it's how it's been done in scikit-learn until now (HGBT, SGD, ...). But I agree that it does not prevent us from finding a better solution :)
However, the better solution involves an evolution of the API and a lot of work so it should be clearly motivated. That is show a concrete realistic example where the data leakage coming from the preprocessing is really detrimental. We discussed that irl with @glemaitre but I haven't started yet.
Let's discuss it in the next one on march 22nd. I'll send a mail on the mailing list to make others aware.
We could also take the opportunity to discuss the callback API with the support of the newly writen slep if you'd like.
lorentzenchr commentedon Mar 8, 2024
@amueller Are we talking about the same problem? In my understanding, the issue here is early stopping in the final estimator of a pipeline, not ES in a preprocessing step before that, nor an unbiased estimate of the out-of-sample performance à la cross validation. Sure, the preprocessing has an effect on the final estimator, but ES should just avoid overfitting or spare resources of the final estimator. As @betatim stated, #28440 (comment), as long as the validation curve is only y-shifted, everything is fine.
Also sure, a mechanism to tell a pipeline to pass validation data through it is a missing piece in our API (even after metadata routing) and would solve this issue in the methodological soundest way (or not sound at all, we just put the burden on the user). It was proposed by @adrinjalali, #28440 (comment), and, IMHO, should be proposed in an own issue or even in a SLEP.
thomasjpfan commentedon Mar 22, 2024
The issue around feature selection in a pipeline is described here: #28440 (comment). Concretely, something like this:
amueller commentedon Mar 25, 2024
@lorentzenchr Sorry I was busy and missed the March 22nd draft meeting (which is also somewhat inconvenient for my 7am but I would have made it if I'd known this was the issue discussed).
And I don't see why the validation curve would be y-shifted only. I have a ICML rebuttal deadline this week, so I won't have time to develop the example this week, but in essence it's the pipeline that @thomasjpfan proposed. Early stopping is a hyper-parameter selection problem. Whether the validation set was considered in the feature selection or not creates completely different validation sets for the hyper-parameter selection problem, and I don't see how they would be related in this case.
@jeremiedbb:
It's been done that way in single estimators, and was impossible to do in pipelines. I.e. the issue we're talking about was absent because doing the wrong thing was impossible. Doing the right thing was also impossible, though.
ogrisel commentedon Aug 1, 2024
Cross-linking to a PR that stemmed from the discussion in #28440:
lorentzenchr commentedon Aug 20, 2025
After further longer discussions, we arrived at #27124, i.e. introduce new arguments to fit:
X_val, y_val. In my opinion and understanding, this is now the way to go and should apply to all estimators with early stopping.andreabosisio commentedon Jan 19, 2026
I’m looking for the same feature for the MLPClassifier
lorentzenchr commentedon Nov 15, 2026
Update as of 1.9
Meanwhile, we have
X_val,y_valandsample_weight_valas arguments offit, only for HGBT.Question
X_val,y_valto further estimators likeGradientBoostingClassifier,RandomForestClassifier,LogisticRegression?@scikit-learn/core-devs friendly ping for the interested ones
adam2392 commentedon Nov 15, 2026
I'd be in favor of adding to RF.
I guess I don't see why not if we have it in HGBT?
betatim commentedon Dec 2, 2026
I think we are at risk of creating a muddled UX because multiple ways of (possibly) achieving the same thing are emerging. From my perspective callbacks are the way to do this. By providing a callback that decides "should we stop or not?" the user can implement what ever rule they want, including using a validation dataset. Something I've not quite figure out yet (but also not thought about for more than 2min) is how you determine what is in your validation dataset and how all this interacts with cross validation. But I assume someone who worked on callbacks has done this/made sure it is possible.
Adding more parameters to
fit()feels ugly. More arguments is not pretty (for my definition of pretty) and I think you'd need more API somewhere to deal with deciding how the validation dataset should be use. I think wrapping this up in a callback is nicer.Similarly, with metadata routing you can deliver the data, but then what? The stopping rule still needs configuring somehow.