Repository navigation
Description
Problem
The flows for marginal estimators (MarginalTrainer, marginal_nn) and for single-observation VI (VIPosterior with a flow q) z-score their input in the wrong place. All of them use build_zuko_unconditional_flow. With z-scoring on, which is the default, this function puts the z-score transform after the flow transforms. So the learned transforms get the raw data, and the z-scoring only acts on their output. The conditional builder build_zuko_flow uses the correct order: z-score first, then the flow. A comment in the function already questions the order.
This matters for data that is far from 0 or on a large scale. Zuko's spline transforms are the identity outside their bound of 5. For such data an NSF learns almost nothing, and the flow is close to a Gaussian fit. Affine flows such as MAF still train, but on badly scaled inputs. Users get none of the benefit of the default z-scoring.
This snippet shows the failure. Data around 1000 passes all five spline transforms unchanged:
import torch
from sbi.neural_nets.net_builders.flow import build_zuko_unconditional_flow
torch.manual_seed(0)
x = 1000 + 0.01 * torch.randn(500, 2)
flow = build_zuko_unconditional_flow("NSF", x, z_score_x="independent")
h = x[:1]
for t in flow.net.transform.transforms:
h = t(None)(h)
print(type(t).__name__, h)
# MaskedAutoregressiveTransform tensor([[999.9888, 999.9885]]), five times
# UnconditionalTransform tensor([[-1.1172, -1.1406]])The same function has a second bug. It removes num_bins together with other nflows-only arguments and does not pass it on as zuko's bins. So marginal_nn(ZukoFlowType.NSF, num_bins=16) builds a flow with zuko's default of 8 bins. MarginalNSFConfig builds 10 bins by default. The two entry points give different flows for the same settings.
How to approach it
- Apply the z-scoring before the flow transforms in
build_zuko_unconditional_flow, in the same order asbuild_zuko_flow. If possible, let both builders share the step that orders these transforms, so that they cannot drift apart again. - Make
marginal_nnandMarginalNSFConfigbuild the same flow for the same settings. One code path for both is better than a second mapping ofnum_bins. - Add tests that fail on
main. One test checks that the learned transforms receive z-scored data. One test checks thatmarginal_nnandMarginalNSFConfigbuild the same network for the same bin count. - Check the accuracy of VI and of the marginal estimators. CI skips the slow tests, so run
pytest -m slow tests/vi_test.pyand the marginal estimator tests yourself. Also compare one marginal fit on data far from 0 before and after the fix. - The new order changes the layout of the flow's modules. Check whether estimators saved before the fix still load, and say in the PR what changes for users.
How to contribute
Please comment here first with your proposal: what causes the problem, how you want to fix it, and how you want to test it (see the contributing guide). Write this comment yourself. Then open a PR that links this issue, describes how you tested the fix, and lists the slow tests you ran.
Activity
BHARATH0153 commentedon Oct 29, 2026
#2105 fixed in
fix: z-score before the flow transforms in unconditional Zuko flows (s…