詳解tf.nn.bias_add和tf.add、tf.add_n的區別
阿新 • • 發佈:2018-12-15
tf.add(x,y,name=None)
x
:a tensor musut be one of the following types: half
, float32
, float64
, uint8
, int8
, int16
, int32
, int64
, complex64
, complex128
, string
.
y
: A Tensor
. Must have the same type as x
.
name
: A name for the operation (optional).
import tensorflow as tf a=tf.constant([[1,1],[2,2],[3,3]],dtype=tf.float32) b=tf.constant([1,-1],dtype=tf.float32) c=tf.constant([1],dtype=tf.float32) with tf.Session() as sess: print('bias_add:') print(sess.run(tf.nn.bias_add(a, b))) #執行下面語句錯誤 #print(sess.run(tf.nn.bias_add(a, c))) print('add:') print(sess.run(tf.add(a, c)))
輸出結果:
bias_add: [[ 2. 0.] [ 3. 1.] [ 4. 2.]] add: [[ 2. 2.] [ 3. 3.]
[ 4. 4.]]
tf.add_n(inputs,name=None)
函式是實現一個列表的元素的相加。就是輸入的物件是一個列表,列表裡的元素可以是向量,矩陣等但沒有廣播功能
例子:
import tensorflow as tf; import numpy as np; input1 = tf.constant([1.0, 2.0, 3.0]) input2 = tf.Variable(tf.random_uniform([3])) output = tf.add_n([input1, input2]) with tf.Session() as sess: sess.run(tf.initialize_all_variables()) print (sess.run(input1 + input2)) print (sess.run(output))
輸出:
[ 1.30945706 2.29760814 3.81558323] [ 1.30945706 2.29760814 3.81558323]