# 比较与tf.math.cumsum的功能差异 [![查看源文件](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/r1.8/resource/_static/logo_source.png)](https://gitee.com/mindspore/docs/blob/r1.8/docs/mindspore/source_zh_cn/note/api_mapping/tensorflow_diff/TensorCumsum.md) ## tf.math.cumsum ```python tf.math.cumsum(x, axis=0, exclusive=False, reverse=False, name=None) ``` 更多内容详见[tf.math.cumsum](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/math/cumsum)。 ## mindspore.Tensor.cumsum ```python mindspore.Tensor.cumsum(self, axis=None, dtype=None) ``` 更多内容详见[mindspore.Tensor.cumsum](https://mindspore.cn/docs/zh-CN/r1.8/api_python/mindspore/mindspore.Tensor.html#mindspore.Tensor.cumsum)。 ## 使用方式 > 在TensorFlow1.15版本中,tf.math.cumsum==tf.cumsum。 两接口基本功能相同,都是计算某个维度上Tensor的累加和(cumulative sum)。不同点在于,`tf.math.cumsum`多两个入参:`exclusive`用于指定是否包含当前值的累加,`reverse`用于指定返回值是否做逆转。 ## 代码示例 ```python import mindspore as ms import numpy as np a = ms.Tensor(np.ones((2,3)).astype("float32")) print(a.cumsum(axis=0)) # [[1. 1. 1.] # [2. 2. 2.]] import tensorflow as tf tf.enable_eager_execution() b = tf.constant(np.ones((2, 3))) print(tf.cumsum(b, axis=0)) # tf.Tensor( # [[1. 1. 1.] # [2. 2. 2.]], shape=(2, 3), dtype=float64) print(tf.cumsum(b, exclusive=True)) # tf.Tensor( # [[0. 0. 0.] # [1. 1. 1.]], shape=(2, 3), dtype=float64) print(tf.cumsum(b, reverse=True)) # tf.Tensor( # [[2. 2. 2.] # [1. 1. 1.]], shape=(2, 3), dtype=float64) ```