66
77
88@serializable ("bayesflow.adapters" )
9- class Nnpe (ElementwiseTransform ):
9+ class NNPE (ElementwiseTransform ):
1010 """Implements noisy neural posterior estimation (NNPE) as described in [1], which adds noise following a
1111 spike-and-slab distribution to the training data as a mild form of data augmentation to robustify against noisy
1212 real-world data (see [1, 2] for benchmarks).
@@ -48,6 +48,23 @@ def __init__(self, *, slab_scale: float = 0.25, spike_scale: float = 0.01, seed:
4848 self .rng = np .random .default_rng (seed )
4949
5050 def forward (self , data : np .ndarray , stage : str = "inference" , ** kwargs ) -> np .ndarray :
51+ """
52+ Add spike‐and‐slab noise (see “Notes” section of the class docstring for details) to `data` during training.
53+
54+ Parameters
55+ ----------
56+ data : np.ndarray
57+ Input array to be perturbed.
58+ stage : str, default='inference'
59+ If 'training', noise is added; else data is returned unchanged.
60+ **kwargs
61+ Unused keyword arguments.
62+
63+ Returns
64+ -------
65+ np.ndarray
66+ Noisy data when `stage` is 'training', otherwise the original input.
67+ """
5168 if stage != "training" :
5269 return data
5370 mixture_mask = self .rng .binomial (n = 1 , p = 0.5 , size = data .shape ).astype (bool )
@@ -57,6 +74,7 @@ def forward(self, data: np.ndarray, stage: str = "inference", **kwargs) -> np.nd
5774 return data + noise
5875
5976 def inverse (self , data : np .ndarray , ** kwargs ) -> np .ndarray :
77+ """Non-invertible transform."""
6078 return data
6179
6280 def get_config (self ) -> dict :
0 commit comments