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 probabilityq=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 >>> from paddle.distribution import Bernoulli >>> # init `probs` with a float >>> rv = Bernoulli(probs=0.3) >>> print(rv.mean) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 0.30000001) >>> print(rv.variance) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 0.21000001) >>> print(rv.entropy()) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 0.61086434)
-
probs
(
value
)
probs¶
-
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¶
-
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 >>> from paddle.distribution import Bernoulli >>> rv = Bernoulli(paddle.full((1), 0.3)) >>> print(rv.sample([100]).shape) [100, 1] >>> rv = Bernoulli(paddle.to_tensor(0.3)) >>> print(rv.sample([100]).shape) [100] >>> rv = Bernoulli(paddle.to_tensor([0.3, 0.5])) >>> print(rv.sample([100]).shape) [100, 2] >>> rv = Bernoulli(paddle.to_tensor([0.3, 0.5])) >>> print(rv.sample([100, 2]).shape) [100, 2, 2]
-
rsample
(
shape,
temperature=1.0
)
rsample¶
-
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 >>> paddle.seed(1) >>> from paddle.distribution import Bernoulli >>> rv = Bernoulli(paddle.full((1), 0.3)) >>> print(rv.sample([100]).shape) [100, 1] >>> rv = Bernoulli(0.3) >>> print(rv.rsample([100]).shape) [100] >>> rv = Bernoulli(paddle.to_tensor([0.3, 0.5])) >>> print(rv.rsample([100]).shape) [100, 2] >>> rv = Bernoulli(paddle.to_tensor([0.3, 0.5])) >>> 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, ]) >>> rsample_sigmoid = paddle.nn.functional.sigmoid(rsample) >>> print(rsample) Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, [-1.46112013, -0.01239836, -1.32765460]) >>> print(rsample_sigmoid) Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, [0.18829606, 0.49690047, 0.20954758]) >>> # The smaller the `temperature`, the distribution of `rsample` closer to `sample`, with `probs` of 0.3. >>> print(paddle.nn.functional.sigmoid(rv.rsample([1000, ], temperature=1.0)).sum()) >>> Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 365.63122559) >>> >>> print(paddle.nn.functional.sigmoid(rv.rsample([1000, ], temperature=0.1)).sum()) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 320.15057373)
-
cdf
(
value
)
cdf¶
-
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 >>> from paddle.distribution import Bernoulli >>> rv = Bernoulli(0.3) >>> print(rv.cdf(paddle.to_tensor([1.0]))) Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, [1.])
-
log_prob
(
value
)
log_prob¶
-
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 >>> from paddle.distribution import Bernoulli >>> rv = Bernoulli(0.3) >>> print(rv.log_prob(paddle.to_tensor([1.0]))) Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, [-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
)
prob¶
-
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 >>> from paddle.distribution import Bernoulli >>> rv = Bernoulli(0.3) >>> print(rv.prob(paddle.to_tensor([1.0]))) Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True, [0.29999998])
-
entropy
(
)
entropy¶
-
Entropy of Bernoulli distribution.
\[{ entropy = -(q \log q + p \log p) }\]- Returns
-
Entropy of distribution.
- Return type
-
Tensor
Examples
>>> import paddle >>> from paddle.distribution import Bernoulli >>> rv = Bernoulli(0.3) >>> print(rv.entropy()) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 0.61086434)
-
kl_divergence
(
other
)
[source]
kl_divergence¶
-
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 >>> from paddle.distribution import Bernoulli >>> rv = Bernoulli(0.3) >>> rv_other = Bernoulli(0.7) >>> print(rv.kl_divergence(rv_other)) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 0.33891910)