Skip to content

Callbacks x Metadata-routing #34068

Open
Open

Description

@jeremiedbb

Warning

This issue is not yet ready for a PR. If you are interested in contributing to scikit-learn, please have a look at our contributing guidelines, and in particular the sections for new contributors and the "Needs triage" label.

There are 2 (related) things to figure out to enable callbacks to be compatible with metadata-routing. I think it's the first thing to solve before starting to implement callback support in more estimators.

  • Do we want to allow passing arbitrary metadata to the callbacks ?

    my_business_metric.set_score_request(amount=True)
    callback = ScoringMonitor(scoring=my_business_metric)
    LogisticRegression().set_callbacks(callback).fit(X, y, amount=amount)

    I'm not sure that it's very interesting for the ScoringMonitor. Do we think that anyone is interested in the evolution of a business metric during fit ?
    Same question for early stopping. Is it something that people do ?
    That being said, even if not useful for these callbacks, that may be useful for custom callbacks that I haven't thought about.

    What does that require ?
    Basically make every estimator a router. And callbacks as well.
    That's not a major difficulty but it means that implementing callback support is tied to metadata-routing if you want full capacity (it might be possible to make it optional and then callbacks would be limited when there's no metadata-routing, as it's the case now)

  • How to handle X_val ?
    Assuming that we do the above point, we're facing a major issue with how X_val is treated right now, that is as regular metadata. Consider the following:

    my_business_metric.set_score_request(amount=True)
    callback = ScoringMonitor(scoring=my_business_metric, eval_on="val")
    LogisticRegression().set_callbacks(callback).fit(X, y, X_val=X_val, y_val=y_val, amount_train=amount, amount_val=amount_val)

    LogisticRegression is not a consumer of amount: it has no idea which one is for which set. In fact amount_train is not even useful here so I could have just passed amount=amount_val.

    X_val and y_val themselves could arguably be consumed by the estimator (at least there are estimators that transform X so X_val should follow).

    Another thing is: if we request amount on the scorer then how do we sort out how amount_train and amount_val are forwarded to the scorer ?

    All of it comes to me from the fact that X_val is considered regular metadata while it's not really metadata, it's more a secondary dataset with possibly associated metadata. I'm under the impression that the solution lies in the metadata-routing framework rather than in the callbacks, by differentiating from the start metadata associated the train set and metadata associated to the validation set.

ping @FrancoisPgm @StefanieSenger @adrinjalali @ogrisel

Activity

  1. added
    Metadata Routing
    on May 20, 2026
  2. moved this to Todo in Labson May 20, 2026
  3. StefanieSenger commented on May 21, 2026

    @StefanieSenger
    Member

    Do we want to allow passing arbitrary metadata to the callbacks ?

    Yes, I think we have to. If users can route metadata to callbacks, it is allowed to pass any metadata that can get consumed. Also, this makes custom callbacks more attractive.

    I'm not sure that it's very interesting for the ScoringMonitor. Do we think that anyone is interested in the evolution of a business metric during fit ?

    We don't need to change ScoringMonitor for that or allow custom metrics in early stopping . We could constrain that it only works with certain metrics / scikit-learn metrics, or decide about this later. In any case, how we decide about this wouldn't change how we implement metadata routing.

    (it might be possible to make it optional and then callbacks would be limited when there's no metadata-routing, as it's the case now)

    It is much simpler not making it optional. The current implementation could pass whatever LogisticRegression.fit knows, with metadata routing, users could set specific requests and the results could differ. The current passing is not less, but different.

    LogisticRegression is not a consumer of amount: it has no idea which one is for which set. In fact amount_train is not even useful here so I could have just passed amount=amount_val.

    It seems here we would require the user to filter their sample_weight or their amount for the same indices as X_val and only pass this, which is not so ideal and error prone.

    All of it comes to me from the fact that X_val is considered regular metadata while it's not really metadata, it's more a secondary dataset with possibly associated metadata. I'm under the impression that the solution lies in the metadata-routing framework rather than in the callbacks, by differentiating from the start metadata associated the train set and metadata associated to the validation set.

    X_val, y_val and sample_weight_val are only implemented for HistGradientBoosting so far and as a criterion for early stopping only if I understand correctly. Do you think an EarlyStopping callback could fully substitute this?

  4. jeremiedbb commented on May 21, 2026

    @jeremiedbb
    MemberAuthor

    It seems here we would require the user to filter their sample_weight or their amount for the same indices as X_val and only pass this, which is not so ideal and error prone.

    Indeed, that's why we need to figure out a robust and generic solution.

    X_val, y_val and sample_weight_val are only implemented for HistGradientBoosting so far and as a criterion for early stopping only if I understand correctly. Do you think an EarlyStopping callback could fully substitute this?

    So far, but the goal is to add it to more or less all estimators.

    Do you think an EarlyStopping callback could fully substitute this?

    yes. And early stopping is really interesting only if we can do it on a validation set

  5. jeremiedbb commented on May 21, 2026

    @jeremiedbb
    MemberAuthor

    A first idea that could make things simpler: 2 scorers instead of eval_on parameter

    class ScoringMonitor:
        def __init__(self, *, scoring_train, scoring_val)
    
    train_scorer = make_scorer(func).set_score_request(amount="amount_train")
    val_scorer = make_scorer(func).set_score_request(amount="amount_val")
    
    callback = ScoringMonitor(scoring_train=train_scorer, scoring_val=val_scorer)
    
    LogisticRegression().set_callbacks(callback).fit(X, y, X_val=X_val, y_val=y_val, amount_train=amount_train, amount_val=amount_val)
  6. StefanieSenger commented on May 21, 2026

    @StefanieSenger
    Member

    Do you think an EarlyStopping callback could fully substitute this?

    yes. And early stopping is really interesting only if we can do it on a validation set

    I'm dropping some thoughts, that you probably have solved for yourself before, but I'm not there yet. 😅

    1. If we'd drop the condition that X_val needs to be user-configurable, then we could split off X_val, y_val and metadata_val from X, y and metadata when running the callback hooks. Or is it not justifiable for users to get a slightly different training result than as if this training would run on the full dataset and end after the same number of iterations? Would that even matter if we documented this?

    2. Users could instead pass X_val, y_val and sample_weight_val into the the early stopping callback at init (not into estimator.fit) and we could make it reproduce all the transform steps on this new data on the first hook call and then store it on the callback temporarily. (My fantasy taking over, not sure if this is in fact all possible the way I imagine it.) That would remove it from the metadata routing.

  7. jeremiedbb commented on May 21, 2026

    @jeremiedbb
    MemberAuthor

    If we'd drop the condition that X_val needs to be user-configurable, then we could split off X_val, y_val and metadata_val from X, y and metadata when running the callback hooks.

    Are you suggesting that the estimator itself splits X into X_train and X_val ? There's a major issue with that: If X goes through preprocessing before entring the estimator, you're leaking data. Splitting should always be done before any action on the dataset. (that's why transform_input was added to pipeline before X_val to HGBT)

    Users could instead pass X_val, y_val and sample_weight_val into the the early stopping callback at init (not into estimator.fit) and we could make it reproduce all the transform steps on this new data on the first hook call and then store it on the callback temporarily

    That was my first thought when I started to work on callbacks but I don't see how that would be possible. Suppose you have a pipeline with preprocessing steps and a final estimator. You need the X_val in the callback to have been transformed exactly the same way as X was transformed in the pipeline. Also some estimators internally transform X and it has to be mimicked (linear models can center X for instance)

  8. jeremiedbb commented on May 21, 2026

    @jeremiedbb
    MemberAuthor

    Maybe X_val could as well be requested by the scorer ? One issue I have so far is that there's no link between X_val and the metadata to use alongside X_val. If you take the example above (#34068 (comment)), the callback has no idea that it should evaluate the score with X_val and amount_val. It only knows that it should pass amount_val to the scorer.

    class ScoringMonitor:
        def __init__(self, *, scoring_train, scoring_val)
    
    train_scorer = make_scorer(func).set_score_request(amount="amount_train")
    val_scorer = make_scorer(func).set_score_request(X="X_val", y="y_val", amount="amount_val")
    
    callback = ScoringMonitor(scoring_train=train_scorer, scoring_val=val_scorer)
    
    LogisticRegression().set_callbacks(callback).fit(X, y, X_val=X_val, y_val=y_val, amount_train=amount_train, amount_val=amount_val)

    This way, the scorer requests everything it needs, and X_val with its associated metadata are tied together from the start. Then I guess the current metadata-routing framework should be able to handle it with (almost ?) no structural change.

  9. jeremiedbb commented on May 21, 2026

    @jeremiedbb
    MemberAuthor

    One thing to consider with that setting is that some estimators internally transform X. So either

    • they have X_val as explicit param, are considered consumers of X_val, and transform it as well internally. It means that X_val is special and that callbacks can only work with X_val (no aliases).
    • they are not consumers but then we need a transform_input parameter, like in Pipeline, to tell the estimator to apply the internal transformation on a given dataset. It adds complexity but enables aliasing.
  10. adrinjalali commented on May 21, 2026

    @adrinjalali
    Member

    As for scorers and ScoringMonitor, our scorers already accept metadata (sample_weight), and it shouldn't matter which metadata they request, so supporting metadata for scorers seems quite necessary to me. And we can't even special case sample_weight+scoring, since we don't know where we should forward metadata and where not. Metadata routing solves this.

    Solving this for all estimators is not too hard, since we can put this on the _MetadataRequester / BaseEstimator level.

    As for validation, I was thinking along the same lines as your suggestion here: #34068 (comment)

    Then it's the job of the estimator to know if there's any validation set to give to the ScoringMonitor, and if yes, how to transform the data / etc.

    So both these issues are solved to me (in theory), and there's no way around making estimators which support callback a router.

  11. jeremiedbb commented on May 21, 2026

    @jeremiedbb
    MemberAuthor

    #34068 (comment) can't be the complete picture because the estimator can't know which metadata is associated with which dataset. The estimator is not a consumer of amount_train/val, it just passes it forward. Even if it's a consumer of X_val, it can't make the link with the right metadata. That's why I think that we need something like #34068 (comment) to complete the picture.

  12. adrinjalali commented on May 21, 2026

    @adrinjalali
    Member

    I think we're mixing two issues here:

    • having a validation set handled in an estimator, and therefore also having the scores on the validation set naturally included in the report / callbacks, sometimes with extra metadata
    • estimator not having anything to do with a validation set, but wanting to have a callback / report / score with a sort of an "external" set, in the middle of the training.

    I'd say the priority / sensibility of the first one is higher than the second one. And it also makes it a lot more natural to handle scores of the validation set. We then don't need to have the finicky issue you raise in #34068 (comment) at all.

  13. jeremiedbb commented on May 21, 2026

    @jeremiedbb
    MemberAuthor

    Even in the first case, there's a link missing to associate a given dataset to a given metadata. Take
    LogisticRegression().fit(X, y, X_val=X_val, y_val=y_val, amount_train=amount_train, amount_val=amount_val)
    Who knows that train_scorer should be evaluated on X and val_scorer should be evaluated on X_val ?
    The signature of fit would be def fit(self, X, y, *, X_val=None, y_val=None, **params).

    EDIT: unless the callbacks have explicit X_val, y_val args in the hooks: on_fit_task_end(context, X, y, X_val, y_val, metadata). And then ScoringMonitor evaluates its scoring_train on X and its scoring_val on X_val. The metadata-routing then passes what's requested by each scorer. That should work out of the box indeed.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    CallbacksMetadata Routing

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions