Normal¶
- class paddle.distribution. Normal ( loc, scale, name=None ) [source]
-
The Normal distribution with location loc and scale parameters.
Mathematical details
The probability density function (pdf) is
\[pdf(x; \mu, \sigma) = \frac{1}{Z}e^{\frac {-0.5 (x - \mu)^2} {\sigma^2} }\]\[Z = (2 \pi \sigma^2)^{0.5}\]In the above equation:
\(loc = \mu\): is the mean.
\(scale = \sigma\): is the std.
\(Z\): is the normalization constant.
- Parameters
-
loc (int|float|list|tuple|numpy.ndarray|Tensor) – The mean of normal distribution.The data type is float32 and float64.
scale (int|float|list|tuple|numpy.ndarray|Tensor) – The std of normal distribution.The data type is float32 and float64.
name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.
Examples
>>> import paddle >>> from paddle.distribution import Normal >>> # Define a single scalar Normal distribution. >>> dist = Normal(loc=0., scale=3.) >>> # Define a batch of two scalar valued Normals. >>> # The first has mean 1 and standard deviation 11, the second 2 and 22. >>> dist = Normal(loc=[1., 2.], scale=[11., 22.]) >>> # Get 3 samples, returning a 3 x 2 tensor. >>> dist.sample([3]) >>> # Define a batch of two scalar valued Normals. >>> # Both have mean 1, but different standard deviations. >>> dist = Normal(loc=1., scale=[11., 22.]) >>> # Complete example >>> value_tensor = paddle.to_tensor([0.8], dtype="float32") >>> normal_a = Normal([0.], [1.]) >>> normal_b = Normal([0.5], [2.]) >>> sample = normal_a.sample([2]) >>> # a random tensor created by normal distribution with shape: [2, 1] >>> entropy = normal_a.entropy() >>> print(entropy) Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, [1.41893852]) >>> lp = normal_a.log_prob(value_tensor) >>> print(lp) Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, [-1.23893857]) >>> p = normal_a.probs(value_tensor) >>> print(p) Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, [0.28969154]) >>> kl = normal_a.kl_divergence(normal_b) >>> print(kl) Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, [0.34939718])
- property batch_shape
-
Returns batch shape of distribution
- Returns
-
batch shape
- Return type
-
Sequence[int]
- property event_shape
-
Returns event shape of distribution
- Returns
-
event shape
- Return type
-
Sequence[int]
- property mean
-
Mean of normal distribution.
- Returns
-
mean value.
- Return type
-
Tensor
-
prob
(
value
)
prob¶
-
Probability density/mass function evaluated at value.
- Parameters
-
value (Tensor) – value which will be evaluated
- property variance
-
Variance of normal distribution.
- Returns
-
variance value.
- Return type
-
Tensor
-
sample
(
shape=(),
seed=0
)
sample¶
-
Generate samples of the specified shape.
- Parameters
-
shape (Sequence[int], optional) – Shape of the generated samples.
seed (int) – Python integer number.
- Returns
-
Tensor, A tensor with prepended dimensions shape.The data type is float32.
-
rsample
(
shape=()
)
rsample¶
-
Generate reparameterized samples of the specified shape.
- Parameters
-
shape (Sequence[int], optional) – Shape of the generated samples.
- Returns
-
A tensor with prepended dimensions shape.The data type is float32.
- Return type
-
Tensor
-
entropy
(
)
entropy¶
-
Shannon entropy in nats.
The entropy is
\[entropy(\sigma) = 0.5 \log (2 \pi e \sigma^2)\]In the above equation:
\(scale = \sigma\): is the std.
- Returns
-
Tensor, Shannon entropy of normal distribution.The data type is float32.
-
log_prob
(
value
)
log_prob¶
-
Log probability density/mass function.
- Parameters
-
value (Tensor) – The input tensor.
- Returns
-
log probability.The data type is same with
value
. - Return type
-
Tensor
-
probs
(
value
)
probs¶
-
Probability density/mass function.
- Parameters
-
value (Tensor) – The input tensor.
- Returns
-
Tensor, probability. The data type is same with
value
.
-
kl_divergence
(
other
)
[source]
kl_divergence¶
-
The KL-divergence between two normal distributions.
The probability density function (pdf) is
\[KL\_divergence(\mu_0, \sigma_0; \mu_1, \sigma_1) = 0.5 (ratio^2 + (\frac{diff}{\sigma_1})^2 - 1 - 2 \ln {ratio})\]\[ratio = \frac{\sigma_0}{\sigma_1}\]\[diff = \mu_1 - \mu_0\]In the above equation:
\(loc = \mu_0\): is the mean of current Normal distribution.
\(scale = \sigma_0\): is the std of current Normal distribution.
\(loc = \mu_1\): is the mean of other Normal distribution.
\(scale = \sigma_1\): is the std of other Normal distribution.
\(ratio\): is the ratio of scales.
\(diff\): is the difference between means.
- Parameters
-
other (Normal) – instance of Normal.
- Returns
-
Tensor, kl-divergence between two normal distributions.The data type is float32.