5 Statistical Functions in PyTorch

PyTorch functions useful for machine learning

Anurag Lahon
Towards Data Science

--

PyTorch logo official

PyTorch is a Python package that provides two high-level features:

  • Tensor computation (like NumPy) with strong GPU acceleration
  • Deep neural networks built on a tape-based autograd system

PyTorch is designed to be intuitive, linear in thought and easy to use. When you execute a line of code, it gets executed. It has minimal framework overhead. It is fast — whether you run small or large neural networks.

In this blog we will discussed 5 Statistical Functions in PyTorch that I find interesting:

  • torch.bernoulli()
  • torch.poisson()
  • torch.normal()
  • torch.normal_()
  • torch.norm()
import libraries

We create a custom tensor and passed to bernoulli function it return a binary number (0 or 1).

If we create a tensor of All one’s .Probability of drawing “1” is 1.

All zero’s in the matrix.probability of drawing "1" is 0

These are 3 types of example of torch.bernoulli() function.

Bernoulli Distribution is a random experiment that has only two outcomes (usually called a “Success” or a “Failure”). It is best used when we have two outcomes of a given event.

Bernoulli distribution

A input tensor contain rate parameter between 0 and 6 with 3 rows and 3 columns, returns a output from a poission distribution.

A input tensor contain rate parameter between 0 and 7 with 5 rows and 4 columns, returns a output from a poission distribution

The Poisson distribution is popular for modeling the number of times an event occurs in an interval of time or space.

It returns a tensor of random numbers drawn from separate normal distributions.

It returns a tensor of random numbers drawn from separate normal distributions whose standard deviation is 1.

It does not work when mean is 0 and std is 1 so you will learn about tensor.normal_() in the next function.

The normal distribution is a probability function that describes how the values of a variable are distributed.

It creates a standard normal distribution with mean =0 and std=1.

It create a matrix Z (a 1d tensor) of dimension 1 × 5, filled with random elements samples from the normal distribution parameterized by mean = 4 and std = 0.5.

It helps to create standard normal distribution

It returns vector norm of a given tensor where dim =0.

It returns vector norm of a given tensor where dim =1.

It returns vector norm of a given tensor where dim =1 and p=1.

Matrix norms are indirectly used in any applications that require matrix functions and/or matrix series.

Conclusion

These are the 5 statistical PyTorch function that I find interesting as discussed above and you can find more in the PyTorch documentation.

Reference Links

Provide links to your references about tensors

--

--