Repository navigation
Conversation
A batch with one row has no neighbors to pair with: regression pretraining crashed in get_knn() and classification raised the "no usable sample" ValueError. Such a batch occurs whenever the number of training or validation rows leaves a remainder of 1 for the batch size. Skip it in the training, validation and test steps. Fixes #529
A classification anchor needs both a same-class and a different-class neighbor, so no row of a two-row batch can be one, and such a trailing batch failed with the 'Use a larger batch size' error. The pretraining note now says which batches are skipped. Refs #529
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #529
Problem
The loaders keep a trailing partial batch, so pretraining sees tiny batches whenever the number of training or validation rows leaves a small remainder for
batch_size. Such a batch can't form any pair:get_knn()squeezed the(1, 1)distance matrix to shape(1,)andtopk(...).indices[:, 1:]raisedIndexError;ValueErrorstopped pretraining.Fix
ContrastivePretrainer._cannot_form_pairs(batch)is true for batches too small for any row to be an anchor: fewer than two rows for regression, fewer than three for classification.training_step,validation_stepandtest_stepreturnNonefor such a batch, so Lightning skips it. Larger batches are handled exactly as before, including the documentedValueErrorwhen no row of a batch qualifies (e.g. three rows of three different classes).#577 (PR #578) independently makes the training loader drop a one-row trailing training batch. This PR also covers one-row validation batches and two-row classification batches, which that change doesn't touch.
Tests
In
tests/training/test_training_pretraining.py:test_batch_too_small_to_form_pairs_is_skipped[regression-one-row|classification-one-row|classification-two-classes|classification-one-class]: the three steps returnNone.test_three_row_batch_without_same_class_neighbors_still_raises: the documented error is kept for larger batches.test_estimator_pretraining_completes_with_a_tiny_last_batch[regression-one-row-val-batch|classification-one-row-val-batch|classification-two-row-train-batch]:FTTransformer*.pretrain()withbatch_size=16and 17 validation rows or 34 training rows.All new cases except the three-row guard fail on
main; all pass with this change.Docs
The pretraining note in
docs/api/training/index.rstsays that batches too small to form any pair are skipped rather than raising.Checks
just test: 3244 passed, 46 skipped, 28 xfailed; coverage 91.30%just docs: build succeeded (warnings treated as errors)just check: all hooks passed (ruff, prettier, pyright)