nifty8.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.

__init__(domain)[source]#
apply(x, mode)[source]#

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

Parameters:
  • x (nifty8.field.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:

nifty8.field.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 (nifty8.field.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#
__init__(position, hamiltonian, n_samples, mirror_samples, initial_sig=1, comm=None, nanisinf=False)[source]#
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.

__init__(domain)[source]#
apply(x)[source]#

Applies the operator to a Field, MultiField or Linearization.

Parameters:

x (nifty8.field.Field, nifty8.multi_field.MultiField,) – or nifty8.linearization.Linearization Input on which the operator shall act. Needs to be defined on domain. If x`is a :class:`nifty8.linearization.Linearization, apply returns a new nifty8.linearization.Linearization contining the result of the operator application as well as its Jacobian, evaluated at x.

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.

__init__(target)[source]#
apply(x, mode)[source]#

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

Parameters:
  • x (nifty8.field.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:

nifty8.field.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 (nifty8.field.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 nifty8.field.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#
__init__(position, hamiltonian, n_samples, mirror_samples, initial_sig=1, comm=None, nanisinf=False)[source]#
draw_sample()[source]#
property entropy#
property mean#
minimize(minimizer)[source]#
property std#