Repository navigation
z_score correct order in Zuko - #1492
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1492 +/- ##
===========================================
- Coverage 89.69% 79.04% -10.66%
===========================================
Files 121 121
Lines 9347 9347
===========================================
- Hits 8384 7388 -996
- Misses 963 1959 +996
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
|
Double checked this and I think you're right @anastasiakrouglova, agreed that it's great that you caught this! As a test, I suggest doing a quick test (i.e. without training), because even if the input data is not normalized the network might be able to learn something good (indeed this may have been happening already). I have a quick idea and will post the results here asap |
|
Ok, here is the sanity check I promised: #import stuff
from sbi.utils.sbiutils import standardizing_transform_zuko
from zuko.lazy import LazyComposedTransform
from zuko.flows import UnconditionalTransform
from zuko.transforms import SigmoidTransform
import torch
#define data with very large scale
torch.random.manual_seed(0)
theta = 10000*torch.randn(1000,5)
#define a standardizing transform and a sigmoid transform (which maps everything to (0,1))
transform1 = standardizing_transform_zuko(theta)
transform2 = UnconditionalTransform(SigmoidTransform)
#compose the transforms in both orders
standardizing_first = LazyComposedTransform(*[transform1,transform2])
standardizing_last = LazyComposedTransform(*[transform2,transform1])
#Because the data has a large range, the standardizing transform essentially divides the data by 10000.
#If the standardizing transform is called after the sigmoid, then everything will get mapped to 0.
#If the standardizing transform is called before the sigmoid, then we apply sigmoid roughly to
#standardized data, giving a reasonable scale for our outputs without training.
z1 = standardizing_first()(theta)
z2 = standardizing_last()(theta)
print(f"data range standardizing first: min:{z1.min().item():.4f}, max:{z1.max().item():.4f}")
print(f"data range standardizing last: min:{z2.min().item():.4f}, max:{z2.max().item():.4f}")This gives the output: So indeed we should be calling the standardizing transform first, not last as before the change from @anastasiakrouglova. |
janfb
left a comment
There was a problem hiding this comment.
Yes, makes sense and we can merge this now!
Thanks @gmoss13 for the double-check and thanks again @anastasiakrouglova for this important bug fix! 🎉
…2105) build_zuko_unconditional_flow applied the z-score after the flow transforms, so the transforms saw raw data. For data far from 0 or at a small scale, NSF splines sat in their identity tails and the flow stayed at the Gaussian of the z-score with zero gradients, and MAF trained on badly scaled inputs. Both Zuko builders now add the z-score first through one helper, as the conditional flows have done since PR #1492. This affects MarginalTrainer, marginal_nn, and VIPosterior with a user-supplied theta_transform; VI's default link transform already standardized theta. NCSF models data on [-pi, pi), which no affine z-score preserves. Both NCSF configs now offer z_score_input="none" only, both builders raise for an NCSF with an input transform, and build_zuko_ncsf and marginal_nn default to no z-score for NCSF. marginal_nn now builds the per-model marginal configs, so it passes num_bins on as Zuko's bins and builds the same flow as MarginalNSFConfig for the same settings. A setting the chosen model does not use now raises, as in the other factories, and None is no longer read as unset for its integer arguments. The marginal and conditional configs of NSF, NCSF, BPF and SOSPF now offer randperm, which Zuko accepts for all flows built on MAF. marginal_nn no longer takes num_components, which never reached a marginal flow, and the legacy MarginalFlowConfig and its base _EstimatorBuilderBase are removed. Flows saved with state_dict before this change do not load into flows built after it, because the transform order changed: a strict load raises, and a non-strict load puts weights into the wrong layers. Pickled estimators keep their old order and behaviour. Closes #2114.
Bug resolved:
The order of the transformations and standardization has been changed: we first want to standardize the input and then perform a series of transformations.