计算张量 x 沿 axis 的累积和。
用法
tf.math.cumsum(
x, axis=0, exclusive=False, reverse=False, name=None
)参数
-
x一个Tensor。必须是以下类型之一:float32,float64,int64,int32,uint8,uint16,int16,int8,complex64,complex128,qint8,quint8,qint32,half。 -
axisint32类型的Tensor(默认值:0)。必须在[-rank(x), rank(x))范围内。 -
exclusive如果True,执行独占cumsum。 -
reversebool(默认值:False)。 -
name操作的名称(可选)。
返回
-
一个
Tensor。具有与x相同的类型。
默认情况下,此操作执行包含 cumsum,这意味着输入的第一个元素与输出的第一个元素相同:例如:
# tf.cumsum([a, b, c]) # [a, a + b, a + b + c]
x = tf.constant([2, 4, 6, 8])
tf.cumsum(x)
<tf.Tensor:shape=(4,), dtype=int32,
numpy=array([ 2, 6, 12, 20], dtype=int32)>
# using varying `axis` values
y = tf.constant([[2, 4, 6, 8], [1,3,5,7]])
tf.cumsum(y, axis=0)
<tf.Tensor:shape=(2, 4), dtype=int32, numpy=
array([[ 2, 4, 6, 8],
[ 3, 7, 11, 15]], dtype=int32)>
tf.cumsum(y, axis=1)
<tf.Tensor:shape=(2, 4), dtype=int32, numpy=
array([[ 2, 6, 12, 20],
[ 1, 4, 9, 16]], dtype=int32)>
通过将 exclusive kwarg 设置为 True ,将执行独占 cumsum:
# tf.cumsum([a, b, c], exclusive=True) => [0, a, a + b]
x = tf.constant([2, 4, 6, 8])
tf.cumsum(x, exclusive=True)
<tf.Tensor:shape=(4,), dtype=int32,
numpy=array([ 0, 2, 6, 12], dtype=int32)>
通过将 reverse kwarg 设置为 True ,cumsum 以相反的方向执行:
# tf.cumsum([a, b, c], reverse=True) # [a + b + c, b + c, c]
x = tf.constant([2, 4, 6, 8])
tf.cumsum(x, reverse=True)
<tf.Tensor:shape=(4,), dtype=int32,
numpy=array([20, 18, 14, 8], dtype=int32)>
这比使用单独的 tf.reverse 操作更有效。 reverse 和 exclusive kwargs 也可以组合:
# tf.cumsum([a, b, c], exclusive=True, reverse=True) # [b + c, c, 0]
x = tf.constant([2, 4, 6, 8])
tf.cumsum(x, exclusive=True, reverse=True)
<tf.Tensor:shape=(4,), dtype=int32,
numpy=array([18, 14, 8, 0], dtype=int32)>
相关用法
- Python tf.math.cumulative_logsumexp用法及代码示例
- Python tf.math.cumprod用法及代码示例
- Python tf.math.conj用法及代码示例
- Python tf.math.count_nonzero用法及代码示例
- Python tf.math.confusion_matrix用法及代码示例
- Python tf.math.ceil用法及代码示例
- Python tf.math.cos用法及代码示例
- Python tf.math.cosh用法及代码示例
- Python tf.math.special.fresnel_cos用法及代码示例
- Python tf.math.polyval用法及代码示例
- Python tf.math.is_finite用法及代码示例
- Python tf.math.special.bessel_k0e用法及代码示例
- Python tf.math.acosh用法及代码示例
- Python tf.math.invert_permutation用法及代码示例
- Python tf.math.segment_prod用法及代码示例
- Python tf.math.bincount用法及代码示例
- Python tf.math.bessel_i0e用法及代码示例
- Python tf.math.unsorted_segment_min用法及代码示例
- Python tf.math.scalar_mul用法及代码示例
- Python tf.math.zero_fraction用法及代码示例
注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.math.cumsum。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
