Repository navigation
[BTY-72] feat(tpc): add MyBatchNorm1d, per-stay loss, mortality multi… - #1699
github-actions[bot] merged 3 commits into
Conversation
…task head, and ablations
Test CoverageTotal: 88.18% (57187 / 64853 lines)
|
|
/review |
There was a problem hiding this comment.
Previous review
AI Review
suggestion: need change
decision: need change
Thank you for this contribution. It adds MyBatchNorm1d, Eq. 6 per-stay loss, a mortality multitask head, and the weight-sharing and no-skip ablations to TPC, with defaults unchanged. Coverage is strong: 99.3% on tpc.py and 88% for the repository, both above the bar. The change is close, but two blocking problems remain.
To get this merged:
- The docs claim eval-mode predictions are strictly causal (docs rst, around line 45). With
batchnorm="mybatchnorm", eval statistics are pooled over the batch and time axes. A prediction at hour t therefore depends on later hours, so the claim is wrong for that option. Please state this exception in the docs and in theTPCdocstring. Optionally add a test showing the behaviour. The existing causality test only coversshare_weightsanduse_skip. - The
min_timefallback in_resolve_sequence_maskis decided across the whole batch. If at least one stay has post-warmup steps, stays whose valid steps are all inside the warmup get zero weight. If none do, the warmup hours are kept for every stay. A stay's loss therefore depends on which stays share its batch. This is not described in the docs and is untested. Please either make the behaviour per-stay and consistent, or document it clearly, and add a test with a mixed batch.
Optional suggestions:
torch.nn.modules.batchnorm._BatchNormis a private torch import. It works, but it may change between torch versions, so consider noting this in a comment.- The docs say the loss uses Eq. 6 warmup. Spelling out the fallback there would help users.
Reviewed commit cfa79b5
…subclass BatchNorm1d
|
/review |
There was a problem hiding this comment.
Previous review
AI Review
suggestion: need change
decision: need change
Thank you for this follow-up to #1412. It adds MyBatchNorm1d, per-stay Eq. (6) loss averaging, a mortality multitask head, and the weight-sharing and no-skip ablations. Defaults keep the existing behavior, the tests are thorough, and the docs match the code. Coverage of pyhealth/models/tpc.py is about 99% against the 80% bar, and the whole repository is at 88%, above the 60% bar. The change touches only TPC, its tests and its docs, so I don't see it affecting the core part of the codebase.
To get this merged:
- Per-hour targets can't reach the per-stay path through a dataset.
loss_reduction="per_stay"withmask_keyandmin_timeonly takes the sequence branch wheny_truehas shape(batch, time)or(batch, time, 1).TPC.__init__requires the label processor to be inregressionmode, which gives one scalar per stay. A normal dataset, DataLoader and Trainer flow therefore always falls into the scalar branch.- The tests only reach the sequence branch by overwriting
batch["los"]by hand. - Please either (a) allow a per-hour label schema, for example a
tensorlabel processor for the LoS key whenloss_reduction="per_stay", and add a test that goes from dataset to model to loss without patching the batch, or (b) state clearly in the description and docs that sequence targets must be supplied by the caller, and thatper_stay_lossis meant to be used directly or with custom batches. - In the scalar fallback,
mask_keyandmin_timehave no effect. Please say so in the docs too.
Optional suggestions:
MyBatchNorm1dwithtrack_running_stats=Falsecan fail ineval()whenbatch * time == 1, because there are no running statistics to fall back on. A guard or a note in the docstring would help.- A short README or example showing how to build a per-hour label dataset would make the new options easier to use.
Reviewed commit 8f23e87
…calar fallback, guard MyBatchNorm1d
|
/review |
| official code accepts one size per layer). | ||
| * No mortality head or multitask loss, and no sequence-length mask in | ||
| the loss because windows have a fixed length. | ||
| Optional extensions beyond the scalar baseline: |
There was a problem hiding this comment.
[low] Removed caveats from the official-implementation note
The 'Differences from the official implementation' list was deleted from the TPC class note. Three caveats still hold and are no longer documented: forward() predicts only the last hour, temporal and pointwise sizes are the same in every layer, and the default loss has no sequence-length mask. Restore those bullets in the note, or move them to the forward() docstring, so users comparing with the official code still see them.
| bce_elem = F.binary_cross_entropy_with_logits( | ||
| mort_logit, mort_true, reduction="none" | ||
| ) | ||
| mort_loss = _reduce_per_stay(bce_elem, eval_mask) |
There was a problem hiding this comment.
[low] Mortality BCE also uses min_time and mask, which is undocumented
In per_stay mode the mortality loss is reduced with the same eval_mask as the LoS loss, so warmup hours (index < min_time) and padded steps are also dropped from the mortality BCE. The min_time and mask_key docs only describe the LoS loss. Add a sentence saying the mortality BCE uses the same per-stay mask and warmup exclusion, or exclude the mortality loss from warmup.
| else: | ||
| if ( | ||
| mortality_key not in self.label_keys | ||
| and mortality_key not in self.feature_keys |
There was a problem hiding this comment.
[low] mortality_key may name an input feature without a binary check
The validation accepts mortality_key when it is only an input feature, and the binary check at the label-processor branch only runs for label keys. A non-binary feature used as the target then goes into binary_cross_entropy_with_logits without error. Require mortality_key to be in label_keys with a binary processor, or add a binary check and a test for the feature-key path.
There was a problem hiding this comment.
AI Review
suggestion: approve
decision: approve
Thank you for this follow-up to the TPC model. It adds MyBatchNorm1d, Eq. 6 per-stay loss averaging, an optional mortality multitask head, and the share_weights and use_skip ablations. All of the new options default to the earlier behavior, and the paper and reference code are cited.
This looks ready to merge. The change stays inside pyhealth/models/tpc.py, its tests and its docs page. The new TensorProcessor import is used only for an isinstance check. Coverage for tpc.py is 99.3%, above the 80% bar for a changed file. Total repository coverage is 88.2%, above the 60% bar. The docs match the code, including the note that mybatchnorm breaks strict eval-mode causality.
Optional suggestions:
- The old "Differences from the official implementation" note is gone. Some of its caveats still hold, so you could keep them:
forwardscores only the last hour by default, layer sizes are the same in every layer, and the default loss has no sequence mask. - Document that the mortality BCE also uses
mask_keyandmin_timewhenloss_reduction="per_stay". mortality_keymay name an input feature, and that case gets no binary-label check. Consider validating it.mask_keyandmin_timeare silently ignored withloss_reduction="mean". A warning or an error would make that clearer.- With
mortality_keyset,model.modemay beNone, soget_metrics_fn(model.mode)could fail. A short note in the docs, or a fix, would help. - A short example script showing the new options would help users.
Reviewed commit 65f3075
|
/merge |
Contributor Information
tkumar70@gatech.edu), Yi Wang (ywang4715@gatech.edu)tkumar70,ywang4715)Contribution Type
Model Enhancement (
pyhealth.models.TPC) —[BTY-72]Description
Follow-up to #1428, contributing the requested paper-level options on top of the merged
TPCmodel (#1412) in a focused, backward-compatible PR:MyBatchNorm1d(batchnorm="mybatchnorm"): AddsMyBatchNorm1dmirroring the official TPC repository (EmmaRocheteau/TPC-LoS-prediction,models/tpc_model.py, MIT License notice updated at the top ofpyhealth/models/tpc.py), normalizing over batch statistics duringeval()while freezing running statistics updates (batchnorm="default"andbatchnorm="none"also supported).per_stay_loss,loss_reduction="per_stay",mask_key,min_time): Implements Equation (6) of Rocheteau et al. (ACM CHIL 2021) — averaging per-hour errors within each stay first (excluding padded steps and optional warmup hours< min_time) and then averaging equally across valid stays in the batch.mortality_key,mortality_loss_weight,forward_mortality_sequence): Adds the parallel two-layer pointwise binary mortality prediction head and weighted multitask BCE objective (loss = los_loss + mortality_loss_weight * mortality_loss, default weight100.0per Appendix C of the paper), covering the mortality prediction component of[BTY-72].share_weights,use_skip):share_weights=True: shares one(C -> Y)temporal convolution kernel across allRfeatures in eachTPCLayer(full-TPC temporal weight sharing; note that the official repository'sTempWeightSharescript pairsshare_weights=Truewith a temporal-only architecture).use_skip=False: disables raw-input and intermediate pointwise skip connections (TPC (no skip)in Table 3 of the paper, matchingtemp_pointwise_no_skipinmodels/tpc_model.py).Backward Compatibility: No breaking changes. All new parameters are optional keyword arguments whose defaults preserve the existing #1412
TPCandTPCLayerbehavior. No core classes orpyhealth/models/__init__.pyexports are modified.Paper & Reference Code
EmmaRocheteau/TPC-LoS-prediction(MIT License)Files to Review
pyhealth/models/tpc.py—MyBatchNorm1d,per_stay_loss, and optionalbatchnorm,share_weights,use_skip,loss_reduction/mask_key/min_time, andmortality_key/mortality_loss_weightextensions onTPCLayerandTPCtests/core/test_tpc.py— Unit tests inTestTPCPaperOptionscovering all new options, edge cases, causality, and gradient flow on small synthetic datadocs/api/models/pyhealth.models.tpc.rst— Updated API documentation forMyBatchNorm1d,per_stay_loss, and the newTPCoptionsVerification
pytest tests/core/test_tpc.py -v: 45 passed (20 subtests passed)pyhealth/models/tpc.py: 99% (425 / 428 statements)python -m doctest pyhealth/models/tpc.py: passedpython tools/check_pr_rules.py --base upstream/master --head HEAD: passed (0ruffviolations,>>>doctests on all public classes/functions)