張量流math.add_n()
方法將所有通過的張量逐元素相加。該操作在a和b的表示上完成。
此方法屬於math模塊。
用法:tf.math.add_n(inputs, name=None)
爭論
- 輸入:它指定tf.Tensor或tf.IndexedSlices對象的列表,並且每個對象的形狀和類型必須相同。 tf.IndexedSlices對象在應用方法之前會自動轉換為密集張量。
- 名稱:這是可選參數,這是操作的名稱。
返回:它返回一個Tensor,其形狀和類型與傳遞的輸入的元素相同。
注意:此方法執行與tf.math.accumulate_n相同的操作,但是此方法在開始求和之前會等待輸入準備就緒。因此,當輸入可能未同時準備好時,這種緩衝導致更多的內存消耗。
讓我們借助幾個示例來了解這個概念:
範例1:
範例1:
# Importing the Tensorflow library
import tensorflow as tf
# A constant a and b
a = tf.constant([[1, 3], [2, 8]])
b = tf.constant([[2, 1], [6, 7]])
# Applying the math.add_n() function
# storing the result in 'c'
c = tf.math.add_n([a, b])
# Initiating a Tensorflow session
with tf.Session() as sess:
print("Input 1", a)
print(sess.run(a))
print("Input 2", b)
print(sess.run(b))
print("Output:", c)
輸出:
Input 1 Tensor("Const_99:0", shape=(2, 2), dtype=int32) [[1 3] [2 8]] Input 2 Tensor("Const_100:0", shape=(2, 2), dtype=int32) [[2 1] [6 7]] Output: Tensor("AddN:0", shape=(2, 2), dtype=int32) [[ 3 4] [ 8 15]]
範例2:
# Importing the Tensorflow library
import tensorflow as tf
# A constant a and b
a = tf.constant([[1, 1], [2, 6]])
b = tf.constant([[5, 1], [8, 7]])
# Applying the math.add_n() function
# storing the result in 'c'
c = tf.math.add_n([a, b], name = "Add_N")
# Initiating a Tensorflow session
with tf.Session() as sess:
print("Input 1", a)
print(sess.run(a))
print("Input 2", b)
print(sess.run(b))
print("Output:", c)
print(sess.run(c))
輸出:
Input 1 Tensor("Const_101:0", shape=(2, 2), dtype=int32) [[1 1] [2 6]] Input 2 Tensor("Const_102:0", shape=(2, 2), dtype=int32) [[5 1] [8 7]] Output: Tensor("Add_N:0", shape=(2, 2), dtype=int32) [[ 6 2] [10 13]]
相關用法
- Python Tensorflow log()用法及代碼示例
- Python Tensorflow cos()用法及代碼示例
- Python Tensorflow exp()用法及代碼示例
- Python Tensorflow tan()用法及代碼示例
- Python Tensorflow sin()用法及代碼示例
- Python Tensorflow abs()用法及代碼示例
- Python Tensorflow logical_or()用法及代碼示例
- Python Tensorflow logical_xor()用法及代碼示例
- Python Tensorflow cosh()用法及代碼示例
- Python Tensorflow sinh()用法及代碼示例
- Python Tensorflow asin()用法及代碼示例
- Python Tensorflow atan()用法及代碼示例
- Python Tensorflow acos()用法及代碼示例
- Python Tensorflow acosh()用法及代碼示例
- Python Tensorflow logical_and()用法及代碼示例
- Python Tensorflow asinh()用法及代碼示例
- Python Tensorflow atanh()用法及代碼示例
- Python Tensorflow reciprocal()用法及代碼示例
- Python Tensorflow logical_not()用法及代碼示例
注:本文由純淨天空篩選整理自PranchalKatiyar大神的英文原創作品 Python – Tensorflow math.add_n() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。