Reputation: 3504
This is rather a simple question. I've a tensor in 3D shape. Lets say it is [2,2,3]
. I've another tensor in 2D shape [2,2]
. But the first 2 dimensions of both the elements are matching. I want to concat
them such that the 2D tensor is added to the 3D tensor in the third dimension. i.e
[2,2,4]
.
I am not sure how to achieve this. I tried using tf.concat
and tf.stack
but that requires both the tensors to be the same rank.
Upvotes: 2
Views: 3314
Reputation: 126154
You can use tf.expand_dims()
to convert the smaller tensor to the correct rank, followed by tf.concat()
:
tensor_3d = tf.placeholder(tf.float32, shape=[2, 2, 3])
tensor_2d = tf.placeholder(tf.float32, shape=[2, 2])
tensor_2d_as_3d = tf.expand_dims(tensor_2d, 2) # shape: [2, 2, 1]
result = tf.concat([tensor_3d, tensor_2d_as_3d], 2) # shape: [2, 2, 4]
Upvotes: 7