bayesflow.amortizers module#
- class bayesflow.amortizers.AmortizedTarget(*args, **kwargs)[source]#
Bases:
ABC
An abstract interface for an amortized learned distribution. Children should implement the following public methods:
compute_loss(self, input_dict, **kwargs)
sample(input_dict, **kwargs)
log_prob(input_dict, **kwargs)
- class bayesflow.amortizers.AmortizedPosterior(*args, **kwargs)[source]#
Bases:
Model
,AmortizedTarget
A wrapper to connect an inference network for parameter estimation with an optional summary network as in the original BayesFlow set-up described in the paper:
[1] Radev, S. T., Mertens, U. K., Voss, A., Ardizzone, L., & Köthe, U. (2020). BayesFlow: Learning complex stochastic models with invertible neural networks. IEEE Transactions on Neural Networks and Learning Systems.
But also allowing for augmented functionality, such as model misspecification detection in summary space:
[2] Schmitt, M., Bürkner, P. C., Köthe, U., & Radev, S. T. (2022). Detecting Model Misspecification in Amortized Bayesian Inference with Neural Networks arXiv preprint arXiv:2112.08866.
And learning of fat-tailed posteriors with a Student-t latent pushforward density:
[3] Jaini, P., Kobyzev, I., Yu, Y., & Brubaker, M. (2020, November). Tails of lipschitz triangular flows. In International Conference on Machine Learning (pp. 4673-4681). PMLR.
[4] Alexanderson, S., & Henter, G. E. (2020). Robust model training and generalisation with Studentising flows. arXiv preprint arXiv:2006.06599.
Serves as in interface for learning
p(parameters | data, context).
- __init__(inference_net, summary_net=None, latent_dist=None, latent_is_dynamic=False, summary_loss_fun=None, **kwargs)[source]#
Initializes a composite neural network to represent an amortized approximate posterior for a Bayesian generative model.
- Parameters:
- inference_nettf.keras.Model
An (invertible) inference network which processes the outputs of a generative model
- summary_nettf.keras.Model or None, optional, default: None
An optional summary network to compress non-vector data structures.
- latent_distcallable or None, optional, default: None
The latent distribution towards which to optimize the networks. Defaults to a multivariate unit Gaussian.
- latent_is_dynamicbool, optional, default: False
If set to True, assumes that
latent_dist
is a function of the condtion and takes a different shape depending on the condition. Useful for more expressive transforms of complex distributions, such as fat-tailed or highly-multimodal distributions.Important: In the case of dynamic latents, the user is responsible that the latent is appropriately parameterized! If not using
tensorflow_probability
, thelatent_dist
object needs to implement the following methods: -latent_dist(x).log_prob(z)
and -latent_dist(x).sample(n_samples)
- summary_loss_funcallable, str, or None, optional, default: None
The loss function which accepts the outputs of the summary network. If
None
, no loss is provided and the summary space will not be shaped according to a known distribution (see [2]). Ifsummary_loss_fun='MMD'
, the default loss from [2] will be used.- **kwargsdict, optional, default: {}
Additional keyword arguments passed to the
__init__
method of atf.keras.Model
instance.
- call(input_dict, return_summary=False, **kwargs)[source]#
Performs a forward pass through the summary and inference network given an input dictionary.
- Parameters:
- input_dictdict
Input dictionary containing the following mandatory keys, if
DEFAULT_KEYS
unchanged:parameters
- the latent model parameters over which a condition density is learnedsummary_conditions
- the conditioning variables (including data) that are first passed through a summary networkdirect_conditions
- the conditioning variables that the directly passed to the inference network- return_summarybool, optional, default: False
A flag which determines whether the learnable data summaries (representations) are returned or not.
- **kwargsdict, optional, default: {}
Additional keyword arguments passed to the networks For instance,
kwargs={'training': True}
is passed automatically during training.
- Returns:
- net_out or (net_out, summary_out)tuple of tf.Tensor
the outputs of
inference_net(theta, summary_net(x, c_s), c_d)
, usually a latent variable and log(det(Jacobian)), that is a tuple(z, log_det_J) or (sum_outputs, (z, log_det_J))
ifreturn_summary
is set to True and a summary network is defined.``
- compute_loss(input_dict, **kwargs)[source]#
Computes the loss of the posterior amortizer given an input dictionary, which will typically be the output of a Bayesian
GenerativeModel
instance.- Parameters:
- input_dictdict
Input dictionary containing the following mandatory keys, if
DEFAULT_KEYS
unchanged:parameters
- the latent model parameters over which a condition density is learnedsummary_conditions
- the conditioning variables that are first passed through a summary networkdirect_conditions
- the conditioning variables that the directly passed to the inference network- **kwargsdict, optional, default: {}
Additional keyword arguments passed to the networks For instance,
kwargs={'training': True}
is passed automatically during training.
- Returns:
- total_losstf.Tensor of shape (1,) - the total computed loss given input variables
- call_loop(input_list, return_summary=False, **kwargs)[source]#
Performs a forward pass through the summary and inference network given a list of dicts with the appropriate entries (i.e., as used for the standard call method).
This method is useful when GPU memory is limited or data sets have a different (non-Tensor) structure.
- Parameters:
- input_listlist of dicts, where each dict contains the following mandatory keys, if
DEFAULT_KEYS
unchanged: parameters
- the latent model parameters over which a condition density is learnedsummary_conditions
- the conditioning variables (including data) that are first passed through a summary networkdirect_conditions
- the conditioning variables that the directly passed to the inference network- return_summarybool, optional, default: False
A flag which determines whether the learnable data summaries (representations) are returned or not.
- **kwargsdict, optional, default: {}
Additional keyword arguments passed to the networks
- input_listlist of dicts, where each dict contains the following mandatory keys, if
- Returns:
- net_out or (net_out, summary_out)tuple of tf.Tensor
the outputs of
inference_net(theta, summary_net(x, c_s), c_d)
, usually a latent variable and log(det(Jacobian)), that is a tuple(z, log_det_J) or (sum_outputs, (z, log_det_J)) if return_summary is set to True and a summary network is defined.
- sample(input_dict, n_samples, to_numpy=True, **kwargs)[source]#
Generates random draws from the approximate posterior given a dictionary with conditonal variables.
- Parameters:
- input_dictdict
Input dictionary containing at least one of the following mandatory keys, if
DEFAULT_KEYS
unchanged:summary_conditions
: the conditioning variables (including data) that are first passed through a summary networkdirect_conditions
: the conditioning variables that the directly passed to the inference network- n_samplesint
The number of posterior draws (samples) to obtain from the approximate posterior
- to_numpybool, optional, default: True
Flag indicating whether to return the samples as a
np.ndarray
or atf.Tensor
.- **kwargsdict, optional, default: {}
Additional keyword arguments passed to the networks
- Returns:
- post_samplestf.Tensor or np.ndarray of shape (n_data_sets, n_samples, n_params)
The sampled parameters from the approximate posterior of each data set
- sample_loop(input_list, n_samples, to_numpy=True, **kwargs)[source]#
Generates random draws from the approximate posterior given a list of dicts with conditonal variables. Useful when GPU memory is limited or data sets have a different (non-Tensor) structure.
- Parameters:
- input_listlist of dictionaries, each dictionary having the following mandatory keys, if
DEFAULT_KEYS
unchanged: summary_conditions
: the conditioning variables (including data) that are first passed through a summary networkdirect_conditions
: the conditioning variables that the directly passed to the inference network- n_samplesint
The number of posterior draws (samples) to obtain from the approximate posterior
- to_numpybool, optional, default: True
Flag indicating whether to return the samples as a
np.ndarray
or atf.Tensor
- **kwargsdict, optional, default: {}
Additional keyword arguments passed to the networks
- input_listlist of dictionaries, each dictionary having the following mandatory keys, if
- Returns:
- post_samplestf.Tensor or np.ndarray of shape (n_datasets, n_samples, n_params)
The sampled parameters from the approximate posterior of each data set
- log_posterior(input_dict, to_numpy=True, **kwargs)[source]#
Calculates the approximate log-posterior of targets given conditional variables via the change-of-variable formula for a conditional normalizing flow.
- Parameters:
- input_dictdict
Input dictionary containing the following mandatory keys, if
DEFAULT_KEYS
unchanged:parameters
: the latent model parameters over which a conditional density (i.e., a posterior) is learnedsummary_conditions
: the conditioning variables (including data) that are first passed through a summary networkdirect_conditions
: the conditioning variables that are directly passed to the inference network- to_numpybool, optional, default: True
Flag indicating whether to return the lpdf values as a
np.ndarray
or atf.Tensor
- **kwargsdict, optional, default: {}
Additional keyword arguments passed to the networks
- Returns:
- log_posttf.Tensor or np.ndarray of shape (batch_size, n_obs)
the approximate log-posterior density of each each parameter
- class bayesflow.amortizers.AmortizedLikelihood(*args, **kwargs)[source]#
Bases:
Model
,AmortizedTarget
An interface for a surrogate model of a simulator, or an implicit likelihood
p(data | parameters, context)
.- __init__(surrogate_net, latent_dist=None, **kwargs)[source]#
Initializes a composite neural architecture representing an amortized emulator for the simulator (i.e., the implicit likelihood model).
- Parameters:
- surrogate_nettf.keras.Model
An (invertible) inference network which processes the outputs of the generative model.
- latent_distcallable or None, optional, default: None
The latent distribution towards which to optimize the surrogate network outputs. Defaults to a multivariate unit Gaussian.
- **kwargsdict, optional, default: {}
Additional keyword arguments passed to the
__init__
method of atf.keras.Model
instance.
- call(input_dict, **kwargs)[source]#
Performs a forward pass through the summary and inference network.
- Parameters:
- input_dictdict
Input dictionary containing the following mandatory keys, if
DEFAULT_KEYS
unchanged:observables
- the observables over which a condition density is learned (i.e., the data)conditions
- the conditioning variables that the directly passed to the inference network- **kwargsdict, optional, default: {}
Additional keyword arguments passed to the network For instance,
kwargs={'training': True}
is passed automatically during training.
- Returns:
- net_out
the outputs of
surrogate_net(theta, summary_net(x, c_s), c_d)
, usually a latent variable and log(det(Jacobian)), that is a tuple(z, log_det_J)
.
- call_loop(input_list, **kwargs)[source]#
Performs a forward pass through the surrogate network given a list of dicts with the appropriate entries (i.e., as used for the standard call method).
This method is useful when GPU memory is limited or data sets have a different (non-Tensor) structure.
- Parameters:
- input_listlist of dicts, where each dict contains the following mandatory keys, if
DEFAULT_KEYS
unchanged: observables
- the observables over which a condition density is learned (i.e., the data)conditions
- the conditioning variables that the directly passed to the inference network- **kwargsdict, optional, default: {}
Additional keyword arguments passed to the network
- input_listlist of dicts, where each dict contains the following mandatory keys, if
- Returns:
- net_out or (net_out, summary_out)tuple of tf.Tensor
the outputs of
inference_net(theta, summary_net(x, c_s), c_d)
, usually a latent variable and log(det(Jacobian)), that is a tuple(z, log_det_J)
.
- sample(input_dict, n_samples, to_numpy=True, **kwargs)[source]#
Generates n_samples random draws from the surrogate likelihood given input conditions.
- Parameters:
- input_dictdict
Input dictionary containing the following mandatory keys, if
DEFAULT_KEYS
unchanged:conditions
- the conditioning variables that are directly passed to the surrogate network- n_samplesint
The number of posterior samples to obtain from the approximate posterior
- to_numpybool, optional, default: True
Flag indicating whether to return the samples as a
np.ndarray
or atf.Tensor
- **kwargsdict, optional, default: {}
Additional keyword arguments passed to the network
- Returns:
- lik_samplestf.Tensor or np.ndarray of shape (n_datasets, n_samples, None)
A simulated batch of observables from the surrogate likelihood.
- sample_loop(input_list, n_samples, to_numpy=True, **kwargs)[source]#
Generates random draws from the surrogate network given a list of dicts with conditional variables. Useful when GPU memory is limited or data sets have a different (non-Tensor) structure.
- Parameters:
- input_listlist of dictionaries, each dictionary having the following mandatory keys (default):
conditions
- the conditioning variables that the directly passed to the surrogate network- n_samplesint
The number of posterior draws (samples) to obtain from the approximate posterior
- to_numpybool, optional, default: True
Flag indicating whether to return the samples as a np.array or a tf.Tensor
- **kwargsdict, optional, default: {}
Additional keyword arguments passed to the network
- Returns:
- post_samplestf.Tensor or np.ndarray of shape (n_data_sets, n_samples, data_dim)
the sampled parameters per data set
- log_likelihood(input_dict, to_numpy=True, **kwargs)[source]#
Calculates the approximate log-likelihood of targets given conditional variables via the change-of-variable formula for a conditional normalizing flow.
- Parameters:
- input_dictdict
Input dictionary containing the following mandatory keys, if
DEFAULT_KEYS
unchanged:observables
- the variables over which a condition density is learned (i.e., the observables)conditions
- the conditioning variables that the directly passed to the inference network- to_numpybool, optional, default: True
Boolean flag indicating whether to return the log-lik values as a
np.ndarray
or atf.Tensor
- **kwargsdict, optional, default: {}
Additional keyword arguments passed to the network
- Returns:
- log_liktf.Tensor or np.ndarray of shape (batch_size, n_obs)
the approximate log-likelihood of each data point in each data set
- log_prob(input_dict, to_numpy=True, **kwargs)[source]#
Identical to log_likelihood(input_dict, to_numpy, **kwargs).
- compute_loss(input_dict, **kwargs)[source]#
Computes the loss of the amortized given input data provided in input_dict.
- Parameters:
- input_dictdict
Input dictionary containing the following mandatory keys:
data
- the observables over which a condition density is learned (i.e., the observables)conditions
- the conditioning variables that the directly passed to the surrogate network- **kwargsdict, optional, default: {}
Additional keyword arguments passed to the network For instance,
kwargs={'training': True}
is passed automatically during simulation-based training.
- Returns:
- losstf.Tensor of shape (1,) - the total computed loss given input variables
- class bayesflow.amortizers.AmortizedPosteriorLikelihood(*args, **kwargs)[source]#
Bases:
Model
,AmortizedTarget
An interface for jointly learning a surrogate model of the simulator and an approximate posterior given a generative model, as proposed by:
[1] Radev, S. T., Schmitt, M., Pratz, V., Picchini, U., Köthe, U., & Bürkner, P. C. (2023). JANA: Jointly Amortized Neural Approximation of Complex Bayesian Models. arXiv preprint arXiv:2302.09125.
- __init__(amortized_posterior, amortized_likelihood, **kwargs)[source]#
Initializes a joint learner comprising an amortized posterior and an amortized emulator.
- Parameters:
- amortized_posterioran instance of AmortizedPosterior or a custom tf.keras.Model
The generative neural posterior approximator
- amortized_likelihoodan instance of AmortizedLikelihood or a custom tf.keras.Model
The generative neural likelihood approximator
- **kwargsdict, optional, default: {}
Additional keyword arguments passed to the
__init__
method of atf.keras.Model
instance
- call(input_dict, **kwargs)[source]#
Performs a forward pass through both amortizers.
- Parameters:
- input_dictdict
Input dictionary containing the following mandatory keys: posterior_inputs - The input dictionary for the amortized posterior likelihood_inputs - The input dictionary for the amortized likelihood
- Returns:
- (post_out, lik_out)tuple
The outputs of the posterior and likelihood networks given input variables.
- compute_loss(input_dict, **kwargs)[source]#
Computes the loss of the join amortizer by summing the corresponding amortized posterior and likelihood losses.
- Parameters:
- input_dictdict
Nested input dictionary containing the following mandatory keys, if DEFAULT_KEYS unchanged:: posterior_inputs - The input dictionary for the amortized posterior likelihood_inputs - The input dictionary for the amortized likelihood
- Returns:
- total_lossesdict
A dictionary with keys Post.Loss and Lik.Loss containing the individual losses for the two amortizers.
- log_likelihood(input_dict, to_numpy=True, **kwargs)[source]#
Calculates the approximate log-likelihood of data given conditional variables via the change-of-variable formula for conditional normalizing flows.
- Parameters:
- input_dictdict
Input dictionary containing the following mandatory keys, if DEFAULT_KEYS unchanged:
observables - the variables over which a condition density is learned (i.e., the observables) conditions - the conditioning variables that are directly passed to the inference network
OR a nested dictionary with key likelihood_inputs containing the above input dictionary
- to_numpybool, optional, default: True
Flag indicating whether to return the samples as a np.array or a tf.Tensor
- Returns:
- log_liktf.Tensor of shape (batch_size, n_obs)
the approximate log-likelihood of each data point in each data set
- log_posterior(input_dict, to_numpy=True, **kwargs)[source]#
Calculates the approximate log-posterior of targets given conditional variables via the change-of-variable formula for conditional normalizing flows.
- Parameters:
- input_dictdict
Input dictionary containing the following mandatory keys, if DEFAULT_KEYS unchanged:
parameters - the latent generative model parameters over which a condition density is learned summary_conditions - the conditioning variables that are first passed through a summary network direct_conditions - the conditioning variables that the directly passed to the inference network
OR a nested dictionary with key posterior_inputs containing the above input dictionary
- Returns:
- log_posttf.Tensor of shape (batch_size, n_obs)
the approximate log-likelihood of each data point in each data set
- log_prob(input_dict, to_numpy=True, **kwargs)[source]#
Identical to calling separate log_likelihood() and log_posterior().
- Returns:
- out_dictdict with keys log_posterior and log_likelihood corresponding
- to the computed log_pdfs of the approximate posterior and likelihood.
- sample_data(input_dict, n_samples, to_numpy=True, **kwargs)[source]#
Generates n_samples random draws from the surrogate likelihood given input conditions.
- Parameters:
- input_dictdict
Input dictionary containing the following mandatory keys, if DEFAULT_KEYS unchanged:
conditions - the conditioning variables that the directly passed to the inference network
OR a nested dictionary with key likelihood_inputs containing the above input dictionary
- n_samplesint
The number of posterior samples to obtain from the approximate posterior
- to_numpybool, optional, default: True
Flag indicating whether to return the samples as a np.array or a tf.Tensor
- Returns:
- lik_samplestf.Tensor or np.ndarray of shape (n_datasets, n_samples, None)
Simulated observables from the surrogate likelihood.
- sample_parameters(input_dict, n_samples, to_numpy=True, **kwargs)[source]#
Generates random draws from the approximate posterior given conditonal variables.
- Parameters:
- input_dictdict
Input dictionary containing the following mandatory keys, if DEFAULT KEYS unchanged:
summary_conditions : the conditioning variables (including data) that are first passed through a summary network direct_conditions : the conditioning variables that the directly passed to the inference network
OR a nested dictionary with key posterior_inputs containing the above input dictionary
- n_samplesint
The number of posterior samples to obtain from the approximate posterior
- to_numpybool, optional, default: True
Boolean flag indicating whether to return the samples as a np.array or a tf.Tensor
- Returns:
- post_samplestf.Tensor or np.ndarray of shape (n_datasets, n_samples, n_params)
the sampled parameters per data set
- sample(input_dict, n_post_samples, n_lik_samples, to_numpy=True, **kwargs)[source]#
Identical to calling sample_parameters() and sample_data() separately.
- Returns:
- out_dictdict with keys posterior_samples and likelihood_samples corresponding
- to the n_samples from the approximate posterior and likelihood, respectively
- class bayesflow.amortizers.AmortizedModelComparison(*args, **kwargs)[source]#
Bases:
Model
An interface to connect an evidential network for Bayesian model comparison with an optional summary network, as described in the original paper on evidential neural networks for model comparison according to [1, 2]:
[1] Radev, S. T., D’Alessandro, M., Mertens, U. K., Voss, A., Köthe, U., & Bürkner, P. C. (2021). Amortized bayesian model comparison with evidential deep learning. IEEE Transactions on Neural Networks and Learning Systems.
[2] Elsemüller, L., Schnuerch, M., Bürkner, P. C., & Radev, S. T. (2023). A Deep Learning Method for Comparing Bayesian Hierarchical Models. arXiv preprint arXiv:2301.11873.
Note: the original paper [1] does not distinguish between the summary and the evidential networks, but treats them as a whole, with the appropriate architecture dictated by the model application. For the sake of consistency and modularity, the BayesFlow library separates the two constructs.
- __init__(inference_net, summary_net=None, loss_fun=None)[source]#
Initializes a composite neural architecture for amortized bayesian model comparison.
- Parameters:
- inference_nettf.keras.Model
A neural network which outputs model evidences.
- summary_nettf.keras.Model or None, optional, default: None
An optional summary network
- loss_funcallable or None, optional, default: None
The loss function which accepts the outputs of the amortizer. If None, the loss will be the log-loss.
- call(input_dict, return_summary=False, **kwargs)[source]#
Performs a forward pass through both networks.
- Parameters:
- input_dictdict
Input dictionary containing the following mandatory keys, if DEFAULT_KEYS unchanged summary_conditions - the conditioning variables that are first passed through a summary network direct_conditions - the conditioning variables that the directly passed to the evidential network model_indices - the ground-truth, one-hot encoded model indices sampled from the model prior
- return_summarybool, optional, default: False
Indicates whether the summary network outputs are returned along the estimated evidences.
- Returns:
- net_outtf.Tensor of shape (batch_size, num_models) or tuple of (net_out (batch_size, num_models),
summary_out (batch_size, summary_dim)), the latter being the summary network outputs, if
return_summary is True
.
- posterior_probs(input_dict, to_numpy=True, **kwargs)[source]#
Compute posterior model probabilities (PMPs) given a dictionary with observed or simulated data.
- Parameters:
- input_dictdict
Input dictionary containing at least one of the following mandatory keys, if DEFAULT_KEYS unchanged summary_conditions - the conditioning variables that are first passed through a summary network direct_conditions - the conditioning variables that the directly passed to the evidential network
- to_numpybool, optional, default: True
Flag indicating whether to return the PMPs a
np.ndarray
or atf.Tensor
- **kwargsdict, optional, default: {}
Additional keyword arguments passed to the networks
- Returns:
- outtf.Tensor of shape (batch_size, …, num_models)
The approximated PMPs
- compute_loss(input_dict, **kwargs)[source]#
Computes the loss of the amortized model comparison instance.
- Parameters:
- input_dictdict
Input dictionary containing the following mandatory keys, if DEFAULT_KEYS unchanged:: summary_conditions - the conditioning variables that are first passed through a summary network direct_conditions - the conditioning variables that the directly passed to the evidence network model_indices - the ground-truth, one-hot encoded model indices sampled from the model prior
- Returns:
- losstf.Tensor of shape (1,) - the total computed loss given input variables
- class bayesflow.amortizers.TwoLevelAmortizedPosterior(*args, **kwargs)[source]#
Bases:
Model
,AmortizedTarget
An interface for estimating arbitrary two level hierarchical Bayesian models.
- __init__(local_amortizer, global_amortizer, summary_net=None, **kwargs)[source]#
Creates an wrapper for estimating two-level hierarchical Bayesian models.
- Parameters:
- local_amortizerbayesflow.amortizers.AmortizedPosterior
A posterior amortizer without a summary network which will estimate the full conditional of the (varying numbers of) local parameter vectors.
- global_amortizerbayesflow.amortizers.AmortizedPosterior
A posterior amortizer without a summary network which will estimate the joint posterior of hyperparameters and optional shared parameters given a representation of an entire hierarchical data set. If both hyper- and shared parameters are present, the first dimensions correspond to the hyperparameters and the remaining ones correspond to the shared parameters.
- summary_nettf.keras.Model or None, optional, default: None
An optional summary network to compress non-vector data structures.
- **kwargsdict, optional, default: {}
Additional keyword arguments passed to the
__init__
method of atf.keras.Model
instance.
- sample(input_dict, n_samples, to_numpy=True, **kwargs)[source]#
Obtains samples from the joint hierarchical posterior given observations.
Important: Currently works only for single hierarchical data sets!
- Parameters:
- input_dictdict
Input dictionary containing the following mandatory keys, if DEFAULT_KEYS unchanged: summary_conditions - the hierarchical data set (to be embedded by the summary net) As well as optional keys: direct_local_conditions - (Context) variables used to condition the local posterior direct_global_conditions - (Context) variables used to condition the global posterior
- n_samplesint
The number of posterior draws (samples) to obtain from the approximate posterior
- to_numpybool, optional, default: True
Flag indicating whether to return the samples as a np.array or a tf.Tensor
- **kwargsdict, optional, default: {}
Additional keyword arguments passed to the summary network as the amortizers
- Returns:
- samples_dictdict
A dictionary with keys global_samples and local_samples Local samples will hold an array-like of shape (num_replicas, num_samples, num_local) and local samples will hold an array-like of shape (num_samples, num_hyper + num_shared), if optional shared patameters are present, otherwise (num_samples, num_hyper),
- class bayesflow.amortizers.AmortizedPointEstimator(*args, **kwargs)[source]#
Bases:
Model
An interface to connect a neural point estimator for Bayesian estimation with an optional summary network [1].
[1] Sainsbury-Dale, M., Zammit-Mangion, A., & Huser, R. (2024). Likelihood-free parameter estimation with neural Bayes estimators. The American Statistician, 78(1), 1-14.
- __init__(inference_net, summary_net=None, norm_ord=2, loss_fun=None)[source]#
Initializes a composite neural architecture for amortized bayesian model comparison.
- Parameters:
- inference_nettf.keras.Model
A neural network whose final output dimension equals that of the target quantities.
- summary_nettf.keras.Model or None, optional, default: None
An optional summary network
- norm_ordint or np.inf, optional, default: 2
The order of the norm used as a loss function for the point estimator. Should be in
[1, 2, np.inf]
.- loss_funcallable or None, optional, default: None
If not None, it overrides the norm keyword argument.
- call(input_dict, return_summary=False, **kwargs)[source]#
Performs a forward pass through the summary and inference network given an input dictionary.
- Parameters:
- input_dictdict
Input dictionary containing the following mandatory keys, if
DEFAULT_KEYS
unchanged:parameters
- the latent model parameters over which a condition density is learnedsummary_conditions
- the conditioning variables (including data) that are first passed through a summary networkdirect_conditions
- the conditioning variables that the directly passed to the inference network- return_summarybool, optional, default: False
A flag which determines whether the learnable data summaries (representations) are returned or not.
- **kwargsdict, optional, default: {}
Additional keyword arguments passed to the networks For instance,
kwargs={'training': True}
is passed automatically during training.
- Returns:
- net_out or (net_out, summary_out)tuple of tf.Tensor
The outputs of
inference_net(summary_net(x, c_s), c_d)
, usually a batch of point estimates, that is, a tensorestimates
or(sum_outputs, estimates)
ifreturn_summary
is set to True and a summary network is defined.
- estimate(input_dict, to_numpy=True, **kwargs)[source]#
Obtains Bayesian point estimates given the data in input_dict.
- Parameters:
- input_dictdict
Input dictionary containing at least one of the following mandatory keys, if
DEFAULT_KEYS
unchanged:summary_conditions
: the conditioning variables (including data) that are first passed through a summary networkdirect_conditions
: the conditioning variables that the directly passed to the inference network- to_numpybool, optional, default: True
Flag indicating whether to return the samples as a
np.ndarray
or atf.Tensor
.- **kwargsdict, optional, default: {}
Additional keyword arguments passed to the networks.
- Returns:
- estimatestf.Tensor or np.ndarray of shape (num_data_sets, num_params)
The point estimates of the parameters for each data set.
- compute_loss(input_dict, **kwargs)[source]#
Computes the loss of the posterior amortizer given an input dictionary, which will typically be the output of a Bayesian
GenerativeModel
instance.- Parameters:
- input_dictdict
Input dictionary containing the following mandatory keys, if
DEFAULT_KEYS
unchanged:parameters
- the latent model parameters over which a condition density is learnedsummary_conditions
- the conditioning variables that are first passed through a summary networkdirect_conditions
- the conditioning variables that the directly passed to the inference network- **kwargsdict, optional, default: {}
Additional keyword arguments passed to the networks For instance,
kwargs={'training': True}
is passed automatically during training.
- Returns:
- total_losstf.Tensor of shape (1,) - the total computed loss given input variables