cumprod

paddle. cumprod ( x, dim=None, dtype=None, name=None ) [source]

Compute the cumulative product of the input tensor x along a given dimension dim.

Note

The first element of the result is the same as the first element of the input.

Parameters
  • x (Tensor) – the input tensor need to be cumproded.

  • dim (int, optional) – the dimension along which the input tensor will be accumulated. It need to be in the range of [-x.rank, x.rank), where x.rank means the dimensions of the input tensor x and -1 means the last dimension.

  • dtype (str, optional) – The data type of the output tensor, can be float32, float64, int32, int64, complex64, complex128. If specified, the input tensor is casted to dtype before the operation is performed. This is useful for preventing data type overflows. The default value is None.

  • name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.

Returns

Tensor, the result of cumprod operator.

Examples

>>> import paddle

>>> data = paddle.arange(12)
>>> data = paddle.reshape(data, (3, 4))
>>> data
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
[[0 , 1 , 2 , 3 ],
 [4 , 5 , 6 , 7 ],
 [8 , 9 , 10, 11]])

>>> y = paddle.cumprod(data, dim=0)
>>> y
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
[[0  , 1  , 2  , 3  ],
 [0  , 5  , 12 , 21 ],
 [0  , 45 , 120, 231]])

>>> y = paddle.cumprod(data, dim=-1)
>>> y
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
[[0   , 0   , 0   , 0   ],
 [4   , 20  , 120 , 840 ],
 [8   , 72  , 720 , 7920]])

>>> y = paddle.cumprod(data, dim=1, dtype='float64')
>>> y
Tensor(shape=[3, 4], dtype=float64, place=Place(cpu), stop_gradient=True,
[[0.   , 0.   , 0.   , 0.   ],
 [4.   , 20.  , 120. , 840. ],
 [8.   , 72.  , 720. , 7920.]])

>>> assert y.dtype == paddle.float64