The torch.cumprod() is a PyTorch method that calculates the cumulative product of elements along a specified dimension (dim) of a tensor. The output of this method is a new tensor where each element is the product of all preceding elements (including itself).

Syntax
torch.cumprod(input, dim, dtype=None, out=None)
Parameters
Argument | Description |
input (Tensor) | It represents an input tensor, whose cumulative product you want to find. |
dim (int) | It is the dimension to calculate the cumulative product. |
dtype (torch.dtype, optional) | It overrides the output tensor data type (prevents overflow). |
out (Tensor, Optional) | If you have a pre-allocated tensor, you can define an output tensor ( It must match the expected size). |
Cumulative product of 1D Tensor
Let’s define a 1D tensor of 4 elements and find its cumulative product.
import torch tensor = torch.tensor([3, 4, 2, 5]) cumprod_tensor = torch.cumprod(tensor, dim=0) print(cumprod_tensor) # Output: tensor([ 3, 12, 24, 120]) # tensor ([3, 3x4 = 12, 3x4x2 = 24, 3x4x2x5 = 120])
2D Tensor (along rows vs. columns)
If you are working with a 2D tensor, you have two dimensions:
- dim=0 → reduce along rows → operate column-wise
- dim=1 → reduce along columns → operate row-wise
dim=0 (Along rows)

import torch tensor_2d = torch.tensor([[10, 20, 30], [4, 5, 6]]) cumprod_along_rows = torch.cumprod(tensor_2d, dim=0) print(cumprod_along_rows) # operate on column-wise # Output: # tensor([[ 10, 20, 30], # [ 40, 100, 180]]) # tensor ([[10, 20, 30], # [4x10 = 40, 5x20 = 100, 6x30 = 180]])
The first row is unchanged.
The second row becomes:
- 4×10 = 40
- 5×20 = 100
- 6×30 = 180
dim=1 (Along columns)

Along columns horizontally, within each row.
import torch tensor_2d = torch.tensor([[10, 20, 30], [4, 5, 6]]) cumprod_along_columns = torch.cumprod(tensor_2d, dim=1) print(cumprod_along_columns) # Operate on row-wise # Output: # tensor([[ 10, 200, 6000], # [ 4, 20, 120]]) # tensor ([[10, 10x20=200, 30x20x10=6000], # [4, 4x5=20, 4x5 = 20, 4x5x6=120]])
Row 0: [10, 20, 30]
Cumulative product:
- Element 0: 10
- Element 1: 10 × 20 = 200
- Element 2: 200 × 30 = 6000
Output: [10, 200, 6000]
Row 1: [4, 5, 6]
Cumulative product:
- Element 0: 4
- Element 1: 4 × 5 = 20
- Element 2: 20 × 6 = 120
Output: [4, 20, 120]
Zero in input
What if there is a 0 in the input tensor? What should be the output? Well, anything multiplied by 0 is 0. However, it depends on the position; it found 0.
For example, if 0 is not in the first position, the first element won’t be 0. If it is placed in the second position, apart from the first element, all other elements will be 0.
import torch tensor_with_zero = torch.tensor([3, 0, 5, 2]) cumprod = torch.cumprod(tensor_with_zero, dim=0) print(cumprod) # Output: tensor([3, 0, 0, 0])
You can see that in the output tensor, the first element is non-zero and all the other elements are 0.
NaN Propagation
What if any element is NaN? Well, the same thing happens here. Anything multiplied by NaN is NaN.
import torch tensor_with_nan = torch.tensor([12, float('nan'), 10, 34]) cumprod_nan = torch.cumprod(tensor_with_nan, dim=0) print(cumprod_nan) # Output: tensor([12., nan, nan, nan])
Empty tensor
If the input tensor is empty, the output tensor will be empty too!import torch empty_tensor = torch.tensor([]) cumprod_empty = torch.cumprod(empty_tensor, dim=0) print(cumprod_empty) # Output: tensor([])
Negative values
If the input tensor contains negative values, the output tensor will also contain negative values. Again, this depends on the position they are placed in.
import torch tensor_with_negative = torch.tensor([-1, 2, -4]) cumprod_negative = torch.cumprod(tensor_with_negative, dim=0) print(cumprod_negative) # Output: tensor([-1, -2, 8])That’s all!