cumsum
- paddle. cumsum ( x: Tensor, axis: int | None = None, dtype: DTypeLike | None = None, name: str | None = None, *, out: Tensor | None = None ) Tensor [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.
Note
Alias Support: The parameter name
input
can be used as an alias forx
, anddim
can be used as an alias foraxis
. For example,cumsum(input=tensor_x, dim=1, ...)
is equivalent tocumsum(x=tensor_x, axis=1, ...)
.- Parameters
-
x (Tensor) – The input tensor needed to be cumsumed. alias:
input
.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. alias:
dim
.dtype (str|paddle.dtype|np.dtype|None, optional) – The data type of the output tensor, can be bfloat16, float16, float32, float64, int32, int64, complex64, complex128. By default, it is int64 if the input x is int8/int16/int32; otherwise, it is None. If it is not None, the input tensor is casted to dtype before the operation is performed. This is useful for preventing data type overflows.
name (str|None, optional) – Name for the operation (optional, default is None). For more information, please refer to api_guide_Name.
out (Tensor, optional) – The output tensor. If provided, the result will be stored in this tensor.
- 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