Skip to content

Support early_stopping with custom validation_set #18748

Open

Description

@deltawi

Describe the workflow you want to enable

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.

Describe your proposed solution

for example:

clf = SGDClassifier(early_stopping=True)
clf.fit(X_train, y_train, eval_set=(X_val, y_val))

EDIT

Broader Scope

Same applies to GradientBoosting* and HistGradientBoosting*

Activity

  1. TomDLT commented on Nov 19, 2020

    @TomDLT
    Member

    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
    • update the documentation
    • add a test

    Do you want to give it a try ?

  2. NicolasHug commented on Nov 19, 2020

    @NicolasHug
    Member

    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

  3. TomDLT commented on Nov 19, 2020

    @TomDLT
    Member

    Makes sense, forgot about this consideration.

  4. adrinjalali commented on Nov 21, 2020

    @adrinjalali
    Member

    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?

  5. TomDLT commented on Nov 21, 2020

    @TomDLT
    Member

    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.

  6. docmarionum1 commented on Sep 23, 2021

    @docmarionum1

    +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 :)

  7. eddiewu01 commented on Oct 21, 2021

    @eddiewu01

    +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.

  8. adrinjalali commented on Oct 22, 2021

    @adrinjalali
    Member

    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.

  9. eddiewu01 commented on Oct 22, 2021

    @eddiewu01

    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!

  10. thomasjpfan commented on Oct 23, 2021

    @thomasjpfan
    Member

    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.

  11. adrinjalali commented on Oct 25, 2021

    @adrinjalali
    Member

    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?

  12. 6 remaining items

  13. adrinjalali commented on Aug 3, 2023

    @adrinjalali
    Member

    So with metadata routing this would work:

    X, y = load_iris()
    X, y, X_eval, y_eval = train_test_split(X, y)
    
    preprocessing = Pipeline(...) # preprocessing steps
    X = preprocessing.fit_transform(X, y)
    X_eval = preprocessing.transform(X_eval)
    
    gs = GridSearchCV(
        HistGradientBoostingClsasifier().set_fit_request(X_eval=True, y_eval=True),
        ...
    )
    
    gs.fit(X, y, X_evel=X_eval, y_eval=y_eval)

    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.

    Does that make sense?

  14. lorentzenchr commented on Aug 8, 2023

    @lorentzenchr
    Member

    @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.

  15. lorentzenchr commented on Feb 17, 2024

    @lorentzenchr
    Member

    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 no yes via metadata-routing yes ?
    X_val requires same preprocessing as "X_train" yes yes no ?
    data leakage some some depends ?
  16. jeremiedbb commented on Feb 29, 2024

    @jeremiedbb
    Member

    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.

  17. amueller commented on Mar 8, 2024

    @amueller
    Member

    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 :(

  18. jeremiedbb commented on Mar 8, 2024

    @jeremiedbb
    Member

    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.

    This is proposed here #28440 (comment)

    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.

  19. lorentzenchr commented on Mar 8, 2024

    @lorentzenchr
    Member

    @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.

  20. thomasjpfan commented on Mar 22, 2024

    @thomasjpfan
    Member

    The issue around feature selection in a pipeline is described here: #28440 (comment). Concretely, something like this:

    pipe = make_pipeline(
        SequentialFeatureSelector(...),
        HistGradientBoostingClassifier(early_stopping=True),
    )
  21. amueller commented on Mar 25, 2024

    @amueller
    Member

    @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:

    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.

  22. ogrisel commented on Aug 1, 2024

    @ogrisel
    Member

    Cross-linking to a PR that stemmed from the discussion in #28440:

  23. lorentzenchr commented on Aug 20, 2025

    @lorentzenchr
    Member

    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.

  24. andreabosisio commented on Jan 19, 2026

    @andreabosisio

    I’m looking for the same feature for the MLPClassifier

  25. lorentzenchr commented on Nov 15, 2026

    @lorentzenchr
    Member

    Update as of 1.9

    Meanwhile, we have

    Question

    • Should we go on and add X_val, y_val to further estimators like GradientBoostingClassifier, RandomForestClassifier, LogisticRegression?
    • Should we better reconsider and use a new callback for this?

    @scikit-learn/core-devs friendly ping for the interested ones

  26. adam2392 commented on Nov 15, 2026

    @adam2392
    Member

    I'd be in favor of adding to RF.

    I guess I don't see why not if we have it in HGBT?

  27. betatim commented on Dec 2, 2026

    @betatim
    Member

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions