Pytorch Reduction Functions

Pytorch library has many inbuilt functions for performing specific reduction operations on tensors. A reduction function is defined as a function that reduces multiple elements into a single result. In this chapter of Pytorch Tutorial, you will learn about a handful of these Pytorch reduction functions.

torch.mean()

torch.mean(x) returns the mean of all the elements within the tensor x.

mean() can also be called directly on the tensor.

Example

x = torch.tensor([[1., 4., 7.], [4., 9., 11.]])

print(torch.mean(x))
# Outputs- tensor(6.)

print(x.mean())
# Outputs- tensor(6.)

Note– The mean() functions accepts tensors with floating point datatype only.


torch.median()

torch.median(x) returns the median of all the elements within the tensor x.

median() can also be called directly on the tensor.

Example

x = torch.tensor([[1., 4., 1.], [11., 9., 11.]])

print(torch.median(x))
# Outputs- tensor(4.)

print(x.median())
# Outputs- tensor(4.)

Note– Unlike mean() function, median() function accepts tensors with floating point as well as integer datatype.


torch.mode()

torch.mode(x) returns a named tuple of the form (values, indices), where values contains the mode value for each row, and indices contains the index of the occurrence of last mode value in each row.

mode() can also be called directly on the tensor.

Example

x = torch.tensor([[1, 4, 7, 1], [11, 11, 7, 1]])

print(torch.mode(x))
# Outputs- torch.return_types.mode(values=tensor([1, 11]), indices=tensor([3, 1]))

print(x.mode())
# Outputs- torch.return_types.mode(values=tensor([1, 11]), indices=tensor([3, 1]))

Note– Notice how the values contain the mode values of each row and the indices contains the index of the last occurrence of the mode value in each row.

The mode function can also return mode values and indices about any arbitrary dimension, which can be provided to the function by the dim parameter.


torch.std()

torch.std(x) returns the standard deviation of all the elements in tensor x.

std() can also be called directly on the tensor.

Example

x = torch.tensor([[1., 4., 1.], [11., 9., 11.]])

print(torch.std(x))
# Outputs- tensor(4.7504)

print(x.std())
# Outputs- tensor(4.7504)

Note– std() function accepts tensors with floating point datatype only.


torch.var()

torch.var(x) returns the variance of all the elements in tensor x.

var() can also be called directly on the tensor.

Example

x = torch.tensor([[1., 4., 1.], [11., 9., 11.]])

print(torch.var(x))
# Outputs- tensor(22.5667)

print(x.var())
# Outputs- tensor(22.5667)

Note– var() function accepts tensors with floating point datatype only.


torch.prod()

torch.prod(x) returns the product of all the elements in tensor x.

prod() can also be called directly on the tensor.

Example

x = torch.tensor([[1, 4, 1], [11, 9, 11]])

print(torch.prod(x))
# Outputs- tensor(4356)

print(x.prod())
# Outputs- tensor(4356)

torch.max()

torch.max(x) returns the element with the maximum value in tensor x.

max() can also be called directly on the tensor.

Example

x = torch.tensor([[1, 4, 1], [11, 9, 11]])

print(torch.max(x))
# Outputs- tensor(11)

print(x.max())
# Outputs- tensor(11)

torch.min()

torch.min(x) returns the element with the minimum value in tensor x.

min() can also be called directly on the tensor.

Example

x = torch.tensor([[1, 4, 1], [11, 9, 11]])

print(torch.min(x))
# Outputs- tensor(1)

print(x.min())
# Outputs- tensor(1)