Fix ZeroSumTransform logp for non-trailing zero-sum axes#8336
Open
velochy wants to merge 1 commit into
Open
Conversation
`transformed_value_logprob` reduces the logp's *trailing* axes to match a
lower-dim Jacobian, assuming (as for Simplex/Ordered) that a multivariate
transform acts on the trailing axes. `ZeroSumTransform` may zero-sum *arbitrary*
axes, so for a non-trailing axis the reduced logp keeps the wrong axis and
disagrees with `log_jac_det` (which drops `zerosum_axes`). The shapes then differ,
e.g. `(3,) + (2,)`. This is silent with symbolic dims (both broadcastable) but
raises `Incompatible Elemwise input shapes` once dims are static -- e.g. after
`freeze_dims_and_data`, which the numba/jax sampling backends apply.
Fix: expose the integrated-out axes via `ZeroSumTransform.jacobian_reduce_axes`
and have `transformed_value_logprob` reduce those axes (falling back to the
trailing axes for transforms that don't define them). `ZeroSumNormal` is
unaffected (it only zero-sums trailing axes); batched transformed-logp shapes are
preserved.
Minimal repro (raised before, builds after)::
with pm.Model() as m:
pm.Normal("x", 0.0, 1.0, shape=(3, 2),
transform=ZeroSumTransform([0]))
m.logp()
Contributor
Author
|
@ricardoV94 not sure this is the solution you want (requires adding a new property to transform), but no better ones immediately come to mind. Feel free to propose a different approach if you see one. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #8336 +/- ##
==========================================
+ Coverage 79.80% 87.28% +7.48%
==========================================
Files 125 125
Lines 20526 20532 +6
==========================================
+ Hits 16380 17922 +1542
+ Misses 4146 2610 -1536
🚀 New features to boost your workflow:
|
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.
Description
transformed_value_logprobreduces the logp's trailing axes to match a lower-dim Jacobian, assuming (as for Simplex/Ordered) that a multivariate transform acts on the trailing axes.ZeroSumTransformmay zero-sum arbitrary axes, so for a non-trailing axis the reduced logp keeps the wrong axis and disagrees withlog_jac_det(which dropszerosum_axes). The shapes then differ, e.g.(3,) + (2,). This is silent with symbolic dims (both broadcastable) but raisesIncompatible Elemwise input shapesonce dims are static -- e.g. afterfreeze_dims_and_data, which the numba/jax sampling backends apply.Fix: expose the integrated-out axes via
ZeroSumTransform.jacobian_reduce_axesand havetransformed_value_logprobreduce those axes (falling back to the trailing axes for transforms that don't define them).ZeroSumNormalis unaffected (it only zero-sums trailing axes); batched transformed-logp shapes are preserved.Minimal repro (raised before, builds after)::
Related Issue
Checklist
Type of change