How does tf.transpose work in a tensor stream?

tf.transpose(a, perm=None, name='transpose') 

tolerates a. It is resized according to perm. Therefore, if I use this matrix for conversion:

 import tensorflow as tt import os os.environ["TF_CPP_MIN_LOG_LEVEL"]="3" import numpy as bb ab=([[[1,2,3],[6,5,4]],[[4,5,6],[3,6,3]]]) v=bb.array(ab) fg=tt.transpose(v) print(v) with tt.Session() as df: print("\n New tranformed matrix is: \n\n{}".format(df.run(fg))) 

Result:

 [[[1 2 3] [6 5 4]] [[4 5 6] [3 6 3]]] New tranformed matrix is: [[[1 4] [6 3]] [[2 5] [5 6]] [[3 6] [4 3]]] Process finished with exit code 0 

now if i use the perm parameter and then:

 import tensorflow as tt import os os.environ["TF_CPP_MIN_LOG_LEVEL"]="3" import numpy as bb ab=([[[1,2,3],[6,5,4]],[[4,5,6],[3,6,3]]]) v=bb.array(ab) fg=tt.transpose(v,perm=[0,2,1]) print(v) with tt.Session() as df: print("\n New tranformed matrix is: \n\n{}".format(df.run(fg))) 

Result:

 [[[1 2 3] [6 5 4]] [[4 5 6] [3 6 3]]] New tranformed matrix is: [[[1 6] [2 5] [3 4]] [[4 3] [5 6] [6 3]]] Process finished with exit code 0 

In this regard, I am confused, and I have two questions:

  • Whenever I want to transpose a matrix, should I give perm [0,2,1] as default?
  • What is 0.2.1 here?
+7
python numpy matplotlib matrix tensorflow
source share
1 answer

Looking at numpy.transpose , we find that transpose takes an argument

axes : int list, optional
Resize by default, otherwise move the axes according to the specified values.

Thus, the default transpose call is converted by default to np.transpose(a, axes=[1,0]) for 2D code or np.transpose(a, axes=[2,1,0]) .

The operation you want to have here is one that leaves the depth dimension unchanged. Therefore, in the argument of the axes, the depth axes, which are the 0 axes, must remain unchanged. Axes 1 and 2 (where 1 is the vertical axis), you need to change positions. Thus, you change the order of the axes from the initial [0,1,2] to [0,2,1] ( [stays the same, changes with other, changes with other] ).

For some reason, they are renamed axes in perm in the tensor flow. The argument above remains the same.

Images

In terms of images, they differ from arrays in question. Typically, images have x and y stored in the first two dimensions, and the channel in the last, [y,x,channel] .

To β€œtranspose” an image in the sense of a 2D transposition, where the horizontal and vertical axes change, you will need to use

 np.transpose(a, axes=[1,0,2]) 

(the channel remains the same, x and y are exchanged).

enter image description here

+13
source share

All Articles