1

I have a tensor (shape=[batchsize]). I want to reshape the tensor in a specific order and into shape=[-1,2]. I want to have that specific order:

  1. Element at [0,0]
  2. Element at [1,1]
  3. Element at [0,1]
  4. Element at [1,0]
  5. Element at [2,0]
  6. Element at [3,1]
  7. Element at [2,1]
  8. Element at [3,0] and so on for an unknow batchsize.

Here is an example code with a tensor range=(0 to input=8).

import tensorflow as tf
import numpy as np

batchsize = tf.placeholder(shape=[], dtype=tf.int32)
x = tf.range(0, batchsize, 1) 
x = tf.reshape(x, shape=[2, -1])
y = tf.transpose(x)
z = tf.reshape(y, shape=[-1, 2])

input = 8
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    msg = sess.run([z], feed_dict={batchsize: input})
    print(msg)

Now my output is:

[array([[0, 4],
       [1, 5],
       [2, 6],
       [3, 7]], dtype=int32)]

But I want the output to be:

[array([[0, 2],
       [3, 1],
       [4, 6],
       [7, 5]], dtype=int32)]

In general: The important thing is that, looking only at the transformation of a 4x1 block to a 2x2 block, the first 2 elements of the input tensor are on one diagonal and the remaining 2 elements on the counter diagonal.

Keep in mind I do not know how big batchsize is, I just set input=8 for exemplary reason. In my real code the tensor ´x´ is no range array but complex random numbers, so you cannot sort in any way w.r.t. the values. I just made this code for a demonstration purpose.

This question is related to this question but with different order or this question, after doing the normal reshape with transpose just to swap elements every 2nd row.

yoboseyo
  • 13
  • 3

0 Answers0