當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python tensorflow.math.cumsum()用法及代碼示例

TensorFlow是Google設計的開源Python庫,用於開發機器學習模型和深度學習神經網絡。 cumsum()用於計算輸入張量的累積和。

用法:tensorflow.math.cumsum(x, axis, exclusive, reverse, name)

參數:

  • x:它是輸入張量。此張量允許的dtype為float32,float64,int64,int32,uint8,uint16,int16,int8,complex64,complex128,qint8,quint8,qint32,一半。
  • axis(optional):這是int32類型的張量。該值應在int32類型的Tensor範圍內(默認值:0)。必須在[-rank(x),rank(x))範圍內。預設值為0。
  • exclusive(optional):它是布爾型的。默認值為False,如果設置為true,則輸入[a,b,c]的輸出將為[0,a,a + b]。
  • reverse(optional):它是布爾型的。默認值為False,如果設置為true,則輸入[a,b,c]的輸出將為[a + b + c,a + b,a]。
  • name(optional):它定義了操作的名稱。

返回值:它返回與x相同dtype的張量。

範例1:



Python3

# imporing the library 
import tensorflow as tf 
  
# initializing the input 
a = tf.constant([1, 2, 4, 5], dtype = tf.int32)   
  
# Printing the input 
print("Input:",a) 
  
# Cumulative sum 
res  = tf.math.cumsum(a) 
  
# Printing the result 
print("Output:",res)

輸出:

Input: tf.Tensor([1 2 4 5], shape=(4,), dtype=int32)
Output: tf.Tensor([ 1  3  7 12], shape=(4,), dtype=int32)

範例2:在此示例中,reverse和Exclusive都設置為True。

Python3

# imporing the library 
import tensorflow as tf 
  
# initializing the input 
a = tf.constant([2, 3, 4, 5], dtype = tf.int32)   
  
# Printing the input 
print("Input:",a) 
  
# Cumulative sum 
res  = tf.math.cumsum(a, reverse = True, exclusive = True) 
  
# Printing the result 
print("Output:",res)

輸出:

Input: tf.Tensor([2 3 4 5], shape=(4,), dtype=int32)
Output: tf.Tensor([12  9  5  0], shape=(4,), dtype=int32)




相關用法


注:本文由純淨天空篩選整理自aman neekhara大神的英文原創作品 Python – tensorflow.math.cumsum()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。