# Bernoulli¶

class paddle.distribution. Bernoulli ( probs, name=None ) [source]

Bernoulli distribution parameterized by probs, which is the probability of value 1.

In probability theory and statistics, the Bernoulli distribution, named after Swiss mathematician Jacob Bernoulli, is the discrete probability distribution of a random variable which takes the value 1 with probability p and the value 0 with probability q=1-p.

The probability mass function of this distribution, over possible outcomes k, is

$\begin{split}{\begin{cases} q=1-p & \text{if }value=0 \\ p & \text{if }value=1 \end{cases}}\end{split}$
Parameters
• probs (float|Tensor) – The probs input of Bernoulli distribution. The data type is float32 or float64. The range must be in [0, 1].

• name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.

Examples

>>> import paddle

>>> # init probs with a float
>>> rv = Bernoulli(probs=0.3)

>>> print(rv.mean)
0.30000001)

>>> print(rv.variance)
0.21000001)

>>> print(rv.entropy())
0.61086434)

probs ( value )

Probability density/mass function.

Note

This method will be deprecated in the future, please use prob instead.

property mean

Mean of Bernoulli distribution.

Returns

Mean value of distribution.

Return type

Tensor

property variance

Variance of Bernoulli distribution.

Returns

Variance value of distribution.

Return type

Tensor

sample ( shape )

Sample from Bernoulli distribution.

Parameters

shape (Sequence[int]) – Sample shape.

Returns

Sampled data with shape sample_shape + batch_shape + event_shape.

Return type

Tensor

Examples

>>> import paddle

>>> print(rv.sample([100]).shape)
[100, 1]

>>> print(rv.sample([100]).shape)
[100]

>>> print(rv.sample([100]).shape)
[100, 2]

>>> print(rv.sample([100, 2]).shape)
[100, 2, 2]

rsample ( shape, temperature=1.0 )

Sample from Bernoulli distribution (reparameterized).

The rsample is a continuously approximate of Bernoulli distribution reparameterized sample method. [1] Chris J. Maddison, Andriy Mnih, and Yee Whye Teh. The Concrete Distribution: A Continuous Relaxation of Discrete Random Variables. 2016. [2] Eric Jang, Shixiang Gu, and Ben Poole. Categorical Reparameterization with Gumbel-Softmax. 2016.

Note

rsample need to be followed by a sigmoid, which converts samples’ value to unit interval (0, 1).

Parameters
• shape (Sequence[int]) – Sample shape.

• temperature (float) – temperature for rsample, must be positive.

Returns

Sampled data with shape sample_shape + batch_shape + event_shape.

Return type

Tensor

Examples

>>> import paddle

>>> print(rv.sample([100]).shape)
[100, 1]

>>> rv = Bernoulli(0.3)
>>> print(rv.rsample([100]).shape)
[100]

>>> print(rv.rsample([100]).shape)
[100, 2]

>>> print(rv.rsample([100, 2]).shape)
[100, 2, 2]

>>> # rsample has to be followed by a sigmoid
>>> rv = Bernoulli(0.3)
>>> rsample = rv.rsample([3, ])
>>> print(rsample)
[-1.46112013, -0.01239836, -1.32765460])
>>> print(rsample_sigmoid)
[0.18829606, 0.49690047, 0.20954758])

>>> # The smaller the temperature, the distribution of rsample closer to sample, with probs of 0.3.
>>>
365.63122559)
>>>

320.15057373)

cdf ( value )

Cumulative distribution function(CDF) evaluated at value.

$\begin{split}{ \begin{cases} 0 & \text{if } value \lt 0 \\ 1 - p & \text{if } 0 \leq value \lt 1 \\ 1 & \text{if } value \geq 1 \end{cases} }\end{split}$
Parameters

value (Tensor) – Value to be evaluated.

Returns

CDF evaluated at value.

Return type

Tensor

Examples

>>> import paddle

>>> rv = Bernoulli(0.3)
[1.])

log_prob ( value )

Log of probability density function.

Parameters

value (Tensor) – Value to be evaluated.

Returns

Log of probability density evaluated at value.

Return type

Tensor

Examples

>>> import paddle

>>> rv = Bernoulli(0.3)
[-1.20397282])

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]

prob ( value )

Probability density function(PDF) evaluated at value.

$\begin{split}{ \begin{cases} q=1-p & \text{if }value=0 \\ p & \text{if }value=1 \end{cases} }\end{split}$
Parameters

value (Tensor) – Value to be evaluated.

Returns

PDF evaluated at value.

Return type

Tensor

Examples

>>> import paddle

>>> rv = Bernoulli(0.3)
[0.29999998])

entropy ( )

Entropy of Bernoulli distribution.

${ entropy = -(q \log q + p \log p) }$
Returns

Entropy of distribution.

Return type

Tensor

Examples

>>> import paddle

>>> rv = Bernoulli(0.3)
>>> print(rv.entropy())
0.61086434)

kl_divergence ( other ) [source]

The KL-divergence between two Bernoulli distributions.

${ KL(a || b) = p_a \log(p_a / p_b) + (1 - p_a) \log((1 - p_a) / (1 - p_b)) }$
Parameters

other (Bernoulli) – instance of Bernoulli.

Returns

kl-divergence between two Bernoulli distributions.

Return type

Tensor

Examples

>>> import paddle