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

\[pdf(x; a, b) = \frac{1}{Z}, \ a <=x <b\]
\[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 (e.g., high - low is a valid operation).

Note

If you want know more about broadcasting, please refer to Introduction to Tensor .

Parameters
  • low (int|float|list|tuple|numpy.ndarray|Tensor) – The lower boundary of uniform distribution.The data type is float32 and float64.

  • high (int|float|list|tuple|numpy.ndarray|Tensor) – The higher boundary of uniform distribution.The data type is float32 and float64.

  • name (str, optional) – For details, please refer to Name. Generally, no setting is required. Default: None.

Examples

>>> import paddle
>>> from paddle.distribution import Uniform
>>> paddle.seed(2023)

>>> # 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()
>>> print(entropy)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
    [0.69314718])

>>> lp = uniform.log_prob(value_tensor)
>>> print(lp)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
    [-0.69314718])

>>> p = uniform.probs(value_tensor)
>>> print(p)
Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
    [0.50000000])
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]

kl_divergence ( other ) [source]

kl_divergence

The KL-divergence between self distributions and other.

property mean

Mean of distribution

prob ( value )

prob

Probability density/mass function evaluated at value.

Parameters

value (Tensor) – value which will be evaluated

rsample ( shape=() )

rsample

reparameterized sample

property variance

Variance of distribution

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

Tensor, A tensor with prepended dimensions shape. The data type is float32.

log_prob ( value )

log_prob

Log probability density/mass function.

Parameters

value (Tensor) – The input tensor.

Returns

Tensor, log probability.The data type is same with value.

probs ( value )

probs

Probability density/mass function.

Parameters

value (Tensor) – The input tensor.

Returns

Tensor, probability. The data type is same with value.

entropy ( )

entropy

Shannon entropy in nats.

The entropy is

\[\begin{split}entropy(low, high) = \\log (high - low)\end{split}\]
Returns

Tensor, Shannon entropy of uniform distribution.The data type is float32.