5 Statistical Functions for Random Sampling in PyTorch
By: Shailesh Kumar
Tags: Machine Learning, Statistics, Pytorch

5 Statistical Functions for Random Sampling in PyTorch

Photo by CHUTTERSNAP on Unsplash

PyTorch is an open-source machine learning library for Python which provides maximum flexibility and speed on scientific computing for deep learning. It can be considered as a NumPy extension to GPUs.

PyTorch offers easy to use API and due to its pythonic environment it smoothly integrates with the Python data science stack.

In this article, we will explore 5 Statistical functions available in PyTorch for Random Sampling:

torch.bernoulli()

torch.normal()

torch.poisson()

torch.randn()

torch.randperm()

First, we will import PyTorch using : import torch

Function 1: torch.bernoulli()

It draws binary random numbers (0 or 1) from a Bernoulli Distribution and the Output is of the same shape as Input .

Example-1:

rand_m = torch.rand(4, 4) # generate a random matrix of shape 4x4

print(rand_m)

torch.bernoulli(rand_m) # draws a binary random number (0 or 1) Output:

tensor([[0.0643, 0.0690, 0.5069, 0.0323],

[0.3004, 0.6761, 0.1933, 0.7579],

[0.2958, 0.8812, 0.2917, 0.3713],

[0.8113, 0.0845, 0.9176, 0.9883]]) tensor([[0., 0., 1., 0.],

[1., 1., 0., 1.],

[0., 1., 0., 0.],

[1., 1., 1., 1.]])

Example-2:

# generate a uniform random matrix with range [0, 1]

uni_m = torch.empty(4, 4).uniform_(0, 1)

print(uni_m)

torch.bernoulli(uni_m) # draws a binary random number (0 or 1) Output:

tensor([[0.1723, 0.1165, 0.3703, 0.8497],

[0.5641, 0.0323, 0.1722, 0.3173],

[0.1663, 0.7910, 0.7019, 0.3336],

[0.8514, 0.7596, 0.9491, 0.4763]]) tensor([[0., 0., 1., 1.],

[1., 0., 0., 0.],

[0., 0., 1., 0.],

[1., 0., 1., 0.]])

Bernoulli Distribution

Note- Bernoulli Distribution is a random experiment that has only two outcomes (usually called a Success or a Failure) . It is best suited when we have two outcomes for a given event.

Function 2: torch.normal()

Returns a tensor of random numbers drawn from separate Normal Distributions whose mean and standard deviation are given.

The mean is a tensor with the mean of each output element’s normal distribution. The std is a tensor with the standard deviation of each output element’s normal distribution.

The shapes of mean and std don’t need to match, but the total number of elements in each tensor need to be the same.

Example-1:

torch.normal(mean=torch.arange(1.,11.), std=torch.arange(1,0,-0.1)) Output:

tensor([ 1.2426, 3.9152, 1.9166, 3.5866, 4.1520, 6.2800, 6.4655, 7.8572, 8.9195, 10.0185])

Example-2:

torch.normal(mean = torch.arange(1., 6.)) Output:

tensor([0.0205, 0.4621, 3.4993, 4.7508, 5.3769])

Note- The Normal Distribution is a probability function that describes how the values of a variable are distributed.

Function 3: torch.poisson()

It returns a tensor of the same size as input with each element sampled from a Poisson distribution with rate parameter given by the corresponding element in input .

Example-1:

rates = torch.rand(4, 4) * 4 # rate parameter between 0 and 4

print(rates)

torch.poisson(rates) Output:

tensor([[3.0679, 3.7367, 1.9038, 1.6546],

[1.8523, 1.0336, 3.4176, 3.6221],

[3.7246, 2.7234, 0.6564, 0.5517],

[2.0784, 3.7418, 1.3999, 2.3717]]) tensor([[2., 6., 1., 2.],

[2., 0., 3., 8.],

[4., 3., 0., 0.],

[1., 3., 3., 2.]])

Example-2:

rates = torch.rand(5, 4) * 7 # rate parameter between 0 and 7

print(rates)

torch.poisson(rates) Output:

tensor([[1.3724, 5.5118, 5.4870, 5.1002],

[0.9739, 2.7864, 2.1179, 3.6325],

[6.7643, 5.6531, 1.9739, 2.6923],

[2.5331, 6.6457, 4.4047, 2.9525],

[5.1334, 5.9861, 3.9731, 4.5331]]) tensor([[ 2., 5., 6., 4.],

[ 2., 2., 6., 5.],

[11., 3., 3., 4.],

[ 4., 9., 5., 4.],

[ 6., 6., 5., 4.]])

Note- The Poisson distribution shows how many times an event is likely to occur within a specified period of time.

Function 4: torch.randn()

It returns a tensor filled with random numbers from a normal distribution with mean 0 and variance 1 (also called the standard normal distribution ).

Example-1:

torch.randn(4) # 1-D input Output:

tensor([-0.7137, -0.5516, 0.4733, 0.4774])

Example-2:

torch.randn(4,4) # 2-D input Output:

tensor([[ 1.4962, -0.3302, -0.1390, 0.1099],

[ 0.7223, 0.8696, -2.1832, 0.6171],

[-1.0721, -1.7165, -0.3011, -0.5699],

[ 1.1563, 1.7144, -0.4076, -0.8162]])

Note- Standard Normal Distribution has a standard score or z-score is used to calculate the probability of a score occurring within our normal distribution and allows us to compare two scores that are from different normal distributions.

Function 5: torch.randperm()

It returns a random permutation of integers from 0 to n - 1 .

Example-1:

torch.randperm(5) Output:

tensor([2, 0, 3, 4, 1])

Example-2:

torch.randperm(9) Output:

tensor([6, 2, 7, 5, 1, 0, 3, 4, 8])

Note- Random Permutation is used when order of the elements matter.

Conclusion

In this article, we saw the working of 5 functions available in PyTorch. Hope this article helped you understand these function. There are a number of other useful functions. You can refer to the official documentation for the complete list of available Sampling functions.

References

Resources

The code snippets used in this article are available on my GitHub Page and jovian profile.