計算張量 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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
