Tensorflow Concat错误:形状不匹配

时间:2016-09-02 12:08:51

标签: tensorflow

我目前有一个网络,我从一个16 x 16 x 2的输入张量开始,我执行一些卷积和池化操作,并将其减少到这样声明的张量:

 x1 = tf.Variable(tf.constant(1.0, shape=[32]))

然后张量在输出类别之前经过几层矩阵乘法和relus。

我想做的是通过在上面的向量中添加另外10个参数来扩展卷积阶段的输出。

我有一个占位符,其中加载了数据,其定义如下:

 x2 = tf.placeholder(tf.float32, [None,10])

我正在尝试将这些变量连接在一起,如下所示:

xnew = tf.concat(0,[x1,x2])

我收到以下错误消息:

ValueError: Shapes (32,) and (10,) are not compatible

我确信有一些简单的事我做错了,但我看不到它。

3 个答案:

答案 0 :(得分:3)

x1x2分别有不同的等级,1和2,所以concat失败并不奇怪。这是一个适合我的例子:

x1 = tf.Variable(tf.constant(1.0, shape=[32]))
# create a placeholder that will hold another 10 parameters
x2 = tf.placeholder(tf.float32, shape=[10])
# concatenate x1 and x2
xnew = tf.concat(0, [x1, x2])
init = tf.initialize_all_variables()
with tf.Session() as sess:
    sess.run(init)
    _xnew = sess.run([xnew], feed_dict={x2: range(10)})

enter image description here

答案 1 :(得分:2)

原因很可能是 tensorflow版本

从tensorflow最新的官方api,tf.conat被定义为

tf.concat

的concat(     值     轴,     命名= '的concat' )

因此,更好的方法是通过键值调用此函数。 我尝试了以下代码,没有错误。

 xnew = tf.concat(axis=0, values=[x1, x2])

----------------------------------------------- ---

复制官方api说明如下。

tf.concat CONCAT(     值     轴,     命名= '的concat' )

在tensorflow / python / ops / array_ops.py中定义。

参见指南:Tensor Transformations>切片和加入

沿一个维度连接张量。

沿尺寸轴连接张量值列表。如果值[i] .shape = [D0,D1,... Daxis(i),... Dn],则连接结果具有形状

[D0,D1,...... Raxis,... Dn] 其中

Raxis = sum(Da​​xis(i)) 也就是说,来自输入张量的数据沿着轴维度连接。

输入张量的尺寸必须匹配,除轴外的所有尺寸必须相等。

例如:

t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
tf.concat([t1, t2], 0) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
tf.concat([t1, t2], 1) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]

# tensor t3 with shape [2, 3]
# tensor t4 with shape [2, 3]
tf.shape(tf.concat([t3, t4], 0)) ==> [4, 3]
tf.shape(tf.concat([t3, t4], 1)) ==> [2, 6]

答案 2 :(得分:0)

我真的不明白你为什么在占位符的形状中有None。如果你删除它,它应该工作