首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将占位符添加到层中

将占位符添加到层中
EN

Stack Overflow用户
提问于 2018-05-29 09:37:08
回答 1查看 609关注 0票数 0

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

现在,我想将节点3和4连接到先前计算层的节点1和2。我知道这里有tf.concat,但我不知道如何正确地做到这一点。

如何将与原始净输入相同批次大小的占位符添加到特定层?

编辑:

当我在axis=1上使用axis=1时,最终会遇到以下问题:

代码语言:javascript
运行
复制
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]

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-29 10:25:55

由于您的数据是批处理的,可能是在第一个维度上,所以您需要连接到第二个维度(axis=1):

代码语言:javascript
运行
复制
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))}))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50581074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档