我有一个有两个节点的Tensorflow层。这些是另外两个较大的隐藏层的输出节点。现在我想给这个层增加2个新的节点,所以我总共有4个节点,并做了一些最后的计算。到目前为止,添加的节点都是作为占位符实现的,并且根据批处理大小具有动态形状。下面是这个网络的草图:

现在,我想将节点3和4连接到先前计算层的节点1和2。我知道这里有tf.concat,但我不知道如何正确地做到这一点。
如何将与原始净输入相同批次大小的占位符添加到特定层?
编辑:
当我在axis=1上使用axis=1时,最终会遇到以下问题:
z = tf.placeholder(tf.float32, shape=[None, 2])
Weight_matrix = weight_variable([4, 2])
bias = bias_variable([4, 2])
concat = tf.concat((dnn_out, z), 1)
h_fc3 = tf.nn.relu(tf.matmul(concat, Weight_matrix) + bias)将偏差添加到tf.matmul结果将引发一个InvalidArgumentError: Incompatible shapes: [20,2] vs. [4,2]。
发布于 2018-05-29 10:25:55
由于您的数据是批处理的,可能是在第一个维度上,所以您需要连接到第二个维度(axis=1):
import tensorflow as tf
import numpy as np
dnn_output = tf.placeholder(tf.float32, (None, 2)) # replace with your DNN(input) result
additional_nodes = tf.placeholder(tf.float32, (None, 2))
concat = tf.concat((dnn_output, additional_nodes), axis=1)
print(concat)
# > Tensor("concat:0", shape=(?, 4), dtype=float32)
dense_output = tf.layers.dense(concat, units=2)
print(dense_output)
# > Tensor("dense/BiasAdd:0", shape=(?, 2), dtype=float32)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(dense_output, feed_dict={dnn_output: np.ones((5, 2)),
additional_nodes: np.zeros((5, 2))}))https://stackoverflow.com/questions/50581074
复制相似问题