Skip to content

Marginal and VI flows z-score their input after the flow instead of before it #2114

Closed

Description

@janfb

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

  1. Apply the z-scoring before the flow transforms in build_zuko_unconditional_flow, in the same order as build_zuko_flow. If possible, let both builders share the step that orders these transforms, so that they cannot drift apart again.
  2. Make marginal_nn and MarginalNSFConfig build the same flow for the same settings. One code path for both is better than a second mapping of num_bins.
  3. Add tests that fail on main. One test checks that the learned transforms receive z-scored data. One test checks that marginal_nn and MarginalNSFConfig build the same network for the same bin count.
  4. Check the accuracy of VI and of the marginal estimators. CI skips the slow tests, so run pytest -m slow tests/vi_test.py and the marginal estimator tests yourself. Also compare one marginal fit on data far from 0 before and after the fix.
  5. 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

  1. BHARATH0153 commented on Oct 29, 2026

    @BHARATH0153
    Contributor

    #2105 fixed in

  2. added a commit that references this issue on Oct 29, 2026
    6ca1a59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions