Python

PyTorch – cummax()

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.cummax()

torch.cummax() returns the cumulative maximum of elements in a two-dimensional tensor across rows or across columns. It also returns the indices of the returned maximum values.

Syntax:

torch.cummax(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 maximum 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 Maximum across row: ")
#return cumulative Maximum
print(torch.cummax(data1,1))

Output:

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

Working:

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

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

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

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

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

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

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

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

Example 2:

In this example, we will create a tensor that has four rows and four columns and return the cumulative maximum 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 Maximum across column: ")
#return cumulative Maximum
print(torch.cummax(data1,0))

Output:

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

Working:
Column-1: 2,maximum(2,1),maximum(2,1,2),maximum(2,1,2,2) =[ 2, 2,2,2]

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

Column-2: 3,maximum(3,3),maximum(3,3,3),maximum(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,maximum(4,5),maximum(4,5,2),maximum(4,5,2,4)= [4,5,5,5]

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

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

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

Work with CPU

If you want to run a cummax() 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 and return the cumulative maximum 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 Maximum across row: ")
#return cumulative Maximum
print(torch.cummax(data1,1))

Output:

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

Working:

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

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

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

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

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

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

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

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

Example 2:

In this example, we will create a tensor that has four rows and four columns and return the cumulative maximum 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 Maximum across column: ")
#return cumulative Maximum
print(torch.cummax(data1,0))

Output:

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

Working:

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

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

Column-2: 3,maximum(3,3),maximum(3,3,3),maximum(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,maximum(4,5),maximum(4,5,2),maximum(4,5,2,4)= [4,5,5,5]

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

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

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

Conclusion

In this PyTorch tutorial, we saw how to perform a cumulative maximum operation on a tensor using torch.cummax() function. It returns the cumulative maximum of elements in a two-dimensional tensor and also indexes positions of maximum 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