SelfConsistencyLoss#
- class bayesflow.experimental.SelfConsistencyLoss(num_samples: int, adapter: Adapter | None = None, gradient: Sequence[Literal['prior', 'posterior', 'likelihood']] = ['posterior'], adapted: bool = False, parameter_keys: Sequence[str] | None = None, data_keys: Sequence[str] | None = None, **kwargs)[source]#
Bases:
objectSelf-consistency loss for joint training of prior, likelihood, and posterior.
Implements the self-consistency loss from Schmitt et al. (2023) and Mishra et al. (2026). The key identity is that for any parameter value theta, Bayes’ theorem implies:
log p(y) = log p(theta) + log p(y | theta) - log p(theta | y)
If the three components are consistent, the right-hand side should have zero variance across samples of theta drawn from any proposal. The SC loss minimizes this variance, pushing the components toward mutual consistency.
By default, theta is sampled from the current posterior (with
stop_gradient); estimators are therefore trained only through .log_prob evaluation rather than sampling. Only estimators listed ingradientreceive gradients. By default only the posterior is trained through the SC loss.Schmitt, M., Ivanova, D. R., Habermann, D., Köthe, U., Bürkner, P. C., & Radev, S. T. (2023). Leveraging self-consistency for data-efficient amortized Bayesian inference. arXiv preprint arXiv:2310.04395.
Mishra, A., Habermann, D., Schmitt, M., Radev, S. T., & Bürkner, P. C. (2025). Robust Amortized Bayesian Inference with Self-Consistency Losses on Unlabeled Data. arXiv preprint arXiv:2501.13483.
- Parameters:
- num_samplesint
Number of parameter samples drawn per observation to estimate the variance of log p(y).
- adapterAdapter, optional
Adapter shared across all components, used to map between the data/parameter space and the adapted (network) space. If None, an identity adapter is used.
- gradientsequence of {“prior”, “likelihood”, “posterior”}, optional
Which components receive gradients through the SC loss. All others are wrapped in
stop_gradient. Defaults to["posterior"].- adaptedbool, optional
Determines the space in which the SC loss is computed. Analytic distributions (prior, likelihood) are always assumed to be defined in the original data space; Estimators are always in the adapted (network) space. This flag controls which space all components are converted into before summing:
False(default): compute the loss in original (simulator) space. Estimators have their log-probs adjusted by-log|det J|to convert from adapted to data space; analytic distributions need no adjustment.True: compute the loss in adapted space. Analytic distributions have their log-probs adjusted by+log|det J|to convert from data to adapted space; Estimators need no adjustment.
- parameter_keyssequence of str, optional
Keys in the data dict that correspond to model parameters (theta). If None, inferred from the keys of the posterior’s sample output on the first call.
- data_keyssequence of str, optional
Keys in the data dict that correspond to observed data (y). Required when
adapted=Trueand the likelihood is analytic. If None and the likelihood is an Estimator, inferred from its sample output. Otherwise defaults to an empty list (no Jacobian correction for data).
- __call__(data: Mapping[str, Tensor], stage: str) Mapping[str, Tensor][source]#
Compute the SC loss for one batch.
- Parameters:
- dataMapping[str, Tensor]
A single batch of adapted data (tensors), containing observation keys.
- stagestr, optional
Current training stage (e.g., “training”, “validation”, “inference”). Controls the behavior of standardization and some metric computations (default is “training”).
- Returns:
- dict with key
"loss"holding the scalar SC loss for this batch.
- dict with key
- attach(estimators, adapter)[source]#
Bind the prior, likelihood, posterior estimators and an adapter.
Called by
SemiSupervisedEstimator.compileso that the loss can access the components it needs without them being passed at construction time.- Parameters:
- estimatorsdict
Must contain keys
"prior","likelihood", and"posterior".- adapterAdapter
Shared adapter.