Uniform¶
- class paddle.distribution. Uniform ( low, high, name=None ) [source]
-
Uniform distribution with low and high parameters.
Mathematical Details
The probability density function (pdf) is
\[\begin{split}pdf(x; a, b) = \\frac{1}{Z}, \ a <=x <b\end{split}\]\[Z = b - a\]In the above equation:
\(low = a\),
\(high = b\),
\(Z\): is the normalizing constant.
The parameters low and high must be shaped in a way that supports [broadcasting](https://www.paddlepaddle.org.cn/documentation/docs/en/develop/beginners_guide/basic_concept/broadcasting_en.html) (e.g., high - low is a valid operation).
- Parameters
-
low (int|float|list|tuple|numpy.ndarray|Tensor) – The lower boundary of uniform distribution.The data type is int, float, list, numpy.ndarray or Tensor
high (int|float|list|tuple|numpy.ndarray|Tensor) – The higher boundary of uniform distribution.The data type is int, float, list, numpy.ndarray or Tensor
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 Uniform # Without broadcasting, a single uniform distribution [3, 4]: u1 = Uniform(low=3.0, high=4.0) # 2 distributions [1, 3], [2, 4] u2 = Uniform(low=[1.0, 2.0], high=[3.0, 4.0]) # 4 distributions u3 = Uniform(low=[[1.0, 2.0], [3.0, 4.0]], high=[[1.5, 2.5], [3.5, 4.5]]) # With broadcasting: u4 = Uniform(low=3.0, high=[5.0, 6.0, 7.0]) # Complete example value_tensor = paddle.to_tensor([0.8], dtype="float32") uniform = Uniform([0.], [2.]) sample = uniform.sample([2]) # a random tensor created by uniform distribution with shape: [2, 1] entropy = uniform.entropy() # [0.6931472] with shape: [1] lp = uniform.log_prob(value_tensor) # [-0.6931472] with shape: [1] p = uniform.probs(value_tensor) # [0.5] with shape: [1]
-
sample
(
shape,
seed=0
)
sample¶
-
Generate samples of the specified shape.
- Parameters
-
shape (list) – 1D int32. Shape of the generated samples.
seed (int) – Python integer number.
- Returns
-
A tensor with prepended dimensions shape.The data type is float32.
- Return type
-
Tensor
-
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
-
probability.The data type is same with value.
- Return type
-
Tensor
-
entropy
(
)
entropy¶
-
Shannon entropy in nats.
The entropy is
\[\begin{split}entropy(low, high) = \\log (high - low)\end{split}\]- Returns
-
Shannon entropy of uniform distribution.The data type is float32.
- Return type
-
Tensor
-
kl_divergence
(
other
)
kl_divergence¶
-
The KL-divergence between self distributions and other.