Open In App

5 Statistical Functions for Random Sampling in PyTorch

Last Updated : 28 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

PyTorch is an open source machine learning library used for deep learning with more flexibility and feasibility. This is an extension of NumPy.

For Statistical Functions for Random Sampling, let’s see what they are along with their easy implementations. To run all these the first step is to import Pytorch by import torch. There are 5 functions:

  • torch.bernoulli()
  • torch.normal()
  • torch.poisson()
  • torch.randn()
  • torch.randperm()

1) torch.bernoulli() function:

This function simply makes all the inputs into binary random numbers(0 or 1) from a Bernoulli Distribution. The output shape is same as the data inputted in the code.

Syntax-

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

Parameters-

input (Tensor) – the input tensor of probability values for the Bernoulli distribution

Key Argument-

  • generator (torch.Generator, optional) – a pseudorandom number generator for sampling
  • out (Tensor, optional) – the output tensor.

Example:

In this example, let’s see a basic simple random generated shape and output it in Bernoulli distribution.

Python3




import torch
 
# this function will make a
# tensor of 5X5 array with
# random numbers
rand_matrix = torch.rand(5, 5)
 
print(rand_matrix)
 
# Bernoulli distribution
# this function will do the bernoulli
# distribution on the given matrix and
# form the new tensor
torch.bernoulli(rand_matrix)


Output:

tensor([[0.5010, 0.0622, 0.3710, 0.3325, 0.5136],

        [0.0790, 0.6433, 0.8819, 0.3770, 0.8236],

        [0.3458, 0.9933, 0.2282, 0.6544, 0.6823],

        [0.5454, 0.5916, 0.2471, 0.6174, 0.1676],

        [0.8980, 0.4162, 0.8114, 0.3744, 0.9957]])

tensor([[0., 0., 0., 0., 0.],

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

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

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

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

2) torch.normal() function :

This function works on the Normal Distribution theory. The function returns a tensor of random numbers in which the mean and the standard deviation is given. In this there are 2 parameters – a) mean – is a tensor with the mean of each output element’s normal distribution. b) std- tensor with a standard deviation

Note: The shape of mean and std need not be the same, but the total number of elements in the tensor is the same.

Syntax-

torch.normal(mean, std, *, generator=None, out=None) → Tensor

Parameters-

  • mean (Tensor) – the tensor of per-element means
  • std (Tensor) – the tensor of per-element standard deviations

Key Argument-

  • generator (torch.Generator, optional) – a pseudorandom number generator for sampling
  • out (Tensor, optional) – the output tensor.

Example:

In this example, we will be generating random numbers with the provided mean and standard deviation in torch.normal() function.

Python3




# this function have 2 parameters,
# and will form the tensor of
# normal distribution
# normal(mean,std)
# mean, giving a range of mean
# std,dive the standard in the given range
torch.normal(mean=torch.arange(12., 22.),
             std=torch.arange(1, 0, -0.1))


Output:

tensor([12.2238, 12.8651, 15.5746, 14.7285, 16.3280, 17.4913, 17.8418, 19.5997,

       19.8890, 21.0208])

3) torch.poisson() function :

The output of this function is of the same size as the input with each element got from Poisson Distribution. This distribution shows how many times an event is likely to occur in the given time period.

Syntax:

torch.poisson(input, generator=None) → Tensor

Parameters:

input (Tensor) – the input tensor containing the rates of the Poisson distribution

Key Arguments:

generator (torch.Generator, optional) – a pseudorandom number generator for sampling

Example:

In this example, we will be generating the random tensor matrix of 4×4 using the torch.poisson() function in python.

Python3




# generating the random tensor
# matrix of 4X4
rates_torch = torch.rand(4, 4) * 10
print(rates_torch)
 
# this function will do the poisson
# distribution of the given tensor
# and will give new tensor
# poisson(param),this param is the
# tensor matrix of 4X4
torch.poisson(rates_torch)


Output:

tensor([[7.5055, 6.9471, 7.9227, 2.2798],
       [5.0238, 9.1469, 5.4483, 0.1173],
       [1.3271, 0.0355, 5.4621, 1.8165],
       [5.1992, 2.9028, 0.2533, 3.8208]])
tensor([[ 8., 11., 10.,  5.],
       [ 6., 12.,  5.,  0.],
       [ 2.,  0.,  5.,  0.],
       [ 4.,  2.,  0.,  2.]])

4) torch.randn() function:

This function returns a tensor with random numbers from a normal distribution with mean 0 and variance 1(Standard normal distribution).

Syntax:

torch.randn(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

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

Parameters:

size (int…) – a sequence of integers defining the shape of the output tensor. Can be a variable number of arguments or a collection like a list or tuple.

Key Arguments:

  • generator (torch.Generator, optional) – a pseudorandom number generator for sampling
  • out (Tensor, optional) – the output tensor.
  • dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, uses a global default (see torch.set_default_tensor_type()).
  • layout (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.
  • device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

Example:

In this example, we will be using the torch.randn() function to create the 4×4 matrix by passing 4 and 4 to the function in python.

Python3




# randn function has two parameters
# for making 2X2 matrix,
# it can have 1 parameter also for
# 1-D matrix the below function
# will five a tensor of the random
# generated matrix
torch.randn(4,4) #2-D matrix


Output:

tensor([[ 0.1073,  0.8425, -0.4281,  0.2010],
       [ 1.3098, -0.0065, -1.9434,  0.1854],
       [-0.9948,  0.5385, -0.7217, -0.4963],
       [ 2.8455, -0.2791, -0.1963,  1.4643]])

5) torch.randperm() function:

This function returns a random permutation of integers.

Syntax:

torch.randperm(n, *, generator=None, out=None, dtype=torch.int64, layout=torch.strided, device=None, requires_grad=False, pin_memory=False) → Tensor

Returns a random permutation of integers from 0 to n – 1.

Parameters:

n (int) – the upper bound (exclusive)

Key Arguments:

  • generator (torch.Generator, optional) – a pseudorandom number generator for sampling
  • out (Tensor, optional) – the output tensor.
  • dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: torch.int64.
  • layout (torch.layout, optional) – the desired layout of returned Tensor. Default: torch.strided.
  • device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.
  • pin_memory (bool, optional) – If set, returned tensor would be allocated in the pinned memory. Works only for CPU tensors. Default: False.

Example:

In this example, we are generating random numbers from 0-5 just by passing the 6 as the parameter to the torch.randperm() function in python.

Python3




# this function will give the
# random permutation of the
# given range,
# if randperm(n), then it will
# give 0 to n-1 random permutated
# sequence
torch.randperm(6)


Output:

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads