

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to draw binary random numbers (0 or 1) from a Bernoulli distribution in PyTorch?
To draw binary random numbers (0 or 1) from a Bernoulli distribution, we apply the torch.bernoulli() method. The input to this method is a torch tensor containing the probabilities of drawing 1. These probabilities are used to draw binary random numbers (0 or 1).
As the input tensor contains the probabilities, all the elements should be in the range [0,1]. It returns a tensor whose elements (0 or 1) are randomly selected from a Bernoulli distribution with the input probabilities.
Syntax
torch.bernoulli(input)
where, the parameter input is a torch tensor containing the probabilities of drawing 1. These probabilities are used to draw the elements from a Bernoilli distribution.
Steps
We could use the following steps to draw the binary random numbers (0 or 1) from a Bernoulli distribution −
Import the required library. In all the following examples, the required Python library is torch. Make sure you have already installed it.
import torch
Define a torch tensor input of probabilities. We define the input elements between 0 and 1.
input = torch.randn(5,5).uniform_(0,1)
Compute torch.bernoulli(input) to draw binary random numbers (0 or 1) from a Bernoulli distribution.
random_numbers = torch.bernoulli(input)
Print the computed random number tensor.
print(random_numbers)
Example 1
import torch # define tensor containing the probabilities to be # used for drawing the binary random number. t = torch.randn(5,5).uniform_(0,1) print(t) # generate random numbers (0,1) from Bernoulli # distribution using above probabilities random_numbers = torch.bernoulli(t) print(random_numbers)
Output
tensor([[0.0080, 0.2254, 0.2143, 0.8664, 0.9297], [0.5881, 0.7574, 0.9916, 0.6557, 0.7281], [0.0656, 0.9466, 0.6378, 0.1693, 0.3333], [0.9914, 0.9468, 0.6381, 0.6448, 0.4003], [0.4401, 0.1261, 0.0787, 0.9409, 0.2434]]) tensor([[0., 0., 0., 0., 1.], [0., 1., 1., 1., 1.], [0., 1., 1., 1., 0.], [1., 1., 0., 1., 0.], [0., 0., 0., 1., 0.]])
Notice that we use .uniform_(0,1) to generate the input tensor of probabilities. It generates the numbers in the range [0,1]
Example 2
# Import the required library import torch # define tensor containing the probabilities to be # used for drawing the binary random number. t = torch.randn(2,4,4).uniform_(0,1) print(t) # generate random numbers (0,1) from Bernoulli # distribution using above probabilities random_numbers = torch.bernoulli(t) print(random_numbers)
Output
tensor([[[0.5937, 0.5897, 0.9741, 0.2749], [0.5659, 0.9343, 0.7971, 0.4183], [0.4684, 0.4700, 0.0858, 0.1492], [0.9859, 0.4440, 0.0871, 0.4186]], [[0.1498, 0.5788, 0.7917, 0.4689], [0.2375, 0.7465, 0.0773, 0.3620], [0.6607, 0.0263, 0.4370, 0.9952], [0.0920, 0.5408, 0.7088, 0.2246]]]) tensor([[[1., 1., 1., 0.], [1., 1., 1., 0.], [0., 1., 0., 0.], [1., 0., 0., 1.]], [[0., 1., 1., 0.], [0., 0., 0., 0.], [1., 0., 1., 1.], [0., 0., 0., 0.]]])
Example 3
import torch a = torch.ones(3, 3) # probability of drawing "1" is 1 print(a) print(torch.bernoulli(a)) b = torch.zeros(3, 3) # probability of drawing "1" is 0 print(b) print(torch.bernoulli(b))
Output
tensor([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]]) tensor([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]]) tensor([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]) tensor([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]])
- Related Questions & Answers
- Bernoulli Distribution in Data Structures
- How to generate Bernoulli random variable in R?
- How to rescale a tensor in the range [0, 1] and sum to 1 in PyTorch?
- Generate random numbers following a normal distribution in C/C++
- C++ Program to Generate Random Numbers Using Probability Distribution Function
- How to get a pseudo-random number between 0 and 1 in JavaScript?
- How to create a random sample with values 0 and 1 in R?
- How to create a tensor whose elements are sampled from a Poisson distribution in PyTorch?
- How to create a random sample of values between 0 and 1 in R?
- Java Program to generate custom random number -1 or 1
- An Interesting Method to Generate Binary Numbers from 1 to n?
- Design a DFA machine accepting odd numbers of 0’s or even numbers of 1’s
- Draw a Turing machine to find 1’s complement of a binary number
- How to convert binary variable to 0/1 format in an R data frame?
- How to draw bounding boxes on an image in PyTorch?