Repository navigation
Description
Bug description
I read the warning in standardizing_net, which says a one-sample batch is allowed "because you might be loading a pre-trained net", and looked at #1577, which added that behavior. Then I tried to rebuild estimators from one sample.
This is bad because someone who trains a model and saves its weights cannot load them later: when they rebuild the estimator with one dummy example, it fails for every estimator I tried (maf, nsf, mdn, zuko_maf).
The cause is on two sides:
- theta side:
z_standardizationcomputestorch.stdof a single sample, which is NaN, and the z-scoring transform rejects it ("Scale must be strictly positive"). The one-sample case from Update sbiutils.py to use one-dimensional batch #1577 is only handled instandardizing_net. - x side: in
standardizing_netthe one-sample branch setsstd = torch.ones(1), shape[1], but the saved model hasstdof shape[x_dim], soload_state_dictfails with a size mismatch.
Could I open a PR for this ? I would use a std of ones with the right shape in both places and add a test that rebuilds each estimator from one sample and loads trained weights.
Minimal code example
import torch
from sbi.neural_nets import posterior_nn
torch.manual_seed(0)
theta, x = torch.randn(500, 2), torch.randn(500, 3)
for model in ["maf", "nsf", "mdn", "zuko_maf"]:
trained = posterior_nn(model)(theta, x)
try:
# rebuild with one dummy example to load the saved weights
dummy = posterior_nn(model)(theta[:1], x[:1])
dummy.load_state_dict(trained.state_dict())
print(model, "OK")
except Exception as e:
print(model, type(e).__name__, str(e)[:100])Error message or traceback
maf ValueError: Scale must be strictly positive.
nsf ValueError: Scale must be strictly positive.
mdn ValueError: transform_input scale (second row) must be strictly positive, got values in range [nan, nan]
zuko_maf RuntimeError: Error(s) in loading state_dict for ZukoFlow: size mismatch for _embedding_net.0._std
sbi version
main (e1d488e)
Python version
Python 3.12.16
Device
CPU
Inference method
NPE
Single-round or multi-round
None
Additional context
Ubuntu on WSL2, torch 2.15.0 (CPU), sbi installed from source. #2171 also edits z_standardization, but only the structured branch.
Activity
fix: z-score from one row with a std of ones, so every builder can lo…
janfb commentedon Nov 17, 2026
@clarkf19 thanks for the clear report and the reproduction, I can confirm both sides on
main.The first part of a fix was already in PR #2155, I now added the second part: a net built from one row now gets the row as its mean and ones as its std, on the theta and the x side, so trained weights load into it. A new test rebuilds every estimator from one row, loads trained weights and compares the outputs. So no separate PR is needed for this one.
While checking this, I also added a section to the save-and-load guide on saving only the network weights, which is the use case behind your report.
Thanks for tracking this down, Jan
clarkf19 commentedon Nov 17, 2026
Okay, thanks @janfb for fixing it and for adding the save-and-load section to the docs! I ran my reproduction on #2155 and all four estimators (maf, nsf, mdn, zuko_maf) now load the trained weights and give the same log_prob.
fix: z-scoring layers move with .to(), and nets rebuilt from one row …