mindspore.ops.cumsum

mindspore.ops.cumsum(x, axis, dtype=None)[source]

Computes the cumulative sum of input Tensor along axis.

\[y_i = x_1 + x_2 + x_3 + ... + x_i\]

Note

On Ascend, the dtype of x only support :int8, uint8, int32, float16 or float32 in case of static shape. For the case of dynamic shape, the dtype of x only support int32, float16 or float32.

Parameters
  • x (Tensor) – The input Tensor to accumulate.

  • axis (int) – Axis along which the cumulative sum is computed.

  • dtype (mindspore.dtype, optional) – The desired dtype of returned Tensor. If specified, the input Tensor will be cast to dtype before the computation. This is useful for preventing overflows. If not specified, stay the same as original Tensor. Default: None.

Returns

Tensor, the shape of the output Tensor is consistent with the input Tensor’s.

Raises
Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor
>>> import mindspore.ops as ops
>>> x = Tensor(np.array([[3, 4, 6, 10], [1, 6, 7, 9], [4, 3, 8, 7], [1, 3, 7, 9]]).astype(np.float32))
>>> # case 1: along the axis 0
>>> y = ops.cumsum(x, 0)
>>> print(y)
[[ 3.  4.  6. 10.]
 [ 4. 10. 13. 19.]
 [ 8. 13. 21. 26.]
 [ 9. 16. 28. 35.]]
>>> # case 2: along the axis 1
>>> y = ops.cumsum(x, 1)
>>> print(y)
[[ 3.  7. 13. 23.]
 [ 1.  7. 14. 23.]
 [ 4.  7. 15. 22.]
 [ 1.  4. 11. 20.]]