How to permutate tranposition in tensorflow?

alvas picture alvas · Jul 22, 2016 · Viewed 12.7k times · Source

From the docs:

Transposes a. Permutes the dimensions according to perm.

The returned tensor's dimension i will correspond to the input dimension perm[i]. If perm is not given, it is set to (n-1...0), where n is the rank of the input tensor. Hence by default, this operation performs a regular matrix transpose on 2-D input Tensors.

But it's still a little unclear to me how should I be slicing the input tensor. E.g. from the docs too:

tf.transpose(x, perm=[0, 2, 1]) ==> [[[1  4]
                                      [2  5]
                                      [3  6]]

                                     [[7 10]
                                      [8 11]
                                      [9 12]]]

Why is it that perm=[0,2,1] produces a 1x3x2 tensor?

After some trial and error:

twothreefour = np.array([ [[1,2,3,4], [5,6,7,8], [9,10,11,12]] , 
                        [[13,14,15,16], [17,18,19,20], [21,22,23,24]] ])
twothreefour

[out]:

array([[[ 1,  2,  3,  4],
        [ 5,  6,  7,  8],
        [ 9, 10, 11, 12]],

       [[13, 14, 15, 16],
        [17, 18, 19, 20],
        [21, 22, 23, 24]]])

And if I transpose it:

fourthreetwo = tf.transpose(twothreefour) 
with tf.Session() as sess:
    init = tf.initialize_all_variables()
    sess.run(init)
    print (fourthreetwo.eval())

I get a 4x3x2 to a 2x3x4 and that sounds logical.

[out]:

[[[ 1 13]
  [ 5 17]
  [ 9 21]]

 [[ 2 14]
  [ 6 18]
  [10 22]]

 [[ 3 15]
  [ 7 19]
  [11 23]]

 [[ 4 16]
  [ 8 20]
  [12 24]]]

But when I use the perm parameter the output, I'm not sure what I'm really getting:

twofourthree = tf.transpose(twothreefour, perm=[0,2,1]) 
with tf.Session() as sess:
    init = tf.initialize_all_variables()
    sess.run(init)
    print (threetwofour.eval())

[out]:

[[[ 1  5  9]
  [ 2  6 10]
  [ 3  7 11]
  [ 4  8 12]]

 [[13 17 21]
  [14 18 22]
  [15 19 23]
  [16 20 24]]]

Why does perm=[0,2,1] returns a 2x4x3 matrix from a 2x3x4 ?

Trying it again with perm=[1,0,2]:

threetwofour = tf.transpose(twothreefour, perm=[1,0,2]) 
with tf.Session() as sess:
    init = tf.initialize_all_variables()
    sess.run(init)
    print (threetwofour.eval())

[out]:

[[[ 1  2  3  4]
  [13 14 15 16]]

 [[ 5  6  7  8]
  [17 18 19 20]]

 [[ 9 10 11 12]
  [21 22 23 24]]]

Why does perm=[1,0,2] return a 3x2x4 from a 2x3x4?

Does it mean that the perm parameter is taking my np.shape and transposing the tensor based on the elements based on my array shape?

I.e. :

_size = (2, 4, 3, 5)
randarray = np.random.randint(5, size=_size)

shape_idx = {i:_s for i, _s in enumerate(_size)}

randarray_t_func = tf.transpose(randarray, perm=[3,0,2,1]) 
with tf.Session() as sess:
    init = tf.initialize_all_variables()
    sess.run(init)
    tranposed_array = randarray_t_func.eval()
    print (tranposed_array.shape)

print (tuple(shape_idx[_s] for _s in [3,0,2,1]))

[out]:

(5, 2, 3, 4)
(5, 2, 3, 4)

Answer

maxymoo picture maxymoo · Jul 22, 2016

I think perm is permuting the dimensions. For example perm=[0,2,1] is short for dim_0 -> dim_0, dim_1 -> dim_2, dim_2 -> dim_1. So for a 2D tensor, perm=[1,0] is just matrix transpose. Does this answer your question?