2017-06-17 405 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]))