bernoulli_

paddle. bernoulli_ ( x, p=0.5, name=None ) [source]

This is the inplace version of api bernoulli, which returns a Tensor filled with random values sampled from a bernoulli distribution. The output Tensor will be inplaced with input x. Please refer to bernoulli.

Parameters
  • x (Tensor) – The input tensor to be filled with random values.

  • p (float|Tensor, optional) – The success probability parameter of the output Tensor’s bernoulli distribution. If p is float, all elements of the output Tensor shared the same success probability. If p is a Tensor, it has per-element success probabilities, and the shape should be broadcastable to x. Default is 0.5

  • name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name.

Returns

A Tensor filled with random values sampled from the bernoulli distribution with success probability p .

Examples

>>> import paddle
>>> paddle.set_device('cpu')
>>> paddle.seed(200)
>>> x = paddle.randn([3, 4])
>>> x.bernoulli_()
>>> print(x)
Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0., 1., 0., 1.],
 [1., 1., 0., 1.],
 [0., 1., 0., 0.]])

>>> x = paddle.randn([3, 4])
>>> p = paddle.randn([3, 1])
>>> x.bernoulli_(p)
>>> print(x)
Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
[[1., 1., 1., 1.],
 [0., 0., 0., 0.],
 [0., 0., 0., 0.]])