mindspore.mint.cumsum

mindspore.mint.cumsum(input, dim, dtype=None)[源代码]

计算输入Tensor input 沿轴 dim 的累积和。

\[y_i = x_1 + x_2 + x_3 + ... + x_i\]
参数:
  • input (Tensor) - 输入Tensor。

  • dim (int) - 累积和计算的轴。

  • dtype (mindspore.dtype, 可选) - 输出数据类型。如果不为None,则输入会转化为 dtype。这有利于防止数值溢出。如果为None,则输出和输入的数据类型一致。默认值: None

返回:

Tensor,和输入Tensor的shape相同。

异常:
  • TypeError - 如果 input 不是Tensor。

  • ValueError - 如果 dim 超出范围。

支持平台:

Ascend

样例:

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