Python

PyTorch – cummin()

PyTorch is an open-source framework for the Python programming language.

A tensor is a multidimensional array that is used to store data. So 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 data is a multi-dimensional array.

torch.cummin()

The cumulative minimum of elements in a two-dimensional tensor across rows or columns is returned by torch.cummin(). It also returns the indices of returned minimum values.

Syntax:

torch.cummin(tensor_object,dim)

Parameters:

  1. It takes tensor_object as the first parameter. It has to be two-dimensional.
  2. dim=0 specifies column-wise computation and dim=1 specifies row-wise computation.

Example 1:

In this example, we will create a tensor that has four rows and four columns and return the cumulative minimum of each element across the row.

#import torch module
import torch
 
 #create  tensor
data1 = torch.tensor([[2,3,4,5],[1,3,5,3],[2,3,2,1],[2,3,4,2]])
 
#display
print("Actual Tensor: ")
print(data1)
 
print("Cumulative Minimum across row: ")
#return cumulative Minimum
print(torch.cummin(data1,1))

Output:

Actual Tensor:
tensor([[2, 3, 4, 5],
        [1, 3, 5, 3],
        [2, 3, 2, 1],
        [2, 3, 4, 2]])
Cumulative Minimum across row:
torch.return_types.cummin(
values=tensor([[2, 2, 2, 2],
        [1, 1, 1, 1],
        [2, 2, 2, 1],
        [2, 2, 2, 2]]),
indices=tensor([[0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 2, 3],
        [0, 0, 0, 3]]))

Working:
Row-1: 2,minimum(2,3),minimum(2,3,4),minimum(2,3,4,5) = [2, 2, 2, 2]

So, [2,2,2,2] Index positions in actual tensor are – [0,0,0,0]

Row-2: 1,minimum(1,3),minimum(1,3,5),minimum(1,3,5,3) = [ 1,1,1,1]

So, [ 1,1,1,1] Index positions in actual tensor are – [0,0,0,0]

Row-3: 2,minimum(2,3),minimum(2,3,2),minimum(2,3,2,1) = [ 2,2,2,1]

So, [ 2,2,2,1] Index positions in actual tensor are – [0,0,2,3]

Row-4: 2,minimum(2,3),minimum(2,3,4),minimum(2,3,4,2) = [ 2,2,2,2]

So, [ 2,2,2,2] Index positions in actual tensor are – [0,0,0,3]

Example 2:

In this example, we will create a tensor that has four rows and four columns and return the cumulative minimum of each element across the column.

#import torch module
import torch
 
 
#create  tensor
data1 = torch.tensor([[2,3,4,5],[1,3,5,3],[2,3,2,1],[2,3,4,2]])
 
#display
print("Actual Tensor: ")
print(data1)
 
print("Cumulative Minimum across column: ")
#return cumulative Minimum
print(torch.cummin(data1,0))

Output:

Actual Tensor:
tensor([[2, 3, 4, 5],
        [1, 3, 5, 3],
        [2, 3, 2, 1],
        [2, 3, 4, 2]])
Cumulative Minimum across column:
torch.return_types.cummin(
values=tensor([[2, 3, 4, 5],
        [1, 3, 4, 3],
        [1, 3, 2, 1],
        [1, 3, 2, 1]]),
indices=tensor([[0, 0, 0, 0],
        [1, 1, 0, 1],
        [1, 2, 2, 2],
        [1, 3, 2, 2]]))

Working:

Column-1: 2,minimum(2,1),minimum(2,1,2),minimum(2,1,2,2) =[ 2, 1,1,1]

So, [2, 1,1,1] Index positions in actual tensor are – [0,1,1,1]

Column-2: 3,minimum(3,3),minimum(3,3,3),minimum(3,3,3,3) = [ 3,3,3,3]

So, [ 3,3,3,3] Index positions in actual tensor are – [0,1,2,3]

Column-3: 4,minimum(4,5),minimum(4,5,2),minimum(4,5,2,4)= [4,4,2,2]

So, [4,4,2,2] Index positions in actual tensor are – [0,0,2,2]

Column-4: 5,minimum(5,3),minimum(5,3,1),minimum(5,3,1,2) = [ 5,3,1,1]

So, [5,3,1,1] Index positions in actual tensor are – [0,1,2,2]

Work with CPU

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

At this time, when we are creating a tensor, we can use the cpu() function.

Syntax:

torch.tensor(data).cpu()

Example 1:

In this example, we will create a tensor that has four rows and four columns on the CPU and return the cumulative minimum of each element across the row.

#import torch module
import torch
 
 #create  tensor
data1 = torch.tensor([[2,3,4,5],[1,3,5,3],[2,3,2,1],[2,3,4,2]]).cpu()
 
#display
print("Actual Tensor: ")
print(data1)
 
print("Cumulative Minimum across row: ")
#return cumulative Minimum
print(torch.cummin(data1,1))

Output:

Actual Tensor:
tensor([[2, 3, 4, 5],
        [1, 3, 5, 3],
        [2, 3, 2, 1],
        [2, 3, 4, 2]])
Cumulative Minimum across row:
torch.return_types.cummin(
values=tensor([[2, 2, 2, 2],
        [1, 1, 1, 1],
        [2, 2, 2, 1],
        [2, 2, 2, 2]]),
indices=tensor([[0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 2, 3],
        [0, 0, 0, 3]]))

Working:

Row-1: 2,minimum(2,3),minimum(2,3,4),minimum(2,3,4,5) = [2, 2, 2, 2]

So, [2,2,2,2] Index positions in actual tensor are – [0,0,0,0]

Row-2: 1,minimum(1,3),minimum(1,3,5),minimum(1,3,5,3) = [ 1,1,1,1]

So, [ 1,1,1,1] Index positions in actual tensor are – [0,0,0,0]

Row-3: 2,minimum(2,3),minimum(2,3,2),minimum(2,3,2,1) = [ 2,2,2,1]

So, [ 2,2,2,1] Index positions in actual tensor are – [0,0,2,3]

Row-4: 2,minimum(2,3),minimum(2,3,4),minimum(2,3,4,2) = [ 2,2,2,2]

So, [ 2,2,2,2] Index positions in actual tensor are – [0,0,0,3]

Example 2:

In this example, we will create a tensor that has four rows and four columns on the CPU and return the cumulative minimum of each element across the column.

#import torch module
import torch
 
 
#create  tensor
data1 = torch.tensor([[2,3,4,5],[1,3,5,3],[2,3,2,1],[2,3,4,2]]).cpu()
 
#display
print("Actual Tensor: ")
print(data1)
 
print("Cumulative Minimum across column: ")
#return cumulative Minimum
print(torch.cummin(data1,0))

Output:

Actual Tensor:
tensor([[2, 3, 4, 5],
        [1, 3, 5, 3],
        [2, 3, 2, 1],
        [2, 3, 4, 2]])
Cumulative Minimum across column:
torch.return_types.cummin(
values=tensor([[2, 3, 4, 5],
        [1, 3, 4, 3],
        [1, 3, 2, 1],
        [1, 3, 2, 1]]),
indices=tensor([[0, 0, 0, 0],
        [1, 1, 0, 1],
        [1, 2, 2, 2],
        [1, 3, 2, 2]]))

Working:

Column-1: 2,minimum(2,1),minimum(2,1,2),minimum(2,1,2,2) =[ 2, 1,1,1]

So, [2, 1,1,1] Index positions in actual tensor are – [0,1,1,1]

Column-2: 3,minimum(3,3),minimum(3,3,3),minimum(3,3,3,3) = [ 3,3,3,3]

So, [ 3,3,3,3] Index positions in actual tensor are – [0,1,2,3]

Column-3: 4,minimum(4,5),minimum(4,5,2),minimum(4,5,2,4)= [4,4,2,2]

So, [4,4,2,2] Index positions in actual tensor are – [0,0,2,2]

Column-4: 5,minimum(5,3),minimum(5,3,1),minimum(5,3,1,2) = [ 5,3,1,1]

So, [5,3,1,1] Index positions in actual tensor are – [0,1,2,2]

Conclusion

In this PyTorch tutorial, we saw how to perform a cumulative minimum operation on a tensor using the torch.cummin() function. It returns the cumulative minimum of elements in a two-dimensional tensor and also indexes the positions of minimum values across rows or across columns. We also implemented this function on the CPU using the cpu() function.

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