简体   繁体   中英

Concatenate 3d tensors by cloning one tensor?

I have two tensors:

a = tf.placeholder(tf.float32, [None, 20, 100])
b = tf.placeholder(tf.float32, [None, 1, 100])

I want to append b to a[i, 20, 100] , to create c such as c has a shape of [None, 20, 200] .

This seems rather simple but I haven't figured out how to do this with tf.concat :

tf.concat(0, [a, b]) -> Shapes (20, 100) and (1, 100) are not compatible
tf.concat(1, [a, b]) => shape=(?, 28, 100) which is not what I wanted
tf.concat(2, [a, b]) -> Shapes (?, 20) and (?, 1) are not compatible

Do I need to reshape a and b first then concat?

This can be done using tf.tile . You will need to clone the tensor along dimension 1, 20 times to make it compatible with a . Then a simple concatenation along dimension 2 will give you the result.

Here is the complete code,

import tensorflow as tf

a = tf.placeholder(tf.float32, [None, 20, 100])
b = tf.placeholder(tf.float32, [None, 1, 100])
c = tf.tile(b, [1,20,1])
print c.get_shape()
# Output - (?, 20, 100)
d = tf.concat(2, [a,c])
print d.get_shape()
# Output - (?, 20, 200)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM