Repository navigation
Conversation
For classification, get_knn() drops rows without a same-class or different-class neighbor from the neighbor indices, but contrastive_loss() still built one anchor block per batch row, so a batch with a class singleton crashed CosineEmbeddingLoss with a shape mismatch. Return the anchor rows alongside the neighbor indices (_pair_indices) and let contrastive_loss() pair the neighbors with exactly those anchors. The singleton row still serves as a negative for the others. Correct the pretraining note in the training API docs. Fixes #527
get_knn() no longer points readers to a private method, get_knn() and contrastive_loss() get numpydoc sections, and the singleton test now asserts that the singleton row is drawn as a negative. Refs #527
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 #527
Problem
For classification,
ContrastivePretrainer.get_knn()keeps only thevalidrows: rows that have at least one same-class and one different-class neighbor.contrastive_loss()still repeated every batch row as an anchor. A batch containing a class singleton therefore had more anchor rows than pair rows, andCosineEmbeddingLossfailed with a shape mismatch (32vs30). Even with matching sizes, the neighbor rows would have been attributed to the wrong anchors. #414 mentions this crash; its fix corrected the anchor layout but not the anchor selection.Fix
In
deeptab/training/pretraining.py:_pair_indices(labels)returns(knn_indices, neg_indices, anchor_indices), the batch rows each neighbor row belongs to. For regression every row is an anchor.get_knn()keeps its public two-value return and delegates to it.contrastive_loss(..., anchor_indices=None)builds the anchor block from exactly those rows, in both the pooled and the unpooled branch. A singleton row is not used as an anchor, but it still serves as a negative neighbor for the other rows.training_step,validation_stepandtest_steppass the anchor indices through.The
ValueErrorfor batches in which no row qualifies is unchanged.Tests
In
tests/training/test_training_pretraining.py:test_class_singleton_is_dropped_from_anchors_not_from_pairs: anchor selection, the singleton row drawn as a negative for every anchor (withk_neighbors=4all four different-class rows are drawn, so the check is deterministic), plus finite pooled and unpooled losses.test_contrastive_loss_uses_the_given_anchor_rows: with orthonormal embeddings the loss is zero only when the neighbor rows are paired with the given anchors.test_classifier_pretraining_completes_with_a_class_singleton: end-to-endFTTransformerClassifier.pretrain()with a class that occurs once.All three fail on
mainand pass with this change.Docs
The pretraining note in
docs/api/training/index.rstsaid aValueErroris raised "if any sample has no same-class or no different-class neighbor". It now says that such samples are skipped as anchors and that the error only occurs when no sample in a batch qualifies.get_knn()andcontrastive_loss()have numpydoc Parameters/Returns sections;get_knn()explains that its rows need not match the batch rows instead of pointing to the private_pair_indices().Checks
just test: 3239 passed, 46 skipped, 28 xfailed; coverage 91.56%just docs: build succeeded (warnings treated as errors)just check: all hooks passed (ruff, prettier, pyright)