Skip to content

fix(data): fit target-aware preprocessing on vector targets - #635

Open
fbaeumer wants to merge 1 commit into
OpenTabular:mainfrom
fbaeumer:fix/dirichlet-target-aware-preprocessing
Open

fbaeumer wants to merge 1 commit into
OpenTabular:mainfrom
fbaeumer:fix/dirichlet-target-aware-preprocessing

Conversation

@fbaeumer

Copy link
Copy Markdown

Problem

Fitting a Dirichlet LSS model with the default preprocessing fails before training starts:

import numpy as np
from deeptab.models import MLPLSS

rng = np.random.default_rng(0)
X = rng.normal(size=(200, 2))
y = rng.dirichlet([1, 2, 3], size=200)

MLPLSS().fit(X, y, family="dirichlet", max_epochs=1)
# pretab.exceptions.PretabDataError: X and y must have same length. Got 160 and 480

PreTab flattens the target it receives (np.asarray(y).ravel()), so the (n_samples, n_components) target matrix reaches the target-aware numerical encoders as n_samples * n_components scalar targets. This affects:

  • numerical_method="ple", which is the default, and
  • spline placement with target_aware=True (fails with operands could not be broadcast together with shapes (64,) (192,)).

Only target-free methods such as "standardization" work. The existing Dirichlet tests all set numerical_method="standardization" explicitly, which is why this was not caught.

Fix

TabularDataModule.preprocess_data now reduces a regression target matrix to a single column before fitting the preprocessor, by projecting it onto its first principal axis. Target-aware placement only needs a scalar signal that varies with the target, and this is the direction along which the targets vary most. For a two-component target it is equivalent to using either column.

Scalar targets (1-D or a single column) and classification labels are passed through unchanged, so existing fits are not affected. The model itself still trains on the full target matrix.

An alternative would be to raise an actionable error that asks for a target-free numerical_method. I went with the projection so that the documented fit(X, y, family="dirichlet") call works with default settings, but I am happy to switch if you prefer the stricter behaviour.

Tests

  • tests/integration/test_lss_base.py: Dirichlet fit with PLE and with a target-aware spline, each with an automatic and an explicit validation split. All four cases fail on main and pass with this change.
  • tests/data/test_datamodule.py: unit tests for the projection (scalar targets unchanged, aligned 1-D output, two-component equivalence, constant targets).

ruff check, ruff format --check and pyright pass on the changed files. I ran tests/data, tests/integration/test_lss_base.py, tests/lifecycle, tests/distributions, tests/contracts and tests/boundaries locally (macOS, Python 3.13).

One note: TestSaveLoad::test_load_predictions_match fails for me when tests/data runs before tests/integration/test_lss_base.py, on unchanged main as well. It compares predictions with rtol=1e-4 and no atol, so a value close to zero trips it on float32 rounding (2.2e-08 absolute difference). I left it out of this PR.

Fitting a Dirichlet LSS model with the default preprocessing failed with
"X and y must have same length. Got 160 and 480". PreTab flattens the
target it receives, so a target matrix of shape (n_samples, n_targets)
reached the target-aware numerical encoders as n_samples * n_targets
scalar targets. This affected PLE, the default numerical method, and
spline placement with target_aware=True; only target-free methods such
as standardization worked.

The data module now reduces a regression target matrix to one column
before fitting the preprocessor by projecting it onto its first
principal axis. Scalar targets and classification labels are passed
through unchanged, and the model still trains on the full target matrix.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant