Fix MoleRec fallback predictor never training - #1214
Open
fbonc wants to merge 1 commit into
Open
Conversation
simple_predictor was created lazily inside forward(), so an optimizer built from model.parameters() beforehand (the standard pattern) never saw its parameters. Create it in __init__ on the no-SMILES fallback path.
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.
Issue
On MoleRec's fallback path (no valid SMILES data), the linear fallback predictor simple_predictor was created lazily inside forward() (molerec.py:814-819). The standard PyHealth training pattern builds the optimizer from model.parameters() before the first forward pass, so the predictor's parameters were never registered with the optimizer and never trained.
Fix
Create simple_predictor in init when has_valid_smiles is False, sized nn.Linear(hidden_dim * 2, label_size) to match the concatenated condition/procedure patient embedding. Removed the lazy creation (and its now-unnecessary .to(self.device)) from forward. Parameters are now registered before the optimizer is built.
Notes
Added regression test test_fallback_predictor_registered_and_trains in tests/core/test_molerec.py: it asserts simple_predictor is in named_parameters before any forward and that an optimizer step actually updates its weights.