NNPE#
- class bayesflow.adapters.transforms.NNPE(*, spike_scale: _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, slab_scale: _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, per_dimension: bool = True, seed: int | None = None)[source]#
Bases:
ElementwiseTransform
Implements noisy neural posterior estimation (NNPE) as described in [1], which adds noise following a spike-and-slab distribution to the training data as a mild form of data augmentation to robustify against noisy real-world data (see [1, 2] for benchmarks). Adds the options of automatic noise scale determination and dimensionwise noise application to the original implementation in [1] to provide more flexibility in dealing with unstandardized and heterogeneous data.
[1] Ward, D., Cannon, P., Beaumont, M., Fasiolo, M., & Schmon, S. (2022). Robust neural posterior estimation and statistical model criticism. Advances in Neural Information Processing Systems, 35, 33845-33859. [2] Elsemüller, L., Pratz, V., von Krause, M., Voss, A., Bürkner, P. C., & Radev, S. T. (2025). Does Unsupervised Domain Adaptation Improve the Robustness of Amortized Bayesian Inference? A Systematic Evaluation. arXiv preprint arXiv:2502.04949.
- Parameters:
- spike_scalefloat or np.ndarray or None, default=None
The scale of the spike (Normal) distribution. Automatically determined if None (see “Notes” section). Expects a float if per_dimension=False or a 1D array of length data.shape[-1] if per_dimension=True.
- slab_scalefloat or np.ndarray or None, default=None
The scale of the slab (Cauchy) distribution. Automatically determined if None (see “Notes” section). Expects a float if per_dimension=False or a 1D array of length data.shape[-1] if per_dimension=True.
- per_dimensionbool, default=True
If true, noise is applied per dimension of the last axis of the input data. If false, noise is applied globally. Thus, if per_dimension=True, any provided scales must be arrays with shape (n_dimensions,) and automatic scale determination occurs separately per dimension. If per_dimension=False, provided scales must be floats and automatic scale determination occurs globally. The original implementation in [1] uses global application (i.e., per_dimension=False), whereas dimensionwise is recommended if the data dimensions are heterogeneous.
- seedint or None
The seed for the random number generator. If None, a random seed is used. Used instead of np.random.Generator here to enable easy serialization.
Notes
The spike-and-slab distribution consists of a mixture of a Normal distribution (spike) and Cauchy distribution (slab), which are applied based on a Bernoulli random variable with p=0.5.
The scales of the spike and slab distributions can be set manually, or they are automatically determined by scaling the default scales of [1] (which expect standardized data) by the standard deviation of the input data. For automatic determination, the standard deviation is determined either globally (if per_dimension=False) or per dimension of the last axis of the input data (if per_dimension=True). Note that automatic scale determination is applied batch-wise in the forward method, which means that determined scales can vary between batches due to varying standard deviations in the batch input data.
The original implementation in [1] can be recovered by applying the following settings on standardized data: - spike_scale=0.01 - slab_scale=0.25 - per_dimension=False
Examples
>>> adapter = bf.Adapter().nnpe(["x"])
- DEFAULT_SPIKE = 0.01#
- DEFAULT_SLAB = 0.25#
- forward(data: ndarray, stage: str = 'inference', **kwargs) ndarray [source]#
Add spike‐and‐slab noise to data during training, using automatic scale determination if not provided (see “Notes” section of the class docstring for details).
- Parameters:
- datanp.ndarray
Input array to be perturbed.
- stagestr, default=’inference’
If ‘training’, noise is added; else data is returned unchanged.
- **kwargs
Unused keyword arguments.
- Returns:
- np.ndarray
Noisy data when stage is ‘training’, otherwise the original input.