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.]])

Updated on: 27-Jan-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements