Skip to content

fix(data): reshape labels instead of unsqueeze so a column-vector y works - #455

Open
ChrisW09 wants to merge 1 commit into
mainfrom
fix/column-vector-y
Open

fix(data): reshape labels instead of unsqueeze so a column-vector y works#455
ChrisW09 wants to merge 1 commit into
mainfrom
fix/column-vector-y

Conversation

@ChrisW09

Copy link
Copy Markdown
Collaborator

Fixes #441

Problem

fit() documents y : array-like, shape (n_samples,) or (n_samples, n_targets), but
TabularDataModule.setup() applied .unsqueeze(dim=1) without flattening first. An (n, 1) target
became a (B, 1, 1) label tensor against (B, 1) predictions:

  • Regression: silently wrong. MSELoss broadcasts (B, 1) against (B, 1, 1) to (B, B, 1) and
    optimises an all-pairs objective — the per-row pairing between prediction and target is gone. Same
    data, same seed, only the y shape differing: R2 = 0.996 (1-D) vs 0.002 (column vector).
  • Binary classification: hard crash. ValueError: Target size (torch.Size([8, 1, 1])) must be the same as input size (torch.Size([8, 1])).

The corrupted loss also drives val_loss, so early stopping and checkpoint selection were affected.

Fix

reshape(-1, 1) instead of unsqueeze(dim=1) — identical output for 1-D input, correct for a column
vector. The multiclass path already used view(-1) and was unaffected.

Tests

New tests/test_target_shapes.py: label tensors are 2-D for both input shapes, a column-vector y
reaches the same R2 as 1-D (within 0.1), and binary classification accepts a column vector.
Verified 3 of the 4 fail on main. tests/test_data.py and tests/test_models.py pass unchanged.

🤖 Generated with Claude Code

…orks

fit() documents y as (n_samples,) or (n_samples, n_targets), but
TabularDataModule.setup() applied unsqueeze(dim=1) without flattening
first. An (n, 1) target therefore became a (B, 1, 1) label tensor against
(B, 1) predictions: MSELoss broadcast that to (B, B, 1) and optimised an
all-pairs objective, losing the per-row pairing entirely, while
BCEWithLogitsLoss refused to broadcast and raised.

Same data and seed, only the y shape differing, gave R2 0.996 for 1-D y
versus 0.002 for y.reshape(-1, 1). The corrupted loss also drove val_loss,
so early stopping and checkpoint selection were affected too.

reshape(-1, 1) produces the intended (B, 1) labels for both 1-D and
column-vector input.

Fixes #441

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.

[BUG] A (n,1) column-vector y silently trains against a broadcast all-pairs loss (R2 0.996 -> 0.002)

1 participant