NNPE#
- class bayesflow.augmentations.nnpe.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:
object
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, respectively.
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
[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. 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. 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.
Examples
>>> nnpe_aug = bf.augmentations.NNPE(spike_scale=0.01, slab_scale=0.2, per_dimension=True, seed=42) >>> dataset = bf.datasets.OnlineDataset( ... simulator=my_sim, ... batch_size=64, ... num_batches=100, ... adapter=None, ... augmentations={"data": nnpe_aug}, ... )
- DEFAULT_SPIKE = 0.01#
- DEFAULT_SLAB = 0.25#