# 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 Boardcasting (e.g., high - low is a valid operation).

Note

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

>>> # 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]])
...
>>> u4 = Uniform(low=3.0, high=[5.0, 6.0, 7.0])

>>> # Complete example

>>> uniform = Uniform([0.], [2.])

>>> sample = uniform.sample([2])
>>> # a random tensor created by uniform distribution with shape: [2, 1]
>>> entropy = uniform.entropy()
>>> print(entropy)
[0.69314718])

>>> lp = uniform.log_prob(value_tensor)
>>> print(lp)
[-0.69314718])

>>> p = uniform.probs(value_tensor)
>>> print(p)
[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]

The KL-divergence between self distributions and other.

property mean

Mean of distribution

prob ( value )

Probability density/mass function evaluated at value.

Parameters

value (Tensor) – value which will be evaluated

rsample ( shape=() )

reparameterized sample

property variance

Variance of distribution

sample ( shape, seed=0 )

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 probability density/mass function.

Parameters

value (Tensor) – The input tensor.

Returns

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

probs ( value )

Probability density/mass function.

Parameters

value (Tensor) – The input tensor.

Returns

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

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.