mindspore.ops.CumSum

class mindspore.ops.CumSum(exclusive=False, reverse=False)[源代码]

计算输入Tensor在指定轴上的累加和。

\[y_i = x_1 + x_2 + x_3 + ... + x_i\]
参数:
  • exclusive (bool) - 表示输出结果的第一个元素是否与输入的第一个元素一致。如果为False,表示输出的第一个元素与输入的第一个元素一致。默认值:False。

  • reverse (bool) - 如果为True,则逆向计算累加和。默认值:False。

输入:
  • input (Tensor) - 输入要计算的Tensor。

  • axis (int) - 指定要累加和的轴。仅支持常量值。该值在[-rank(input), rank(input))范围中。

输出:

Tensor。输出Tensor的shape与输入Tensor的shape一致。

异常:
  • TypeError - exclusivereverse 不是bool。

  • TypeError - axis 不是int。

支持平台:

Ascend GPU CPU

样例:

>>> x = Tensor(np.array([[3, 4, 6, 10], [1, 6, 7, 9], [4, 3, 8, 7], [1, 3, 7, 9]]).astype(np.float32))
>>> cumsum = ops.CumSum()
>>> # case 1: along the axis 0
>>> y = 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 = cumsum(x, 1)
>>> print(y)
[[ 3.  7. 13. 23.]
 [ 1.  7. 14. 23.]
 [ 4.  7. 15. 22.]
 [ 1.  4. 11. 20.]]
>>> # Next demonstrate exclusive and reverse, along axis 1
>>> # case 3: exclusive = True
>>> cumsum = ops.CumSum(exclusive=True)
>>> y = cumsum(x, 1)
>>> print(y)
[[ 0.  3.  7. 13.]
 [ 0.  1.  7. 14.]
 [ 0.  4.  7. 15.]
 [ 0.  1.  4. 11.]]
>>> # case 4: reverse = True
>>> cumsum = ops.CumSum(reverse=True)
>>> y = cumsum(x, 1)
>>> print(y)
[[23. 20. 16. 10.]
 [23. 22. 16.  9.]
 [22. 18. 15.  7.]
 [20. 19. 16.  9.]]