bernoulli

paddle. bernoulli ( x, name=None ) [source]

For each element \(x_i\) in input x, take a sample from the Bernoulli distribution, also called two-point distribution, with success probability \(x_i\). The Bernoulli distribution with success probability \(x_i\) is a discrete probability distribution with probability mass function

\[\begin{split}p(y)=\begin{cases} x_i,&y=1\\ 1-x_i,&y=0 \end{cases}.\end{split}\]
Parameters
  • x (Tensor) – The input Tensor, it’s data type should be float32, float64.

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

Returns

A Tensor filled samples from Bernoulli distribution, whose shape and dtype are same as x.

Return type

Tensor

Examples

>>> import paddle

>>> paddle.set_device('cpu')  # on CPU device
>>> paddle.seed(100)

>>> x = paddle.rand([2,3])
>>> print(x)
>>> 
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.55355281, 0.20714243, 0.01162981],
 [0.51577556, 0.36369765, 0.26091650]])
>>> 

>>> out = paddle.bernoulli(x)
>>> print(out)
>>> 
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[1., 0., 1.],
 [0., 1., 0.]])
>>>