title: How to permutate tranposition in tensorflow?
body: <p>From the <a href="https://www.tensorflow.org/api_docs/python/tf/transpose" rel="noreferrer">docs</a>:</p>
  
  <blockquote>
    <p>Transposes <code>a</code>. Permutes the dimensions according to perm.</p>
    
    <p>The returned tensor's dimension i will correspond to the input
    dimension <code>perm[i]</code>. If <code>perm</code> 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.</p>
  </blockquote>
  
  <p>But it's still a little unclear to me how should I be slicing the input tensor. E.g. from the docs too:</p>
  
  <pre><code>tf.transpose(x, perm=[0, 2, 1]) ==&gt; [[[1  4]
                                        [2  5]
                                        [3  6]]
  
                                       [[7 10]
                                        [8 11]
                                        [9 12]]]
  </code></pre>
  
  <p><strong>Why is it that <code>perm=[0,2,1]</code> produces a 1x3x2 tensor?</strong></p>
  
  <p>After some trial and error:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>[out]:</p>
  
  <pre><code>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]]])
  </code></pre>
  
  <p>And if I transpose it:</p>
  
  <pre><code>fourthreetwo = tf.transpose(twothreefour) 
  with tf.Session() as sess:
      init = tf.initialize_all_variables()
      sess.run(init)
      print (fourthreetwo.eval())
  </code></pre>
  
  <p>I get a 4x3x2 to a 2x3x4 and that sounds logical.</p>
  
  <p>[out]:</p>
  
  <pre><code>[[[ 1 13]
    [ 5 17]
    [ 9 21]]
  
   [[ 2 14]
    [ 6 18]
    [10 22]]
  
   [[ 3 15]
    [ 7 19]
    [11 23]]
  
   [[ 4 16]
    [ 8 20]
    [12 24]]]
  </code></pre>
  
  <p>But when I use the <code>perm</code> parameter the output, I'm not sure what I'm really getting:</p>
  
  <pre><code>twofourthree = tf.transpose(twothreefour, perm=[0,2,1]) 
  with tf.Session() as sess:
      init = tf.initialize_all_variables()
      sess.run(init)
      print (threetwofour.eval())
  </code></pre>
  
  <p>[out]:</p>
  
  <pre><code>[[[ 1  5  9]
    [ 2  6 10]
    [ 3  7 11]
    [ 4  8 12]]
  
   [[13 17 21]
    [14 18 22]
    [15 19 23]
    [16 20 24]]]
  </code></pre>
  
  <p><strong>Why does <code>perm=[0,2,1]</code> returns a 2x4x3 matrix from a 2x3x4 ?</strong></p>
  
  <p>Trying it again with <code>perm=[1,0,2]</code>:</p>
  
  <pre><code>threetwofour = tf.transpose(twothreefour, perm=[1,0,2]) 
  with tf.Session() as sess:
      init = tf.initialize_all_variables()
      sess.run(init)
      print (threetwofour.eval())
  </code></pre>
  
  <p>[out]:</p>
  
  <pre><code>[[[ 1  2  3  4]
    [13 14 15 16]]
  
   [[ 5  6  7  8]
    [17 18 19 20]]
  
   [[ 9 10 11 12]
    [21 22 23 24]]]
  </code></pre>
  
  <p><strong>Why does <code>perm=[1,0,2]</code> return a 3x2x4 from a 2x3x4?</strong></p>
  
  <p><strong>Does it mean that the <code>perm</code> parameter is taking my <code>np.shape</code> and transposing the tensor based on the elements based on my array shape?</strong></p>
  
  <p>I.e. :</p>
  
  <pre><code>_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]))
  </code></pre>
  
  <p>[out]:</p>
  
  <pre><code>(5, 2, 3, 4)
  (5, 2, 3, 4)
  </code></pre>