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


Python Tensorflow math.add()用法及代碼示例

張量流math.add()方法返回通過輸入的a + b。該操作在a和b的表示上完成。此方法屬於math模塊。

用法:tf.math.add(a, b, name=None)

爭論

  • a:此參數應為Tensor,也應來自以下類型之一:bfloat16,half,float32,float64,uint8,int8,int16,int32,int64,complex64,complex128,字符串
  • b:這也應該是張量,並且必須與a具有相同的類型。
  • 名稱:這是可選參數,這是操作的名稱。

返回:它返回具有與輸入a相同形狀的張量。

讓我們借助幾個示例來了解這個概念:
範例1:

# Importing the Tensorflow library  
import tensorflow as tf  
  
# A constant a and b 
a = tf.constant(3) 
b = tf.constant(6)   
  
# Applying the math.add() function  
# storing the result in 'c'  
c = tf.math.add(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) 
    print(sess.run(c))

輸出:

Input 1 Tensor("Const_79:0", shape=(), dtype=int32)
3
Input 2 Tensor("Const_80:0", shape=(), dtype=int32)
6
Output: Tensor("Add_1:0", shape=(), dtype=int32)
9

範例2:

# Importing the Tensorflow library  
import tensorflow as tf  
  
# A constant a and b 
a = tf.constant(u"This is ") 
b = tf.constant(u"GeeksForGeeks")   
  
# Applying the math.add() function  
# storing the result in 'c'  
c = tf.math.add(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) 
    print(sess.run(c))

輸出:

Input 1 Tensor("Const_87:0", shape=(), dtype=string)
b'This is '
Input 2 Tensor("Const_88:0", shape=(), dtype=string)
b'GeeksForGeeks'
Output: Tensor("Add_5:0", shape=(), dtype=string)
b'This is GeeksForGeeks'



相關用法


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