bear
bear

Reputation: 663

Concatenating tensors in Tensorflow

I want to concatenate a variable and a tensor in Tensorflow, but Tensorflow wouldn't let those two types concatenate.

Here's how I concatenate the two tensors:

self.embedded_chars = tf.nn.embedding_lookup(self.W, self.input_x) //returns Tensor object
v1 = tf.Variable(tf.zeros([88,77]),dtype=tf.float32)
self.embedded_chars = tf.concat(1,[self.embedded_chars,v1])

But I am getting the following error:

File "test.py", line 93, in l2_reg_lambda=FLAGS.l2_reg_lambda) File "test.py", line 31, in init self.embedded_chars = tf.concat(1,[self.embedded_chars,v1]) File "lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 1047, in concat dtype=dtypes.int32).get_shape( File "lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 651, in convert_to_tensor as_ref=False) File "lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 716, in internal_convert_to_tensor ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref) File "lib/python2.7/site-packages/tensorflow/python/framework/constant_op.py", line 176, in _constant_tensor_conversion_function return constant(v, dtype=dtype, name=name) File "lib/python2.7/site-packages/tensorflow/python/framework/constant_op.py", line 165, in constant tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape)) File "lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.py", line 367, in make_tensor_proto _AssertCompatible(values, dtype) File "lib/python2.7/site-packages/tensorflow/python/framework/tensor_util.py", line 302, in _AssertCompatible (dtype.name, repr(mismatch), type(mismatch).name)) TypeError: Expected int32, got list containing Tensors of type '_Message' instead.

How can I concatenate the variable and tensor correctly?

Upvotes: 0

Views: 4565

Answers (1)

rmeertens
rmeertens

Reputation: 4451

Assuming you are using version 1.0: If you look at the documentation you see that concat (https://www.tensorflow.org/api_docs/python/tf/concat) wants to have values as the first argument, and axis as the second argument.

Your code should be:

self.embedded_chars = tf.nn.embedding_lookup(self.W, self.input_x) //returns Tensor object
v1 = tf.Variable(tf.zeros([88,77]),dtype=tf.float32)
self.embedded_chars = tf.concat([self.embedded_chars,v1],1)

As I did not test it, let me know if it works!

Cheers!

Upvotes: 1

Related Questions