Reduction and Comparison

In this lesson, we will look at some reduction and comparison operations.

Reduction functions

The reduction operator is a type of operator that is commonly used in parallel programming to reduce the elements of an array into a single result. For example, we can calculate the mean value of a float array is a reduction operation.

PyTorch provides some useful functions for reduction. If you have used NumPy before, you may notice the usage and name are the same. Below are some important functions, they all have a parameter dim, which indicates on which dimension the summarization is performed. The default value for dim is 0.

Function Name Purpose
mean Get the mean value of the tensor in the given dimension.
sum Sum the values of the tensor over the given dimension.
median Get the median value of tensor in the given dimension.
std Compute the standard deviation of the tensor over the given dimension.
prod Product the values of the tensor over the given dimension.
cumsum Cumulative sum of values of the tensor over the given dimension.

Get hands-on with 1400+ tech skills courses.