2017-06-22 57 views
3

我希望有人能夠解釋Keras中的輸入層和Tensorflow中的佔位符之間的區別(如果有的話)?Keras輸入層和Tensorflow佔位符之間的區別

我調查的越多,兩者似乎越相似,但我至今沒有100%的說服力。

這裏是我所贊成要求的觀察到,輸入層和tf佔位符是相同的:)

1)張量從keras.Input(返回可用於像在一個佔位符tf.Session的運行方法的feed_dict。下面是使用Keras,其將兩個張量(a和b)並連接的結果與第三張量(C)一個簡單的例子的一部分:

model = create_graph() 

con_cat = model.output[0] 
ab_add = model.output[1] 

# These values are used equivalently to tf.Placeholder() below 
mdl_in_a = model.input[0] 
mdl_in_b = model.input[1] 
mdl_in_c = model.input[2] 

sess = k.backend.get_session() 


a_in = rand_array() # 2x2 numpy arrays 
b_in = rand_array() 
c_in = rand_array() 
a_in = np.reshape(a_in, (1,2,2)) 
b_in = np.reshape(b_in, (1,2,2)) 
c_in = np.reshape(c_in, (1,2,2)) 

val_cat, val_add = sess.run([con_cat, ab_add], 
       feed_dict={ mdl_in_a: a_in, mdl_in_b: b_in, mdl_in_c: c_in}) 

2)從Tensorflow的Contrib該文檔有關Keras Input Layer提到的佔位符在其參數描述:

「稀疏:一個布爾指定佔位符 是否要創建是稀疏」

這裏是WH在我看到贊成輸入層和tf佔位符不相同的說法:

1)我看到人們利用tf.Placeholder而不是輸入層的返回張量。類似於:

a_holder = tf.placeholder(tf.float32, shape=(None, 2,2)) 
b_holder = tf.placeholder(tf.float32, shape=(None, 2,2)) 
c_holder = tf.placeholder(tf.float32, shape=(None, 2,2)) 

model = create_graph() 

con_cat, ab_add = model([a_holder, b_holder, c_holder]) 


sess = k.backend.get_session() 


a_in = rand_array() # 2x2 numpy arrays 
b_in = rand_array() 
c_in = rand_array() 
a_in = np.reshape(a_in, (1,2,2)) 
b_in = np.reshape(b_in, (1,2,2)) 
c_in = np.reshape(c_in, (1,2,2)) 


val_cat, val_add = sess.run([con_cat, ab_add], 
       feed_dict={ a_holder: a_in, b_holder: b_in, c_holder: c_in}) 

回答

1

Input()返回創建的佔位符的句柄,並且不創建其他tf操作符;張量代表操作輸出和佔位符,所以不存在矛盾。

分析究竟是由輸入()創建讓我們運行下面的代碼:

with tf.name_scope("INPUT_LAYER"): 
input_l = Input(shape = [n_features]) 

然後:

writer = tf.summary.FileWriter('./my_graph', tf.get_default_graph()) 
writer.close() 

而且從控制檯啓動Tensorboard:

tensorboard --logdir="./my_graph" 

看結果如下: enter image description here

相關問題