From docs :
Transmits a . Moves dimensions according to perm.
The returned tensor dimension i will correspond to the input dimension perm[i] . If perm not specified, it is set to (n-1 ... 0), where n is the rank of the input tensor. Therefore, by default, this operation performs a regular matrix transposed on two-dimensional input tensors.
But itβs still a little unclear to me how to cut the input tensor. For example. from the documents also:
tf.transpose(x, perm=[0, 2, 1]) ==> [[[1 4] [2 5] [3 6]] [[7 10] [8 11] [9 12]]]
Why does it perm=[0,2,1] create the 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
[exit]:
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 moved it:
fourthreetwo = tf.transpose(twothreefour) with tf.Session() as sess: init = tf.initialize_all_variables() sess.run(init) print (fourthreetwo.eval())
I get 4x3x2 to 2x3x4, and that sounds logical.
[exit]:
[[[ 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 for output, I'm not sure what I really get:
twofourthree = tf.transpose(twothreefour, perm=[0,2,1]) with tf.Session() as sess: init = tf.initialize_all_variables() sess.run(init) print (threetwofour.eval())
[exit]:
[[[ 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 perm=[0,2,1] returns a 2x4x3 matrix from 2x3x4?
Try 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())
[exit]:
[[[ 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 perm=[1,0,2] returns 3x2x4 of 2x3x4?
Does this mean that the perm parameter accepts my np.shape and np.shape tensor based on elements based on my array form?
those.
_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]))
[exit]:
(5, 2, 3, 4) (5, 2, 3, 4)