2017-06-17 29 views
2

假設有兩個輸入Tensorflow一個簡單的神經網絡:Tensorflow:如何合理地合併兩個神經網絡層到一個

W0 = tf.Variable(tf.zeros([784, 100])) 
b0 = tf.Variable(tf.zeros([100])) 
h_a = tf.nn.relu(tf.matmul(x, W0) + b0) 

W1 = tf.Variable(tf.zeros([100, 10])) 
b1 = tf.Variable(tf.zeros([10])) 
h_b = tf.nn.relu(tf.matmul(z, W1) + b1) 

問:什麼將是這兩個圖層合併爲一個好辦法下一層?

我的意思是這樣的:

h_master = tf.nn.relu(tf.matmul(concat(h_a, h_b), W_master) + b_master)

不過,我似乎無法找到這個合適的功能。


編輯:請注意:如果我這樣做:

h_master = tf.nn.tanh(tf.matmul(np.concatenate((h_a,h_b)),W_master) + b_master)

我收到以下錯誤:

ValueError: zero-dimensional arrays cannot be concatenated 

(我的猜測是,這是因爲佔位符被numpy視爲空數組,因此h_a和h_b爲零維。)

回答

2

我找到了一種方法:

h_master = tf.nn.tanh(tf.matmul(tf.concat((h_a, h_b), axis=1), W_master) + b_master) 

其中:

W_master = tf.Variable(tf.random_uniform([110, 10], -0.01, 0.01)) 
b_master = tf.Variable(tf.zeros([10]))