pytorch

PyTorch – cumsum()

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

torch.cumsum() returns the cumulative sum of elements in a two-dimensional tensor across rows or across columns.

Syntax:
torch.cumsum(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 sum 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 Sum across row: ")
#return cumulative sum
print(torch.cumsum(data1,1))

Output:

Actual Tensor:
tensor([[2, 3, 4, 5],
        [1, 3, 5, 3],
        [2, 3, 2, 1],
        [2, 3, 4, 2]])
Cumulative Sum across row:
tensor([[ 2,  5,  9, 14],
        [ 1,  4,  9, 12],
        [ 2,  5,  7,  8],
        [ 2,  5,  9, 11]])

Working:
Row-1: 2,2+3,2+3+4,2+3+4+5 = [ 2, 5, 9, 14]
Row-2: 1,1+3,1+3+5,1+3+5+3 = [ 1, 4, 9, 12]

Row-3: 2,2+3,2+3+2,2+3+2+1= [ 2, 5, 7, 8]
Row-4: 2,2+3,2+3+4,2+3+4+2 = [ 2, 5, 9, 11]

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

Output:

Actual Tensor:
tensor([[2, 3, 4, 5],
        [1, 3, 5, 3],
        [2, 3, 2, 1],
        [2, 3, 4, 2]])
Cumulative Sum across column:
tensor([[ 2,  3,  4,  5],
        [ 3,  6,  9,  8],
        [ 5,  9, 11,  9],
        [ 7, 12, 15, 11]])

Working:
Column-1: 2,2+1,2+1+2,2+1+2+2 =[ 2, 3, 5, 7]
Column-2: 3,3+3,3+3+3,3+3+3+3 = [ 3,6,9,12]
Column-3: 4,4+5,4+5+2,4+5+2+4= [4,9,11,15]
Column-4: 5,5+3,5+3+1,5+3+1+2 = [ 5,8,9,11]

Work with CPU

If you want to run a cumsum() 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 on the CPU that has four rows and four columns and return the cumulative sum 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 Sum across row: ")
#return cumulative sum
print(torch.cumsum(data1,1))

Output:

Actual Tensor:
tensor([[2, 3, 4, 5],
        [1, 3, 5, 3],
        [2, 3, 2, 1],
        [2, 3, 4, 2]])
Cumulative Sum across row:
tensor([[ 2,  5,  9, 14],
        [ 1,  4,  9, 12],
        [ 2,  5,  7,  8],
        [ 2,  5,  9, 11]])

Working:
Row-1: 2,2+3,2+3+4,2+3+4+5 = [ 2, 5, 9, 14]
Row-2: 1,1+3,1+3+5,1+3+5+3 = [ 1, 4, 9, 12]

Row-3: 2,2+3,2+3+2,2+3+2+1= [ 2, 5, 7, 8]
Row-4: 2,2+3,2+3+4,2+3+4+2 = [ 2, 5, 9, 11]

Example 2:
In this example, we will create a tensor on the CPU that has four rows and four columns and return the cumulative sum 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 Sum across column: ")
#return cumulative sum
print(torch.cumsum(data1,0))

Output:

Actual Tensor:
tensor([[2, 3, 4, 5],
        [1, 3, 5, 3],
        [2, 3, 2, 1],
        [2, 3, 4, 2]])
Cumulative Sum across column:
tensor([[ 2,  3,  4,  5],
        [ 3,  6,  9,  8],
        [ 5,  9, 11,  9],
        [ 7, 12, 15, 11]])

Working:
Column-1: 2,2+1,2+1+2,2+1+2+2 =[ 2, 3, 5, 7]
Column-2: 3,3+3,3+3+3,3+3+3+3 = [ 3,6,9,12]
Column-3: 4,4+5,4+5+2,4+5+2+4= [4,9,11,15]
Column-4: 5,5+3,5+3+1,5+3+1+2 = [ 5,8,9,11]

Conclusion

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