Pytorch Torch Bernoulli
# PyTorch torch.bernoulli Function
* * PyTorch torch Reference Manual](#)
`torch.bernoulli` is a PyTorch function for generating 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
* `input` - Probability value or tensor containing probabilities (each element represents the probability of 1 at the corresponding position)
* `p` - Probability value (used when input is not a tensor)
* `size` - Shape of the output tensor
* `generator` - Random number generator (optional)
* `out` - Output tensor (optional)
* * *
## Usage Example
## Example
import torch
# Generate Bernoulli distribution random numbers using 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 random tensor using 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](#)
YouTip