James
James

Reputation: 4052

Tensorflow Concat Error: shape mismatch

I currently have a network whereby I start with a 16 x 16 x 2 input tensor, I perform a few convolution and pooling operations and reduce that down to a tensor that is declared like this:

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

That tensor then passes through a couple more layers of matrix multiplications and relus before outputting a category.

What I would like to do is extend the output of convolution stage by adding another 10 parameters to the vector above.

I have a placeholder where the data is loaded in which is defined like this:

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

I'm trying to concatenate these variables together like this:

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

I'm getting the following error message:

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

I'm sure that there is something simple that I'm doing wrong but I can't see it.

Upvotes: 0

Views: 5040

Answers (3)

HenryZhao
HenryZhao

Reputation: 831

The reason is likely in the tensorflow version.

From the tensorflow latest official api, tf.conat is defined as

tf.concat

concat( values, axis, name='concat' )

So, the better way is calling this function by key value. I tried the following code, no error.

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

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

Copy the offical api explanation as follows.

tf.concat concat( values, axis, name='concat' )

Defined in tensorflow/python/ops/array_ops.py.

See the guide: Tensor Transformations > Slicing and Joining

Concatenates tensors along one dimension.

Concatenates the list of tensors values along dimension axis. If values[i].shape = [D0, D1, ... Daxis(i), ...Dn], the concatenated result has shape

[D0, D1, ... Raxis, ...Dn] where

Raxis = sum(Daxis(i)) That is, the data from the input tensors is joined along the axis dimension.

The number of dimensions of the input tensors must match, and all dimensions except axis must be equal.

For example:

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]

Upvotes: 3

Sergii Gryshkevych
Sergii Gryshkevych

Reputation: 4159

x1 and x2 have different ranks, 1 and 2 respectively, so nothing strange that concat fails. Here is an example that works for me:

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

Upvotes: 3

Vincent Renkens
Vincent Renkens

Reputation: 211

I don't really understand why you have the None in the shape of the placeholder. If you remove it, it should work

Upvotes: 0

Related Questions