Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate two tensors horizontally in TensorFlow?

Tags:

tensorflow

I have 2 tensors of shape (100, 4) and (100, 2). I would like to perform a concatenation operation, in TensorFlow, similar to np.hstack, in NumPy, so that the output would be of shape (100, 6). Is there a TensorFlow function to do that?

like image 901
Mohan Rayapuvari Avatar asked Apr 13 '17 01:04

Mohan Rayapuvari


1 Answers

You can use tf.concat for this purpose as follows:

sess=tf.Session()
t1 = [[1, 2], [4, 5]]
t2 = [[7, 8, 9], [10, 11, 12]]
res=tf.concat(concat_dim=1,values=[t1, t2])
print(res.eval(session=sess))

This prints

[[ 1  2  7  8  9]
 [ 4  5 10 11 12]]
like image 77
Miriam Farber Avatar answered Oct 31 '22 19:10

Miriam Farber