Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How tf.transpose works in tensorflow?

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

transposes a. It permutes the dimensions according to perm. So if I am using this matrix to transform:

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 is :

[[[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 perm argument 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 is :

[[[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

Due to this, I am confused and I have two questions :

  • Whenever I want to transpose a matrix I have to give perm[0,2,1] as default ?
  • What is 0,2,1 here ?
like image 922
stephen Avatar asked Apr 22 '17 19:04

stephen


People also ask

How does TF transpose work?

transpose(x, perm=[1, 0]) . As above, simply calling tf. transpose will default to perm=[2,1,0] . To take the transpose of the matrices in dimension-0 (such as when you are transposing matrices where 0 is the batch dimension), you would set perm=[0,2,1] .

How do you transpose a torch tensor?

Explain how to transpose a torch tensor? We can transpose a torch tensor by using torch. transpose(input, dim0, dim1) function which will consist of the input tensor and dimensions. The function will return the transposed version of the input given and the dimensions given i.e dim0 and dim1 are swapped.

How do you transpose a matrix into an array?

The transpose of a matrix is obtained by moving the rows data to the column and columns data to the rows. If we have an array of shape (X, Y) then the transpose of the array will have the shape (Y, X).

What is TF Argmax?

tf.argmax(input, axis=None, name=None, dimension=None) Returns the index with the largest value across axis of a tensor. For the case in specific, it receives pred as argument for it's input and 1 as axis . The axis describes which axis of the input Tensor to reduce across. For vectors, use axis = 0.


1 Answers

Looking at the numpy.transpose documentation, we find that transpose takes the argument

axes : list of ints, optional
By default, reverse the dimensions, otherwise permute the axes according to the values given.

So the default call to transpose translates into np.transpose(a, axes=[1,0]) for the 2D case, 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 axes argument, the depth axes, which is the 0th axes, needs to stay unchanged. The axes 1 and 2 (where 1 is the vertical axis), need to change positions. So you change the axes order from the initial [0,1,2] to [0,2,1] ([stays the same, changes with other, changes with other]).

In tensorflow, they have for some reason renamed axes to perm. The argument from above stays the same.

images

Concerning images, they differ from the arrays in the question. Images normally have their x and y stored in the first two dimensions and the channel in the last, [y,x,channel].

In order to "transpose" an image in the sense of a 2D transposition, where horizontal and vertical axes are exchanged, you would need to use

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

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

enter image description here

like image 180
ImportanceOfBeingErnest Avatar answered Oct 01 '22 19:10

ImportanceOfBeingErnest