mindspore.Tensor.cumsum

Tensor.cumsum(dim, *, dtype=None) Tensor

Computes the cumulative sum of self Tensor along dim.

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

dim (int) – Dim along which the cumulative sum is computed.

Keyword Arguments

dtype (mindspore.dtype, optional) – The desired dtype of returned Tensor. If specified, the self 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 self Tensor's.

Raises

ValueError – If the dim is out of range.

Supported Platforms:

Ascend

Examples

>>> import numpy as np
>>> from mindspore import Tensor
>>> 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 dim 0
>>> y = x.cumsum(dim=0)
>>> print(y)
[[ 3.  4.  6. 10.]
 [ 4. 10. 13. 19.]
 [ 8. 13. 21. 26.]
 [ 9. 16. 28. 35.]]
>>> # case 2: along the dim 1
>>> y = x.cumsum(dim=1)
>>> print(y)
[[ 3.  7. 13. 23.]
 [ 1.  7. 14. 23.]
 [ 4.  7. 15. 22.]
 [ 1.  4. 11. 20.]]
Tensor.cumsum(axis=None, dtype=None) Tensor

Computes the cumulative sum of self Tensor along axis.

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

Note

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

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

  • dtype (mindspore.dtype, optional) – The desired dtype of returned Tensor. If specified, the self 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 self Tensor's.

Raises

ValueError – If the axis is out of range.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor
>>> 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 = x.cumsum(axis=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 = x.cumsum(axis=1)
>>> print(y)
[[ 3.  7. 13. 23.]
 [ 1.  7. 14. 23.]
 [ 4.  7. 15. 22.]
 [ 1.  4. 11. 20.]]