Python

PyTorch – Min()

We will see how to return the minimum values from a tensor using min() in this PyTorch tutorial.

PyTorch is an open-source framework available with a Python programming language. Tensor is a multidimensional array that is used to store the data. To use a tensor, we have to import the torch module. To create a tensor, the method used is tensor().

Syntax:

torch.tensor(data)

Where the data is a multi-dimensional array.

Min()

Min() in PyTorch is used to return the minimum of elements present in the input tensor object.

Syntax:

torch.min(tensor,dim)

Where:

1. The tensor is the input tensor.

2. Dim is to reduce the dimension. Dim=0 specifies the column comparison which gets the minimum values along a column and dim=1 specifies the row comparison which gets the minimum values along the row.

Return:

It also returns the indices of minimum values.

Example 1:

In this example, we will create a tensor with 2 dimensions that has 3 rows and 5 columns and apply the min() function on rows and columns.

#import torch module

import torch

 

#create a tensor with 2 dimensions (3 * 5)

#with random elements using randn() function

data = torch.randn(3,5)

 

#display

print(data)

 

print()

 

#get minimum values along columns

print("Minimum values across columns:")

print(torch.min(data, dim=0))

 

print()

 

#get minimum values along rows

print("Minimum values across rows:")

print(torch.min(data, dim=1))

Output:

tensor([[ 1.2472e-01, -8.7776e-01, 4.5338e-01, 2.2461e-01, -1.4291e+00],

[ 2.6528e+00, -1.1316e-03, 1.4365e+00, 3.8547e-01, 2.1671e-01],

[-7.2345e-01, -4.1827e-01, 4.8590e-01, -1.3218e+00, 1.5717e+00]])

Minimum values across columns:

torch.return_types.min(

values=tensor([-0.7235, -0.8778, 0.4534, -1.3218, -1.4291]),

indices=tensor([2, 0, 0, 2, 0]))

Minimum values across rows:

torch.return_types.min(

values=tensor([-1.4291e+00, -1.1316e-03, -1.3218e+00]),

indices=tensor([4, 1, 3]))

We can see that the minimum values are returned across the columns and rows along with their indices.

Example 2:

Create a Tensor with 5 * 5 matrix and return the minimum values across the rows and columns.

#import torch module

import torch

 

#create a tensor with 2 dimensions (5 * 5)

#with random elements using randn() function

data = torch.randn(5,5)

 

#display

print(data)

 

print()

 

#get Minimum values along columns

print("Minimum values across columns:")

print(torch.min(data, dim=0))

 

print()

 

#get Minimum values along rows

print("Minimum values across rows:")

print(torch.min(data, dim=1))

Output:

tensor([[ 0.3584, -0.8393, -0.3111, -0.4203, 1.4332],

[ 1.2702, 2.4583, -1.5547, -1.4465, 1.0672],

[-0.2497, -1.7490, 0.2130, 0.3989, -0.1520],

[-1.1165, -2.1209, 0.7191, 0.4764, 2.6431],

[ 1.8286, 0.8787, -0.4475, 1.1866, -1.4123]])

Minimum values across columns:

torch.return_types.min(

values=tensor([-1.1165, -2.1209, -1.5547, -1.4465, -1.4123]),

indices=tensor([3, 3, 1, 1, 4]))

Minimum values across rows:

torch.return_types.min(

values=tensor([-0.8393, -1.5547, -1.7490, -2.1209, -1.4123]),

indices=tensor([1, 2, 1, 1, 4]))

We can see that the minimum values across the rows and columns were returned along with their indices.

Without the Dim Parameter

If we don’t specify the dim parameter, it returns the minimum value from the entire tensor.

Example 1:

Create a 2D tensor with 5*5 matrix and return the minimum value.

#import torch module

import torch

 

#create a tensor with 2 dimensions (5 * 5)

#with random elements using randn() function

data = torch.randn(5,5)

 

#display

print(data)

 

print()

 

#get minimum value

print("Minimum value :")

print(torch.min(data))

Output:

tensor([[-0.5350, 0.5439, -0.1100, -1.0623, -1.3757],

[ 1.5085, -1.0191, 0.4068, -0.4972, 0.3982],

[-0.3360, 0.2665, -0.3139, 0.7079, 0.6624],

[-0.5330, 0.0763, -0.8529, -0.5675, 0.0718],

[ 0.4249, -1.3827, -1.7805, -1.1841, -0.5587]])

Minimum value :

tensor(-1.7805)

Example 2:

Create a 1D tensor with 5 values and return the minimum value.

#import torch module

import torch

#create a tensor with 5 numeric values

data = torch.tensor([10.6,20.7,30.6,40.4,50.0])

#display

print(data)

print()

#get Minimum value

print("Minimum value :")

print(torch.min(data))

Output:

tensor([10.6000, 20.7000, 30.6000, 40.4000, 50.0000])

Minimum value :

tensor(10.6000)

Work with CPU

If you want to run a min() function on the CPU, we have to create a tensor with a cpu() function. This will run on a CPU machine.

When we create a tensor, this time, we can use the cpu() function.

Syntax:

torch.tensor(data).cpu()

Example 1:

In this example, we will create a tensor with 2 dimensions that has 3 rows and 5 columns with the cpu() function and apply the min() function on rows and columns.

#import torch module

import torch

#create a tensor with 2 dimensions (3 * 5)

#with random elements using randn() function

data = torch.randn(3,5).cpu()

#display

print(data)

print()

#get Minimum values along columns

print("Minimum values across columns:")

print(torch.min(data, dim=0))

print()

#get Minimum values along rows

print("Minimum values across rows:")

print(torch.min(data, dim=1))

Output:

tensor([[-0.7268, -0.6932, 1.3316, -1.3355, -0.5170],

[ 1.1113, -1.1252, 0.4458, -0.7343, 2.2207],

[-0.3300, 0.7784, -0.6643, 0.7307, 1.4468]])

Minimum values across columns:

torch.return_types.min(

values=tensor([-0.7268, -1.1252, -0.6643, -1.3355, -0.5170]),

indices=tensor([0, 1, 2, 0, 0]))

Minimum values across rows:

torch.return_types.min(

values=tensor([-1.3355, -1.1252, -0.6643]),

indices=tensor([3, 1, 2]))

We can see that the minimum values are returned across the columns and rows along with their indices.

Example 2:

Create a Tensor with 5 * 5 matrix with the cpu() function and return the minimum values across the rows and columns.

#import torch module

import torch

 

#create a tensor with 2 dimensions (5 * 5)

#with random elements using randn() function

data = torch.randn(5,5).cpu()

 

#display

print(data)

 

print()

 

#get Minimum values along columns

print("Minimum values across columns:")

print(torch.min(data, dim=0))

 

print()

 

#get Minimum values along rows

print("Minimum values across rows:")

print(torch.min(data, dim=1))

Output:

tensor([[-0.4774, -0.6484, -1.5810, 0.9154, 0.9417],

[-1.1097, -0.9460, 1.3099, 2.0782, -0.3319],

[ 0.2239, 1.1931, -0.8064, -1.5089, 2.0238],

[-0.6963, -0.0779, 0.1755, 0.9848, 1.3191],

[ 1.0035, -0.2865, 1.6750, 0.0255, 1.2538]])

Minimum values across columns:

torch.return_types.min(

values=tensor([-1.1097, -0.9460, -1.5810, -1.5089, -0.3319]),

indices=tensor([1, 1, 0, 2, 1]))

Minimum values across rows:

torch.return_types.min(

values=tensor([-1.5810, -1.1097, -1.5089, -0.6963, -0.2865]),

indices=tensor([2, 0, 3, 0, 1]))

We can see that the minimum values across the rows and columns were returned along with their indices.

Conclusion

In this PyTorch lesson, we learned about the min() function and how to apply it on a tensor to return the minimum values across the columns and rows. It also returns the index positions along with the returned minimum values.

We also created a tensor with the cpu() function and returned the minimum values. If the dim is not specified in two or multi-dimensional tensor, it returns the minimum value from the entire tensor.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain