API

Dataloaders

Inference

sbi Runners

pydelfi Runners

Lampe Runners

pydelfi_wrappers

Validation

Runner

Metrics

Embedding

Module providing compression networks for data.

class embedding.fcn.FCN(*args: Any, **kwargs: Any)[source]

Fully connected network to compress data.

Parameters:
  • n_hidden (List[int]) – number of hidden units per layer

  • act_fn (str) – activation function to use

  • n_input (int) – dimensionality of the input (optional)

forward(x: torch.Tensor) torch.Tensor[source]

Forward pass of the neural network, returns the compressed data vector.

Parameters:

x (torch.Tensor) – input

Returns:

data

Return type:

torch.Tensor

initialize_model(n_input: int)[source]

Initialize network once the input dimensionality is known.

Parameters:

n_input (int) – input dimensionality

Utils

NDEs (Pytorch)

Module to provide loading functions for ndes in various backends.

All Mixture Density Networks (mdn) have the configuration:

hidden_features (int): width of hidden layers (each MDN has 3 hidden layers) num_components (int): number of Gaussian components in the mixture model

All flow-based models (maf, nsf, made) have the configuration:

hidden_features (int): width of hidden layers in the coupling layers num_transforms (int): number of coupling layers

Linear classifiers (linear) have no arguments.

MLP and ResNet classifiers (mlp, resnet) have the configuration:

hidden_features (int): width of hidden layers (each has 2 hidden layers)

class utils.ndes_pt.LampeEnsemble(*args: Any, **kwargs: Any)[source]

Simple module to wrap an ensemble of NPE models.

forward(theta: torch.Tensor, x: torch.Tensor) torch.Tensor[source]
log_prob(theta: torch.Tensor, x: torch.Tensor) torch.Tensor[source]
potential(theta: torch.Tensor, x: torch.Tensor) torch.Tensor
sample(shape: tuple, x: Any, show_progress_bars: bool = True)[source]
to(device)[source]
class utils.ndes_pt.LampeNPE(*args: Any, **kwargs: Any)[source]

Simple wrapper to add an embedding network to an NPE model.

flow(x: torch.Tensor)[source]
forward(theta: torch.Tensor, x: Any) torch.Tensor[source]
potential(theta: torch.Tensor, x: Any) torch.Tensor
sample(shape: tuple, x: torch.Tensor, show_progress_bars: bool = True) torch.Tensor[source]

Accept-reject sampling

to(device)[source]
utils.ndes_pt.load_nde_lampe(model: str, embedding_net: torch.nn.Module = torch.nn.Identity, device: str | None = 'cpu', x_normalize: bool = True, theta_normalize: bool = True, engine: str = 'NPE', repeats=1, **model_args)[source]

Load an nde from lampe. Models include:

For more info, see zuko at https://zuko.readthedocs.io/en/stable/index.html

Parameters:
  • model (str) – model to use. One of: mdn, maf, nsf, ncsf, cnf, nice, sospf, gf, naf.

  • embedding_net (nn.Module, optional) – embedding network to use. Defaults to nn.Identity().

  • device (str, optional) – device to use. Defaults to ‘cpu’.

  • x_normalize (bool, optional) – whether to z-normalize x. Defaults to True.

  • theta_normalize (bool, optional) – whether to z-normalize theta. Defaults to True.

  • engine (str, optional) – dummy argument to match sbi interface. Must be set to ‘NPE’ or will be overwritten.

  • **model_args – additional arguments to pass to the model.

utils.ndes_pt.load_nde_sbi(engine: str, model: str, embedding_net: torch.nn.Module = torch.nn.Identity, repeats=1, **model_args)[source]

Load an nde from sbi.

Parameters:
  • engine (str) – engine to use. One of: NPE, NLE, NRE, SNPE, SNLE, or SNRE.

  • model (str) – model to use. One of: mdn, maf, nsf, made, linear, mlp, resnet.

  • embedding_net (nn.Module, optional) – embedding network to use. Defaults to nn.Identity().

  • repeats (int, optional) – number of models to load. Defaults to 1.

  • **model_args – additional arguments to pass to the model.

NDEs (Tensorflow)

Module to provide loading functions for ndes in pydelfi.

All Mixture Density Networks (mdn) have the configuration:

hidden_features (int): width of hidden layers (each MDN has 3 hidden layers) num_components (int): number of Gaussian components in the mixture model

All flow-based models (maf) have the configuration:

hidden_features (int): width of hidden layers in the coupling layers num_transforms (int): number of coupling layers

utils.ndes_tf.load_nde_pydelfi(n_params: int, n_data: int, model: str, index: int = 0, engine: str = 'NLE', **model_args)[source]

Load an nde from pydelfi.

Parameters:
  • n_params (int) – dimensionality of parameters

  • n_data (int) – dimensionality of data points

  • model (str) – model to use. One of: mdn, maf.

  • index (int, optional) – index of the nde in the ensemble. Defaults to 0.

  • engine (str, optional) – dummy argument to match sbi interface. Must be set to ‘NLE’ or will be overwritten.

  • **model_args – additional arguments to pass to the model.

Prior Distributions (Pytorch)

Wrapper module to import distributions from torch.distributions and make their configuration easier in the ltu-ili interface.

Specifically, if we’re using a vector of parameters, we want to be able to pass the vector to the log_prob method of the distribution and return a scalar. This is not the default behavior of several distributions in torch.distributions, so we wrap them here.

class utils.distributions_pt.CustomIndependent(*args: Any, **kwargs: Any)[source]
to(device)[source]
class utils.distributions_pt.IndependentBeta(*args: Any, **kwargs: Any)
class utils.distributions_pt.IndependentCauchy(*args: Any, **kwargs: Any)
class utils.distributions_pt.IndependentChi2(*args: Any, **kwargs: Any)
class utils.distributions_pt.IndependentExponential(*args: Any, **kwargs: Any)
class utils.distributions_pt.IndependentFisherSnedecor(*args: Any, **kwargs: Any)
class utils.distributions_pt.IndependentGamma(*args: Any, **kwargs: Any)
class utils.distributions_pt.IndependentGumbel(*args: Any, **kwargs: Any)
class utils.distributions_pt.IndependentHalfCauchy(*args: Any, **kwargs: Any)
class utils.distributions_pt.IndependentHalfNormal(*args: Any, **kwargs: Any)
class utils.distributions_pt.IndependentLaplace(*args: Any, **kwargs: Any)
class utils.distributions_pt.IndependentLogNormal(*args: Any, **kwargs: Any)
class utils.distributions_pt.IndependentNormal(*args: Any, **kwargs: Any)
class utils.distributions_pt.IndependentPareto(*args: Any, **kwargs: Any)
class utils.distributions_pt.IndependentStudentT(*args: Any, **kwargs: Any)
class utils.distributions_pt.IndependentTruncatedNormal(*args: Any, **kwargs: Any)
Distribution

alias of _UnivariateTruncatedNormal

class utils.distributions_pt.IndependentUniform(*args: Any, **kwargs: Any)
class utils.distributions_pt.IndependentVonMises(*args: Any, **kwargs: Any)
class utils.distributions_pt.IndependentWeibull(*args: Any, **kwargs: Any)
class utils.distributions_pt.LowRankMultivariateNormal(*args: Any, **kwargs: Any)[source]
class utils.distributions_pt.MultivariateNormal(*args: Any, **kwargs: Any)[source]
utils.distributions_pt.Uniform

alias of IndependentUniform

Prior Distributions (Tensorflow)

Wrapper module to import distributions from pydelfi.priors and make their configuration easier in the sbi interface.

class utils.distributions_tf.IndependentNormal(loc, scale, device='cpu')[source]
draw()[source]
logpdf(x)[source]
pdf(x)[source]
class utils.distributions_tf.IndependentTruncatedNormal(*args: Any, **kwargs: Any)[source]

Note the pdf and logpdf as implemented in pydelfi are not normalized.

class utils.distributions_tf.MultivariateTruncatedNormal(*args: Any, **kwargs: Any)[source]

Note the pdf and logpdf as implemented in pydelfi are not normalized.

class utils.distributions_tf.Uniform(*args: Any, **kwargs: Any)[source]

Samplers

Custom samplers for sampling posteriors for Likelihood Estimation and Ratio Estimation models. Currently supports emcee samplers for both sbi and pydelfi backends, and pyro samplers only for the sbi backend.

class utils.samplers.DirectSampler(posterior: sbi.inference.posteriors.base_posterior.NeuralPosterior)[source]

Sampler class for posteriors with a direct sampling method, i.e. amortized posterior inference models.

Parameters:

posterior (Posterior) – posterior object to sample from, must have a .sample method allowing for direct sampling.

sample(nsteps: int, x: Any, progress: bool = False, **kwargs) ndarray[source]

Sample nsteps samples from the posterior, evaluated at data x.

Parameters:
  • nsteps (int) – number of samples to draw

  • x (np.ndarray) – data to evaluate the posterior at

  • progress (bool, optional) – whether to show progress bar. Defaults to False.

  • **kwargs – additional keyword arguments to pass to the

  • method (posterior's sample)

class utils.samplers.EmceeSampler(posterior: sbi.inference.posteriors.base_posterior.NeuralPosterior, num_chains: int = -1, thin: int = 10, burn_in: int = 100)[source]

Sampler class for emcee’s EnsembleSampler

Parameters:
  • posterior (Posterior) – posterior object to sample from, must have a .potential method specifiying the log posterior

  • num_chains (int, optional) – number of chains to sample from. Defaults to os.cpu_count()-1

  • thin (int, optional) – thinning factor for the chains. Defaults to 10

  • burn_in (int, optional) – number of steps to discard as burn-in. Defaults to 100

sample(nsteps: int, x: ndarray, progress: bool = False, skip_initial_state_check: bool = False, **kwargs) ndarray[source]

Sample nsteps samples from the posterior, evaluated at data x.

Parameters:
  • nsteps (int) – number of samples to draw

  • x (np.ndarray) – data to evaluate the posterior at

  • progress (bool, optional) – whether to show progress bar. Defaults to False.

  • skip_initial_state_check (bool, optional) – If True, a check that the initial_state can fully explore the space will be skipped. Defaults to False.

  • **kwargs – unexpected keyword arguments. Emcee sampling does not support additional keyword arguments and will raise an error if any are provided.

class utils.samplers.PyroSampler(posterior: sbi.inference.posteriors.base_posterior.NeuralPosterior, num_chains: int = -1, thin: int = 10, burn_in: int = 100, method='slice_np_vectorized')[source]

Sampler class for pyro’s samplers. Integrates with pyro through the sbi backend

Parameters:
  • posterior (Posterior) – posterior object to sample from, must have a .potential method specifiying the log posterior

  • num_chains (int, optional) – number of chains to sample from. Defaults to os.cpu_count()-1

  • thin (int, optional) – thinning factor for the chains. Defaults to 10

  • burn_in (int, optional) – number of steps to discard as burn-in. Defaults to 100

  • method (str, optional) – method to use for sampling. Defaults to ‘slice_np_vectorized’. See sbi documentation for more details.

sample(nsteps: int, x: ndarray, progress: bool = False, **kwargs) ndarray[source]

Sample nsteps samples from the posterior, evaluated at data x.

Parameters:
  • nsteps (int) – number of samples to draw

  • x (np.ndarray) – data to evaluate the posterior at

  • progress (bool, optional) – whether to show progress bar. Defaults to False.

  • **kwargs – additional keyword arguments to pass to the

  • method. (posterior's sample)

class utils.samplers.VISampler(posterior: sbi.inference.posteriors.base_posterior.NeuralPosterior, dist: str = 'maf', **train_kwargs)[source]

Sampler class for variational inference methods. See https://sbi-dev.github.io/sbi/reference/#sbi.inference.posteriors.vi_posterior.VIPosterior for more details.

Parameters:
  • posterior (Posterior) – posterior object to sample from, must have a .potential method specifiying the log posterior

  • dist (str, optional) – distribution to use for the variational inference. Defaults to ‘maf’.

  • train_kwargs (dict, optional) – keyword arguments to pass to the posterior’s train method. Defaults to {}.

sample(nsteps: int, x: ndarray, progress: bool = False, **kwargs) ndarray[source]

Sample nsteps samples from the posterior, evaluated at data x.

Parameters:
  • nsteps (int) – number of samples to draw

  • x (np.ndarray) – data to evaluate the posterior at

  • progress (bool, optional) – whether to show progress bar. Defaults to False.

  • **kwargs – additional keyword arguments to pass to the

  • method. (posterior's sample)

Import Utilities

Module with tools for importing classes from modules and initializing them

utils.import_utils.load_class(module_name: str, class_name: str) Any[source]

General tool to load any class from any module, without initialization.

Parameters:
  • module_name (str) – module from which to import class

  • class_name (str) – class name

Returns:

the class of choice

Return type:

class (Any)

utils.import_utils.load_from_config(config: Dict) Any[source]

General tool to load and initialize any class from any module with given configuration.

Parameters:

config (Dict) – dictionary with the configuration for the object of the form: {‘module’: Module name, ‘class’: Class name, ‘args’: Dictionary of initialization arguments}

Returns:

the object of choice

Return type:

object (Any)

utils.import_utils.update(config: Dict, **kwargs) Dict[source]

Recursively update a dictionary with another dictionary.

Parameters:
  • config (Dict) – dictionary to be updated

  • **kwargs – dictionary with updates