cumsum

paddle. cumsum ( x, axis=None, dtype=None, name=None ) [source]

The cumulative sum of the elements along a given axis.

Note

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

Parameters
  • x (Tensor) – The input tensor needed to be cumsumed.

  • axis (int, optional) – The dimension to accumulate along. -1 means the last dimension. The default (None) is to compute the cumsum over the flattened array.

  • dtype (str, optional) – The data type of the output tensor, can be float16, float32, float64, int32, int64. 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 cumsum operator.

Examples

>>> import paddle

>>> data = paddle.arange(12)
>>> data = paddle.reshape(data, (3, 4))

>>> y = paddle.cumsum(data)
>>> y
Tensor(shape=[12], dtype=int64, place=Place(cpu), stop_gradient=True,
[0 , 1 , 3 , 6 , 10, 15, 21, 28, 36, 45, 55, 66])

>>> y = paddle.cumsum(data, axis=0)
>>> y
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
[[0 , 1 , 2 , 3 ],
 [4 , 6 , 8 , 10],
 [12, 15, 18, 21]])

>>> y = paddle.cumsum(data, axis=-1)
>>> y
Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
[[0 , 1 , 3 , 6 ],
 [4 , 9 , 15, 22],
 [8 , 17, 27, 38]])

>>> y = paddle.cumsum(data, dtype='float64')
>>> assert y.dtype == paddle.float64