Repository navigation
fix: structured z-scoring takes the per-sample std over the whole event - #2171
Conversation
The per-sample std was taken over dim 1 only, while the mean pools every event dimension. For (N, 1, H, W) input it was NaN; for (N, C, ...) it was the std across channels. Flatten each sample first; (N, D) input is unchanged.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2171 +/- ##
==========================================
+ Coverage 88.77% 89.55% +0.78%
==========================================
Files 142 142
Lines 14898 14898
==========================================
+ Hits 13225 13342 +117
+ Misses 1673 1556 -117
Flags with carried forward coverage won't be shown. Click here to find out more.
|
janfb
left a comment
There was a problem hiding this comment.
@G26karthik thanks, the fix looks good! I also ran NPE with a CNN embedding on single-channel images of shape (1, 16, 16): it stopped with the NaN assertion before and trains now.
One correction to the description: PR #2155 does touch the same lines, since it lets standardizing_net reuse z_standardization. I will resolve that in PR #2155 after this one is merged, so nothing is needed from you.
Thanks for fixing this, Jan
|
Thanks, Jan, also for trying it with a CNN embedding. Noted on #2155, and thank you for handling it there. |
…-tensors The conflict in sbiutils.py keeps PR #2171's per-sample std over the flattened event inside the shared z_standardization, which standardizing_net now calls.
What does this PR do?
With
z_score_x="structured", the per-sample std is taken over dim 1 only, while the mean already pools every event dimension. The docstrings ofz_standardizationandstandardizing_netsay the std is computed per sample first and then averaged, which matches what happens for(N, D)data, but not for data with more than one event dimension:xof shape(N, 1, H, W), the input formCNNEmbeddingdocuments, the std is taken over a size-1 dimension, so it is NaN for every sample.standardizing_netthen fails with "Training data mean or std for standardizing net must not contain NaNs", although the data has no NaNs, andz_standardizationreturns a NaN std.(N, C, H, W)or(N, C, T), it becomes the std across channels at each position, a different scale from the per-sample std.Both functions now flatten each sample before taking the std:
torch.std(batch_t[is_valid_t].flatten(start_dim=1), dim=1). For(N, D)data the flatten does nothing, so time series and other 2-D inputs give exactly the same result as before.Does this close any issues?
No issue, it's a small and clear bug fix.
Anything else we should know?
The new test
test_structured_z_scoring_uses_all_event_dimschecks event shapes(1, 6, 6),(3, 6, 6)and(2, 18)againstbatch.flatten(1).std(dim=1).mean()for both functions. They fail onmain(a NaN std, the channel-wise value, and the NaN assertion fromstandardizing_netfor(1, 6, 6)) and pass with the change.#2155 also edits
standardizing_net, but a different branch of it, so the two should not conflict.I reviewed and tested every change myself; AI tools helped with drafting.
Checklist
pytest tests/sbiutils_test.py -k "structured or z_scor"passes, andruff checkandruff format --checkpass on the changed files. I didn't run the full fast suite for this one.pytest.mark.slow. The new test only builds small tensors and trains nothing.