📜  Python – TensorFlow math.add() 方法

📅  最后修改于: 2022-05-13 01:54:47.681000             🧑  作者: Mango

Python – TensorFlow math.add() 方法

Tensorflow math.add()方法返回 pass 输入的 a + b。该操作是在 a 和 b 的表示上完成的。该方法属于数学模块。

让我们通过几个例子来看看这个概念:
示例 1:
Python3
# 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))


Python3
# 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_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:

Python3

# 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'