Python

# PyTorch – cumprod()

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

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

Syntax:

torch.cumprod(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 product of each element across the row.

product 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 Product across row: ")
#return cumulative Product
print(torch.cumprod(data1,1))

Output:

Actual Tensor:
tensor([[2, 3, 4, 5],
[1, 3, 5, 3],
[2, 3, 2, 1],
[2, 3, 4, 2]])
Cumulative Product across row:
tensor([[  2,   6,  24, 120],
[  1,   3,  15,  45],
[  2,   6,  12,  12],
[  2,   6,  24,  48]])

Working:

Row-1: 2,2*3,2*3*4,2*3*4*5 = [2, 6, 24, 120]

Row-2: 1,1*3,1*3*5,1*3*5*3 = [ 1, 3, 15, 45]

Row-3: 2,2*3,2*3*2,2*3*2*1= [2, 6, 12, 12]

Row-4: 2,2*3,2*3*4,2*3*4*2 = [ 2, 6, 24, 48]

## Example 2:

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

Output:

Actual Tensor:
tensor([[2, 3, 4, 5],
[1, 3, 5, 3],
[2, 3, 2, 1],
[2, 3, 4, 2]])
Cumulative Product across column:
tensor([[  2,   3,   4,   5],
[  2,   9,  20,  15],
[  4,  27,  40,  15],
[  8,  81, 160,  30]])

Working:

Column-1: 2,2*1,2*1*2,2*1*2*2 =[ 2, 2,4,8]

Column-2: 3,3*3,3*3*3,3*3*3*3 = [ 3,9,27,81]

Column-3: 4,4*5,4*5*2,4*5*2*4= [4,20,40,160]

Column-4: 5,5*3,5*3*1,5*3*1*2 = [ 5,15,15,30]

## Work with CPU

If you want to run a cumprod() 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 product 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 Product across row: ")
#return cumulative Product
print(torch.cumprod(data1,1))

Output:

Actual Tensor:
tensor([[2, 3, 4, 5],
[1, 3, 5, 3],
[2, 3, 2, 1],
[2, 3, 4, 2]])
Cumulative Product across row:
tensor([[  2,   6,  24, 120],
[  1,   3,  15,  45],
[  2,   6,  12,  12],
[  2,   6,  24,  48]])

Working:

Row-1: 2,2*3,2*3*4,2*3*4*5 = [2, 6, 24, 120]

Row-2: 1,1*3,1*3*5,1*3*5*3 = [ 1, 3, 15, 45]

Row-3: 2,2*3,2*3*2,2*3*2*1= [2, 6, 12, 12]

Row-4: 2,2*3,2*3*4,2*3*4*2 = [ 2, 6, 24, 48]

## Example 2:

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

Output:

Actual Tensor:
tensor([[2, 3, 4, 5],
[1, 3, 5, 3],
[2, 3, 2, 1],
[2, 3, 4, 2]])
Cumulative Product across column:
tensor([[  2,   3,   4,   5],
[  2,   9,  20,  15],
[  4,  27,  40,  15],
[  8,  81, 160,  30]])

Working:
Column-1: 2,2*1,2*1*2,2*1*2*2 =[ 2, 2,4,8]

Column-2: 3,3*3,3*3*3,3*3*3*3 = [ 3,9,27,81]

Column-3: 4,4*5,4*5*2,4*5*2*4= [4,20,40,160]

Column-4: 5,5*3,5*3*1,5*3*1*2 = [ 5,15,15,30]

## Conclusion

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