Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a list of tensors of dim N to a tensor of dim N+1

Tags:

tensorflow

I need to convert a list of tensors of dimensionality N to a new tensor with dimensionality N+1 so that the new dimension would be the right most dimension.

For example if x and y would be tensors of shape (4,3) both then I am trying to create a new tensor z of shape (4,3,2) by forming z and setting tensor x as the 0th element along the third dimension and setting tensor y as the 1st element along the third dimension. In pseudocode:

z = tf.fromList([x,y],3)

What's the best way to do that in Tensorflow. I was unable to figure it out from the documentation of TF 0.7.1.

like image 686
Tomi Avatar asked Mar 01 '16 18:03

Tomi


2 Answers

If I'm reading you correctly, you want to interleave the data of the two tensors.

You want to tf.pack() them together, which would form a tensor of shape [2, 4, 3] and then tf.transpose([1, 2, 0]) that resulting tensor to get to the interleaving you want.

like image 157
dga Avatar answered Oct 20 '22 12:10

dga


dga's method works, but tf.pack() has been removed from TensorFlow V1.0 onwards.

You can use tf.stack() to achieve the same.

Docs: https://www.tensorflow.org/api_docs/python/tf/stack

like image 44
Joseph Avatar answered Oct 20 '22 12:10

Joseph