Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.bernoulli function

PyTorch torch.bernoulli function


PyTorch torch reference manual PyTorch torch reference manual

torch.bernoulli is a function in PyTorch used to generate random numbers from a Bernoulli distribution.

Function definition

torch.bernoulli(input, *, generator=None, out=None)

torch.bernoulli(p, size, *, generator=None, out=None)

Parameter Description


Usage examples

Example

import

torch

Generate Bernoulli-distributed random numbers using a probability tensor

probs

=

torch.

tensor

(

[

0.1

,

0.5

,

0.9

]

)

result

=

torch.

bernoulli

(

probs

)

print

(

"Probability tensor:"

,

probs

)

print

(

"Bernoulli sampling result:"

,

result

)

Generate a random tensor using a fixed probability

result2

=

torch.

bernoulli

(

0.5

,

(

3

,

3

)

)

print

(

"3x3 random tensor generated with fixed probability 0.5:"

)

print

(

result2

)


PyTorch torch reference manual PyTorch torch reference manual