Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.cumprod function

PyTorch torch.cumprod function


PyTorch torch reference manual 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

)

print

(

"Input:"

,

x

)

print

(

"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

)

print

(

"nCumulative product by column:"

)

print

(

result_col

)

tensor([[ 1,  2,  3],

        [ 4, 10, 18]])

Cumulative by row

result_row

=

torch.

cumprod

(

x

,

dim

=

1

)

print

(

"nCumulative product by row:"

)

print

(

result_row

)

tensor([[  1,   2,   6],

        [  4,  20, 120]])


PyTorch torch reference manual PyTorch torch reference manual