-
Notifications
You must be signed in to change notification settings - Fork 786
Apply regression target transforms to the unnormalized target [RES-2639] #1196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Regression target transforms (`REGRESSION_Y_PREPROCESS_TRANSFORMS`) now act on the target in its original units and are standardized afterwards, instead of being applied to the already z-normalized target. A transform such as `1_plus_log` therefore does what its name suggests; previously it operated on standardized values, where it is undefined for roughly half of the target. This changes the predictions of the v2, v2.5 and v3 defaults, whose ensembles use `safepower` for half of their estimators; the v2.6 default (`"none"`) and any estimator without a target transform are unaffected. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| # Copyright (c) Prior Labs GmbH 2026. | ||
|
|
||
| """Target (y) transformation pipelines for regression. | ||
|
|
||
| The regressor z-normalises the target before handing it to the ensemble | ||
| preprocessing, because the shared bar-distribution space of the checkpoint is | ||
| defined in that z-normalised space. A target transform such as ``1_plus_log`` | ||
| is however only meaningful on the target in its *original* units: applied to | ||
| z-normalised values it operates on a shifted, rescaled target, which is not | ||
| what one would expect (and, for the log-like transforms, mostly produces NaNs | ||
| because roughly half of a z-normalised target is negative). | ||
|
|
||
| :func:`wrap_target_transform` therefore composes each transform into a | ||
| three-step pipeline that | ||
|
|
||
| 1. undoes the regressor's z-normalisation, so the transform sees the target in | ||
| its original units, | ||
| 2. applies the transform, and | ||
| 3. standardises the result again, which is the scale the model expects. | ||
|
|
||
| Keeping the pipeline's input and output in the z-normalised space means the | ||
| rest of the regressor -- in particular the bar-distribution borders and their | ||
| sanity limits, which are expressed in z-units -- is unaffected: | ||
| ``pipeline.inverse_transform`` maps the model's borders straight back into the | ||
| z-normalised space, exactly as an unwrapped transform did. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| import numpy as np | ||
| from sklearn.base import BaseEstimator, TransformerMixin | ||
| from sklearn.pipeline import Pipeline | ||
| from sklearn.preprocessing import StandardScaler | ||
|
|
||
| from tabpfn.preprocessing.steps.utils import make_scaler_safe | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Iterable | ||
|
|
||
| UNSTANDARDIZE_STEP = "unstandardize_target" | ||
| TARGET_TRANSFORM_STEP = "target_transform" | ||
| STANDARDIZE_STEP = "standardize_target" | ||
|
|
||
|
|
||
| class UnstandardizeTarget(TransformerMixin, BaseEstimator): | ||
| """Map a z-normalised target back to its original units. | ||
|
|
||
| ``transform`` undoes a z-normalisation with the given statistics and | ||
| ``inverse_transform`` re-applies it, so this transformer is the first step | ||
| of the pipelines built by :func:`wrap_target_transform`. | ||
|
|
||
| Args: | ||
| mean: Mean that was subtracted by the z-normalisation. | ||
| std: Standard deviation the z-normalisation divided by. | ||
| """ | ||
|
|
||
| def __init__(self, mean: float = 0.0, std: float = 1.0) -> None: | ||
| self.mean = mean | ||
| self.std = std | ||
|
|
||
| def fit(self, X: np.ndarray, y: np.ndarray | None = None) -> UnstandardizeTarget: | ||
| """Stateless, the statistics are given at construction time.""" | ||
| del X, y | ||
| return self | ||
|
|
||
| def transform(self, X: np.ndarray) -> np.ndarray: | ||
| """Return ``X`` in the original units of the target.""" | ||
| return np.asarray(X) * self.std + self.mean | ||
|
|
||
| def inverse_transform(self, X: np.ndarray) -> np.ndarray: | ||
| """Return ``X`` z-normalised again.""" | ||
| return (np.asarray(X) - self.mean) / self.std | ||
|
|
||
|
|
||
| def wrap_target_transform( | ||
| transform: TransformerMixin | Pipeline, | ||
| *, | ||
| mean: float, | ||
| std: float, | ||
| ) -> Pipeline: | ||
| """Compose a target transform so that it acts on the unnormalised target. | ||
|
|
||
| Args: | ||
| transform: The target transform to wrap, e.g. one of the presets of | ||
| :func:`get_all_reshape_feature_distribution_preprocessors`. | ||
| mean: Mean of the training target, used to undo its z-normalisation. | ||
| std: Standard deviation of the training target, used to undo its | ||
| z-normalisation. | ||
|
|
||
| Returns: | ||
| A pipeline mapping the z-normalised target to the transformed and | ||
| re-standardised target, whose ``inverse_transform`` maps back into the | ||
| z-normalised space. | ||
| """ | ||
| return Pipeline( | ||
| steps=[ | ||
| (UNSTANDARDIZE_STEP, UnstandardizeTarget(mean=mean, std=std)), | ||
| (TARGET_TRANSFORM_STEP, transform), | ||
| # The transform may leave the target on an arbitrary scale (e.g. a | ||
| # log target), while the model expects a standardised one. The safe | ||
| # wrapper also keeps non-finite outputs, such as the log of a | ||
| # non-positive target, from reaching the model. | ||
| (STANDARDIZE_STEP, make_scaler_safe("standard", StandardScaler())), | ||
| ], | ||
| ) | ||
|
|
||
|
|
||
| def rebind_target_transform_statistics( | ||
| transforms: Iterable[TransformerMixin | Pipeline | None], | ||
| *, | ||
| mean: float, | ||
| std: float, | ||
| ) -> None: | ||
| """Point wrapped target transforms at another z-normalisation, in place. | ||
|
|
||
| Needed when the transforms were built for one z-normalisation but are | ||
| (re-)fitted on a target normalised with different statistics, as in the | ||
| fine-tuning data pipeline, which re-splits the dataset and z-normalises | ||
| with the statistics of every new training split. | ||
|
|
||
| Transforms that are not pipelines from :func:`wrap_target_transform`, such | ||
| as the ``None`` entries of an unwrapped target transform, are ignored. | ||
|
|
||
| Args: | ||
| transforms: The target transforms to update. | ||
| mean: Mean of the z-normalisation the transforms will be fitted on. | ||
| std: Standard deviation of that z-normalisation. | ||
| """ | ||
| for transform in transforms: | ||
| if not isinstance(transform, Pipeline): | ||
| continue | ||
| step = transform.named_steps.get(UNSTANDARDIZE_STEP) | ||
| if isinstance(step, UnstandardizeTarget): | ||
| step.mean = mean | ||
| step.std = std | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "STANDARDIZE_STEP", | ||
| "TARGET_TRANSFORM_STEP", | ||
| "UNSTANDARDIZE_STEP", | ||
| "UnstandardizeTarget", | ||
| "rebind_target_transform_statistics", | ||
| "wrap_target_transform", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finetuning skips fitted target transforms
Medium Severity
RegressorBatchstill shipslist(conf)for border mapping, not the configs returned onensemble_membersafterfit_transform_ensemble_members. Withn_preprocessing_jobs > 1,wrap_target_transform's outerStandardScaleris fitted only on the worker copy, so finetuninginverse_transformon bar borders can hit an unfitted pipeline or decode with the wrong scale.predict_batchedalready takes member configs for this reason.Additional Locations (1)
src/tabpfn/finetuning/data_util.py#L437-L442Triggered by learned rule: Use executor_.ensemble_members configs, not ensemble_configs_, for fitted transforms
Reviewed by Cursor Bugbot for commit bf24f1d. Configure here.