PyTorch torch.cumprod function
PyTorch torch reference manual
torch.cumprod is a PyTorch function for computing the cumulative product. It returns the cumulative product along a specified dimension, i.e. the product of all elements from the start up to the current position.
Function definition
torch.cumprod(input, dim, dtype=None)
Usage examples
Example
import
torch
Compute the cumulative product
x
=
torch.
tensor
(
[
1
,
2
,
3
,
4
,
5
]
)
result
=
torch.
cumprod
(
x
,
dim
=
0
)
(
"Input:"
,
x
)
(
"Cumulative product:"
,
result
)
Output: tensor([1, 2, 6, 24, 120])
Note: 1, 12=2, 123=6, 1234=24, 12345=120
2D tensor
x
=
torch.
tensor
(
[
[
1
,
2
,
3
]
,
[
4
,
5
,
6
]
]
,
dtype
=
torch.
float32
)
Cumulative by column
result_col
=
torch.
cumprod
(
x
,
dim
=
0
)
(
"nCumulative product by column:"
)
(
result_col
)
tensor([[ 1, 2, 3],
[ 4, 10, 18]])
Cumulative by row
result_row
=
torch.
cumprod
(
x
,
dim
=
1
)
(
"nCumulative product by row:"
)
(
result_row
)