Basic Statistical Operations — PyTorch
By: Mohammed Ismail P
Tags: Deep Learning, Pytorch, Numpy, Machine Learning, Statistics

According to Wikipedia, PyTorch is an open source machine learning library based on the Torch library, used for applications such as computer vision and Natural Language Processing (NLP), primarily developed by Facebook’s AI Research lab. In addition to this, PyTorch provides two high-level features:

1. Tensor computation (like NumPy) with strong GPU acceleration

2. Deep neural networks built on a tape-based autograd system

We will be discussing the 5 basic Statistical operation as statistics is used to process complex problems in the real world so that Data Scientists and Analysts can look for meaningful trends and changes in Data.

Before we begin, let’s install and import PyTorch

Function 1 — Maxima / Minima

Example 1 — Maxima — torch.amax:

Returns the maximum value of each slice of the input tensor in the given dimension(s) dim.

Maximum of the flattened tensor matrix

Maxima along dimension 0

Example 2 — Minima — torch.amin:

Returns the minimum value of each slice of the input tensor in the given dimension(s) dim.

Minimum of the flattened tensor matrix

Minima along dimension 0

Example 3 — IndexError:

IndexError occurred due to Dimension out of range

Function 2 — Sum

Example 1 — Sum — torch.sum:

Returns the sum of all elements in the input tensor

Sum of the flattened tensor matrix

Example 2 — Sum of Dimension:

Returns the sum of each row of the input tensor in the given dimension dim.

Sum of tensor matrix along dimension 0

Example 3 — IndexError:

IndexError occurred due to Dimension out of range

Function 3 — Mean / Median / Mode

Example 1 — Mean — torch.mean:

Returns the mean value of all elements in the input tensor

Mean of the flattened tensor matrix

Mean of tensor along dimension 0

Example 2 — Median — torch.median:

Returns the median value of all elements in the input tensor

Median of flattened tensor matrix

Note: The median is not unique for input tensors with an even number of elements. In this case the lower of the two medians is returned. To compute the mean of both medians in input, use torch.quantile() with q=0.5 instead.

Median of tensor along dimension 0

Returns a namedtuple (values, indices) where values is the median value of each row of the input tensor in the given dimension dim. And indices is the index location of each median value found.

Example 3 — Mode — torch.mode:

Returns a namedtuple (values, indices) where values is the mode value of each row of the input tensor in the given dimension dim , i.e. a value which appears most often in that row, and indices is the index location of each mode value found.

Mode of tensor matrix along dimension -1 i.e. dim=1

Mode of tensor matrix along dimension 0

Example 4 — IndexError:

IndexError due to dimension out of range

Function 4 — Standard Deviation

Example 1 — Standard deviation — torch.std:

Returns the Standard deviation of all elements in the input tensor. The standard-deviation will be calculated via the biased estimator.

Standard deviation of the flattened tensor matrix

Example 2 — Standard deviation of Dimension:

Returns the standard-deviation of each row of the input tensor in the dimension dim . If dim is a list of dimensions, reduce over all of them.

Standard deviation of tensor matrix along dimension 0

Example 3 — IndexError:

IndexError occurred due to Dimension out of range

Function 5 — Variance

Example 1 — Variance — torch.var:

Returns the variance of all elements in the input tensor. The variance will be calculated via the biased estimator.

Variance of the flattened tensor matrix

Example 2 — Variance of Dimension:

Returns the variance of each row of the input tensor in the given dimension dim .

Variance of tensor matrix along dimension 0

Example 3 — IndexError:

IndexError occurred due to Dimension out of range

Conclusion

These are some of the basic terminology used in statistics. Further more, we can refer PyTorch documention.

Reference Links