MultivariateNormalDiag

class paddle.fluid.layers.MultivariateNormalDiag(loc, scale)[source]

A multivariate normal (also called Gaussian) distribution parameterized by a mean vector and a covariance matrix.

The probability density function (pdf) is:

\[pdf(x; loc, scale) = \frac{e^{-\frac{||y||^2}{2}}}{Z}\]

where: .. math:

y = inv(scale) @ (x - loc)
Z = (2\pi)^{0.5k} |det(scale)|

In the above equation:

  • \(inv\) : denotes to take the inverse of the matrix.

  • \(@\) : denotes matrix multiplication.

  • \(det\) : denotes to evaluate the determinant.

Parameters
  • loc (list|numpy.ndarray|Variable) – The mean of multivariateNormal distribution with shape \([k]\) . The data type is float32.

  • scale (list|numpy.ndarray|Variable) – The positive definite diagonal covariance matrix of multivariateNormal distribution with shape \([k, k]\) . All elements are 0 except diagonal elements. The data type is float32.

Examples

import numpy as np
from paddle.fluid import layers
from paddle.fluid.layers import MultivariateNormalDiag

a_loc_npdata = np.array([0.3,0.5],dtype="float32")
a_loc_tensor = layers.create_tensor(dtype="float32")
layers.assign(a_loc_npdata, a_loc_tensor)


a_scale_npdata = np.array([[0.4,0],[0,0.5]],dtype="float32")
a_scale_tensor = layers.create_tensor(dtype="float32")
layers.assign(a_scale_npdata, a_scale_tensor)

b_loc_npdata = np.array([0.2,0.4],dtype="float32")
b_loc_tensor = layers.create_tensor(dtype="float32")
layers.assign(b_loc_npdata, b_loc_tensor)

b_scale_npdata = np.array([[0.3,0],[0,0.4]],dtype="float32")
b_scale_tensor = layers.create_tensor(dtype="float32")
layers.assign(b_scale_npdata, b_scale_tensor)

a = MultivariateNormalDiag(a_loc_tensor, a_scale_tensor)
b = MultivariateNormalDiag(b_loc_tensor, b_scale_tensor)

a.entropy()
# [2.033158] with shape: [1]
b.entropy()
# [1.7777451] with shape: [1]

a.kl_divergence(b)
# [0.06542051] with shape: [1]
log_prob(value)

Log probability density/mass function.

sample()

Sampling from the distribution.

entropy()

Shannon entropy in nats.

Returns

Shannon entropy of Multivariate Normal distribution. The data type is float32.

Return type

Variable

kl_divergence(other)

The KL-divergence between two Multivariate Normal distributions.

Parameters

other (MultivariateNormalDiag) – instance of Multivariate Normal.

Returns

kl-divergence between two Multivariate Normal distributions. The data type is float32.

Return type

Variable