nifty7.library.variational_models module#

class DiagonalSelector(domain)[source]#

Bases: LinearOperator

Extract the diagonal of a two-dimensional field.

Parameters:

domain (Domain, DomainTuple, list of Domain) – The two-dimensional domain of the input field. Must be of shape NxN.

apply(x, mode)[source]#

Applies the Operator to a given x, in a specified mode.

Parameters:
  • x (Field) – The input Field, defined on the Operator’s domain or target, depending on mode.

  • mode (int) –

    • TIMES: normal application

    • ADJOINT_TIMES: adjoint application

    • INVERSE_TIMES: inverse application

    • ADJOINT_INVERSE_TIMES or INVERSE_ADJOINT_TIMES: adjoint inverse application

Returns:

The processed Field defined on the Operator’s target or domain, depending on mode.

Return type:

Field

class FullCovarianceVI(position, hamiltonian, n_samples, mirror_samples, initial_sig=1, comm=None, nanisinf=False)[source]#

Bases: object

Collect the operators required for Gaussian full-covariance variational

Gaussian meanfield variational inference approximates some target distribution with a Gaussian distribution with a diagonal covariance matrix. The parameters of the approximation, in this case the mean and a lower triangular matrix corresponding to a Cholesky decomposition of the covariance, are obtained by minimizing a stochastic estimate of the Kullback-Leibler divergence between the target and the approximation. In order to obtain gradients w.r.t the parameters, the reparametrization trick is employed, which separates the stochastic part of the approximation from a deterministic function, the generator. Samples from the approximation are drawn by processing samples from a standard Gaussian through this generator.

Note that the size of the covariance scales quadratically with the number of model parameters.

Parameters:
  • position (Field) – The initial estimate of the approximate mean parameter.

  • hamiltonian (Energy) – Hamiltonian of the approximated probability distribution.

  • n_samples (int) – Number of samples used to stochastically estimate the KL.

  • mirror_samples (bool) – Whether the negative of the drawn samples are also used, as they are equally legitimate samples. If true, the number of used samples doubles. Mirroring samples stabilizes the KL estimate as extreme sample variation is counterbalanced. Since it improves stability in many cases, it is recommended to set mirror_samples to True.

  • initial_sig (positive float) – The initial estimate for the standard deviation. Initially no correlation between the parameters is assumed.

  • comm (MPI communicator or None) – If not None, samples will be distributed as evenly as possible across this communicator. If mirror_samples is set, then a sample and its mirror image will always reside on the same task.

  • nanisinf (bool) – If true, nan energies which can happen due to overflows in the forward model are interpreted as inf. Thereby, the code does not crash on these occasions but rather the minimizer is told that the position it has tried is not sensible.

property KL#
draw_sample()[source]#
property entropy#
property mean#
minimize(minimizer)[source]#
class GaussianEntropy(domain)[source]#

Bases: EnergyOperator

Entropy of a Gaussian distribution given the diagonal of a triangular decomposition of the covariance.

As metric a SandwichOperator of the Jacobian is used. This is not a proper Fisher metric but may be useful for second order minimization.

Parameters:

domain (Domain, DomainTuple, list of Domain) – The domain of the diagonal.

apply(x)[source]#

Applies the operator to a Field or MultiField.

Parameters:

x (Field or MultiField) – Input on which the operator shall act. Needs to be defined on domain.

class LowerTriangularInserter(target)[source]#

Bases: LinearOperator

Insert the entries of a lower triangular matrix into a matrix.

Parameters:

target (Domain, DomainTuple, list of Domain) – A two-dimensional domain with NxN entries.

apply(x, mode)[source]#

Applies the Operator to a given x, in a specified mode.

Parameters:
  • x (Field) – The input Field, defined on the Operator’s domain or target, depending on mode.

  • mode (int) –

    • TIMES: normal application

    • ADJOINT_TIMES: adjoint application

    • INVERSE_TIMES: inverse application

    • ADJOINT_INVERSE_TIMES or INVERSE_ADJOINT_TIMES: adjoint inverse application

Returns:

The processed Field defined on the Operator’s target or domain, depending on mode.

Return type:

Field

class MeanFieldVI(position, hamiltonian, n_samples, mirror_samples, initial_sig=1, comm=None, nanisinf=False)[source]#

Bases: object

Collect the operators required for Gaussian meanfield variational inference.

Gaussian meanfield variational inference approximates some target distribution with a Gaussian distribution with a diagonal covariance matrix. The parameters of the approximation, in this case the mean and standard deviation, are obtained by minimizing a stochastic estimate of the Kullback-Leibler divergence between the target and the approximation. In order to obtain gradients w.r.t the parameters, the reparametrization trick is employed, which separates the stochastic part of the approximation from a deterministic function, the generator. Samples from the approximation are drawn by processing samples from a standard Gaussian through this generator.

Parameters:
  • position (Field) – The initial estimate of the approximate mean parameter.

  • hamiltonian (Energy) – Hamiltonian of the approximated probability distribution.

  • n_samples (int) – Number of samples used to stochastically estimate the KL.

  • mirror_samples (bool) – Whether the negative of the drawn samples are also used, as they are equally legitimate samples. If true, the number of used samples doubles. Mirroring samples stabilizes the KL estimate as extreme sample variation is counterbalanced. Since it improves stability in many cases, it is recommended to set mirror_samples to True.

  • initial_sig (positive Field or positive float) – The initial estimate of the standard deviation.

  • comm (MPI communicator or None) – If not None, samples will be distributed as evenly as possible across this communicator. If mirror_samples is set, then a sample and its mirror image will always reside on the same task.

  • nanisinf (bool) – If true, nan energies which can happen due to overflows in the forward model are interpreted as inf. Thereby, the code does not crash on these occasions but rather the minimizer is told that the position it has tried is not sensible.

property KL#
draw_sample()[source]#
property entropy#
property mean#
minimize(minimizer)[source]#
property std#