latest (dev)
Copy
Latest development documentation · Updated 2026-10-15
tensorplay.functional.bernoulli
- tensorplay.functional.bernoulli(input: Tensor, *, generator: Generator | None, out: Tensor | None) Tensor[source]
Draws binary random numbers (0 or 1) from a Bernoulli distribution.
The
inputtensor should be a tensor containing probabilities to be used for drawing the binary random number. Hence, all values ininputhave to be in the range: .The element of the output tensor will draw a value according to the probability value given in
input.The returned
outtensor only has values 0 or 1 and is of the same shape asinput.outcan have integraldtype, butinputmust have floating pointdtype.- Parameters:
input (Tensor) – the input tensor of probability values for the Bernoulli distribution
- Keyword Arguments:
generator (
tensorplay.Generator, optional) – a pseudorandom number generator for samplingout (Tensor, optional) – the output tensor.
Example:
>>> a = tensorplay.empty(3, 3).uniform_(0, 1) # generate a uniform random matrix with range [0, 1] >>> a tensor([[ 0.1737, 0.0950, 0.3609], [ 0.7148, 0.0289, 0.2676], [ 0.9456, 0.8937, 0.7202]]) >>> tensorplay.bernoulli(a) tensor([[ 1., 0., 0.], [ 0., 0., 0.], [ 1., 1., 1.]]) >>> a = tensorplay.ones(3, 3) # probability of drawing "1" is 1 >>> tensorplay.bernoulli(a) tensor([[ 1., 1., 1.], [ 1., 1., 1.], [ 1., 1., 1.]]) >>> a = tensorplay.zeros(3, 3) # probability of drawing "1" is 0 >>> tensorplay.bernoulli(a) tensor([[ 0., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]])
Help improve this page
Found an error, an unclear step, or a missing example?

