Conversation
EmbeddingModel inferred the input width of a TensorProcessor feature by calling processor.process() on the first dataset sample. Samples read from a SampleDataset are already processed, so this ran process() a second time: wrong for any processor that is not idempotent (selecting columns, scaling, imputing), and a column-selecting processor raised. - EmbeddingModel reads the width from the first already-processed sample, falling back to processor.size(); process() is never called. - TensorProcessor.fit() records the feature width (last dimension, 1 for scalars) and size() returns it instead of None. - tests/core/test_embedding_tensor_width.py: size() after fit, a column-selecting processor that raises if re-processed builds an MLP with the right width, and plain "tensor" features. - docs: "Custom tensor processors" on the TensorProcessor page. - examples/custom_tensor_processor.py: a column-selecting processor with an MLP. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Problem
For
TensorProcessorfeatures,EmbeddingModel.__init__inferred the input width by callingprocessor.process(sample[field])on the first dataset sample (pyhealth/models/embedding.py). But samples from aSampleDatasetare already processed, soprocess()ran a second time.That's harmless for the plain
TensorProcessor. It's wrong for any processor that isn't idempotent: one that selects columns, scales or imputes. Re-processing either raises, or silently produces the wrong width. On master, a column-selecting subclass raises insideMLP(dataset=...).Changes
EmbeddingModelreads the width from the first already-processed sample, which is exactly what the model will receive. It falls back toprocessor.size()when no sample is available.process()is never called.TensorProcessor.fit()records the feature width (the last dimension, or 1 for scalars), andsize()returns it instead ofNone. Nothing in the library relied onNone(checked by grep). Old pickled processors without the attribute still returnNone.The processed sample takes precedence over
size()on purpose. A subclass that changes the width but doesn't overridesize()would otherwise report the raw width.Tests, docs, example
New
tests/core/test_embedding_tensor_width.py:size()afterfit(), before fit, and for scalars;process()raises if re-run builds anMLPwithin_features == 2;"tensor"features keep their width.It fails on master and passes here.
docs/api/processors/pyhealth.processors.TensorProcessor.rstgains a "Custom tensor processors" section. TheEmbeddingModelandTensorProcessordocstrings get>>>examples, verified as doctests.New
examples/custom_tensor_processor.py: a column-selecting processor with anMLPon synthetic data.Full core suite:
Ran 1386 tests … OK (skipped=76).tools/check_pr_rules.pypasses.Reported by a downstream EHR project that uses a column-selecting, standardising tensor processor.
🤖 Generated with Claude Code