-
Notifications
You must be signed in to change notification settings - Fork 538
Add predict_raw_logits to TabPFNRegressor #688
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
Open
carterprince
wants to merge
1
commit into
PriorLabs:main
Choose a base branch
from
carterprince:feat/regressor-raw-logits
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -879,6 +879,51 @@ def predict( | |||||
| quantiles: list[float] | None = None, | ||||||
| ) -> FullOutputDict: ... | ||||||
|
|
||||||
| def _raw_predict(self, X: XType) -> torch.Tensor: | ||||||
| """Internal method to run prediction. | ||||||
|
|
||||||
| Handles input validation, preprocessing, and the forward pass. | ||||||
| Returns the stack of aligned probabilities from all estimators | ||||||
| (shape: [n_estimators, n_samples, n_borders]) mapped to the global | ||||||
| `znorm_space_bardist_` borders. | ||||||
| """ | ||||||
| check_is_fitted(self) | ||||||
|
|
||||||
| # TODO: Move these at some point to InferenceEngine | ||||||
| X = validate_X_predict(X, self) | ||||||
|
|
||||||
| # Constant target handling | ||||||
| if hasattr(self, "is_constant_target_") and self.is_constant_target_: | ||||||
| # If the target is constant, we have a single bucket (len(borders) - 1). | ||||||
| # We return ones (probability of 1.0) for this single bucket. | ||||||
| n_buckets = len(self.znorm_space_bardist_.borders) - 1 | ||||||
| return torch.ones( | ||||||
| (self.n_estimators, X.shape[0], n_buckets), | ||||||
| device=self.devices_[0], | ||||||
| ) | ||||||
|
|
||||||
| X = fix_dtypes(X, cat_indices=self.inferred_categorical_indices_) | ||||||
| X = process_text_na_dataframe(X, ord_encoder=self.preprocessor_) # type: ignore | ||||||
|
|
||||||
| # Runs over iteration engine | ||||||
| ( | ||||||
| _, | ||||||
| outputs, # list of tensors [N_est, N_samples, N_borders] (after forward) | ||||||
| borders, # list of numpy arrays containing borders for each estimator | ||||||
| ) = self.forward(X, use_inference_mode=True) | ||||||
|
|
||||||
| # --- Translate probs --- | ||||||
| # Map specific estimator borders to the global znorm_space_bardist_ | ||||||
| transformed_probs = [ | ||||||
| translate_probs_across_borders( | ||||||
| logits, | ||||||
| frm=torch.as_tensor(borders_t, device=logits.device), | ||||||
| to=self.znorm_space_bardist_.borders.to(logits.device), | ||||||
| ) | ||||||
| for logits, borders_t in zip(outputs, borders) | ||||||
| ] | ||||||
| return torch.stack(transformed_probs, dim=0) | ||||||
|
|
||||||
| @config_context(transform_output="default") # type: ignore | ||||||
| @track_model_call(model_method="predict", param_names=["X"]) | ||||||
| def predict( | ||||||
|
|
@@ -920,11 +965,6 @@ def predict( | |||||
| """ | ||||||
| check_is_fitted(self) | ||||||
|
|
||||||
| # TODO: Move these at some point to InferenceEngine | ||||||
| X = validate_X_predict(X, self) | ||||||
|
|
||||||
| check_is_fitted(self) | ||||||
|
|
||||||
| if quantiles is None: | ||||||
| quantiles = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9] | ||||||
| else: | ||||||
|
|
@@ -935,42 +975,31 @@ def predict( | |||||
| raise ValueError(f"Invalid output type: {output_type}") | ||||||
|
|
||||||
| if hasattr(self, "is_constant_target_") and self.is_constant_target_: | ||||||
| # We must validate X even if constant target to ensure shape is correct | ||||||
| X = validate_X_predict(X, self) | ||||||
| return self._handle_constant_target(X.shape[0], output_type, quantiles) | ||||||
|
|
||||||
| X = fix_dtypes(X, cat_indices=self.inferred_categorical_indices_) | ||||||
| X = process_text_na_dataframe(X, ord_encoder=self.preprocessor_) # type: ignore | ||||||
| # Get the aligned stack of probabilities from all estimators | ||||||
| stacked_probs = self._raw_predict(X) | ||||||
|
|
||||||
| # Runs over iteration engine | ||||||
| ( | ||||||
| _, | ||||||
| outputs, # list of tensors [N_est, N_samples, N_borders] (after forward) | ||||||
| borders, # list of numpy arrays containing borders for each estimator | ||||||
| ) = self.forward(X, use_inference_mode=True) | ||||||
|
|
||||||
| # --- Translate probs, average, get final logits --- | ||||||
| transformed_logits = [ | ||||||
| translate_probs_across_borders( | ||||||
| logits, | ||||||
| frm=torch.as_tensor(borders_t, device=logits.device), | ||||||
| to=self.znorm_space_bardist_.borders.to(logits.device), | ||||||
| ) | ||||||
| for logits, borders_t in zip(outputs, borders) | ||||||
| ] | ||||||
| stacked_logits = torch.stack(transformed_logits, dim=0) | ||||||
| if self.average_before_softmax: | ||||||
| logits = stacked_logits.log().mean(dim=0).softmax(dim=-1) | ||||||
| # stacked_probs from _raw_predict are probabilities (sum to 1) | ||||||
| # We take log to get logits, average them, then softmax | ||||||
| ensemble_probs = stacked_probs.log().mean(dim=0).softmax(dim=-1) | ||||||
| else: | ||||||
| logits = stacked_logits.mean(dim=0) | ||||||
| # Average the probabilities directly | ||||||
| ensemble_probs = stacked_probs.mean(dim=0) | ||||||
|
|
||||||
| # Post-process the logits | ||||||
| logits = logits.log() | ||||||
| if logits.dtype == torch.float16: | ||||||
| logits = logits.float() | ||||||
| # We ensure we are working with log-probabilities for the criterion methods | ||||||
| ensemble_log_probs = ensemble_probs.log() | ||||||
| if ensemble_log_probs.dtype == torch.float16: | ||||||
| ensemble_log_probs = ensemble_log_probs.float() | ||||||
|
|
||||||
| # Determine and return intended output type | ||||||
| logit_to_output = partial( | ||||||
| _logits_to_output, | ||||||
| logits=logits, | ||||||
| logits=ensemble_log_probs, | ||||||
| criterion=self.raw_space_bardist_, | ||||||
| quantiles=quantiles, | ||||||
| ) | ||||||
|
|
@@ -1000,13 +1029,36 @@ def predict( | |||||
| return FullOutputDict( | ||||||
| **main_outputs, | ||||||
| criterion=self.raw_space_bardist_, | ||||||
| logits=logits, | ||||||
| logits=ensemble_log_probs, | ||||||
| ) | ||||||
|
|
||||||
| return main_outputs | ||||||
|
|
||||||
| return logit_to_output(output_type=output_type) | ||||||
|
|
||||||
| @config_context(transform_output="default") | ||||||
| @track_model_call(model_method="predict", param_names=["X"]) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||
| def predict_raw_logits(self, X: XType) -> np.ndarray: | ||||||
| """Predict the raw logits for the provided input samples. | ||||||
|
|
||||||
| This method returns the raw logits for each estimator, without averaging | ||||||
| estimators. In the case of regression, these logits are aligned to the | ||||||
| global bar distribution used by the model (handling potential target | ||||||
| shifting/scaling per estimator). | ||||||
|
|
||||||
| Args: | ||||||
| X: The input data for prediction. | ||||||
|
|
||||||
| Returns: | ||||||
| An array of predicted logits for each estimator, | ||||||
| Shape (n_estimators, n_samples, n_bins). | ||||||
| """ | ||||||
| # _raw_predict returns aligned probabilities (output of translate_probs) | ||||||
| stacked_probs = self._raw_predict(X) | ||||||
|
|
||||||
| # Convert probabilities to logits (log-space) and detach | ||||||
| return stacked_probs.log().float().detach().cpu().numpy() | ||||||
|
|
||||||
| def forward( | ||||||
| self, | ||||||
| X: list[torch.Tensor] | XType, | ||||||
|
|
||||||
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.
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.
The docstring for
_raw_predictstates that the returned tensor has a shape of[n_estimators, n_samples, n_borders]. However, the last dimension corresponds to the number of bins/buckets, which islen(borders) - 1. To avoid confusion, it would be clearer to usen_binsinstead ofn_borders.