fix(models): make encode() deterministic and repair the LSS override - #458
Open
ChrisW09 wants to merge 2 commits into
Open
fix(models): make encode() deterministic and repair the LSS override#458ChrisW09 wants to merge 2 commits into
ChrisW09 wants to merge 2 commits into
Conversation
encode() never switched the model out of training mode, so embeddings taken after fit() ran with dropout active: two calls on the same rows differed by up to 4.45 and no result was reproducible. It also ran with autograd enabled. The model is now put in eval() under torch.no_grad() and its previous mode restored afterwards. SklearnBaseLSS.encode() additionally unpacked two values from the three-element (num_features, cat_features, embeddings) batches that preprocess_new_data yields, so it raised 'ValueError: too many values to unpack (expected 2)' for every LSS model. It now passes the whole batch to estimator.encode(), matching the base _PredictMixin implementation. Fixes #451 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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 #451
Problems
encode()never leaves training mode. Afterfit()the model is still in train mode, so dropout stays active and embeddings are corrupted and non-reproducible. Verified withattn_dropout=0.5: twoencode(X)calls on the same rows differed by up to 4.45, withestimator.training == True. It also ran with autograd enabled, needlessly building a graph.SklearnBaseLSS.encode()always raises. It unpacks two values from the three-element(num_features, cat_features, embeddings)batches thatpreprocess_new_datayields:ValueError: too many values to unpack (expected 2)for every LSS model, on every architecture.Fix
Put the task model in
eval()undertorch.no_grad()for the duration of the encode, restoring the previous mode afterwards; and have the LSS override pass the whole batch toestimator.encode(), matching the base_PredictMixinimplementation.Tests
New
tests/test_encode.py: twoencode()calls agree, the prior training mode is restored, and LSSencode()returns embeddings. Verified 2 of the 3 fail onmain.tests/test_base_mixins.py,test_inspection.pyandtest_lss_base.pypass unchanged.Note: the stray
self.encoder(x)line inBaseModel.encode()(which makesencode()raise on Mamba backbones) is a separate defect, already filed as #426 item 1 and fixed in #439.🤖 Generated with Claude Code