tf.transpose() function in TensorFlow

tf.transpose() is a function provided in TensorFlow. This function is used to transpose the input tensor.

Syntax: tf.transpose(input_tensor, perm, conjugate)

Parameters:
input_tensor: as the name suggests it is the tensor which is to be transposed.
Type: Tensor

perm: This parameters specifies the permutation according to which the input_tensor is to be transposed.
Type: Vector

conjugate: This parameters is set to True if the input_tensor is of type complex.
Type: Boolean



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

import tensorflow as geek
  
  
x = geek.constant([[1, 2, 3, 4],
                   [5, 6, 7, 8]])
transposed_tensor = geek.transpose(x)

chevron_right


Output :

array([[1, 5],
       [2, 6],
       [3, 7],
       [4, 8]])

Example 2: With using perm parameter:

When this parameter is passes the tensor is transposed along the given axis. In simple words it defines the output shape of the transposed tensor.

filter_none

edit
close

play_arrow

link
brightness_4
code

import tensorflow as geek
  
  
x = geek.constant([[[ 1, 2, 3],
                    [ 4, 5, 6]],
                   [[ 7, 8, 9],
                    [ 10, 11, 12]],
                   [[ 13, 14, 15],
                    [ 16, 17, 18]],
                   [[ 19, 20, 21],
                    [ 22, 23, 24]]])
transposed_tensor = geek.transpose(x, perm = [0, 2, 1])

chevron_right


Output:

array([[[ 1,  4],
        [ 2,  5],
        [ 3,  6]],

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

       [[13, 16],
        [14, 17],
        [15, 18]],

       [[19, 22],
        [20, 23],
        [21, 24]]])
shape (4, 3, 2)

The shape is (4, 3, 2) because our perm was [0, 2, 1]. The following is the mapping from perm to input tensor shape.

0 => 4
2 => 3
1 => 2

Example 3: Now we will study the conjugate parameter
It is set to True when we have complex variables in our tensor.

filter_none

edit
close

play_arrow

link
brightness_4
code

import tensorflow as geek
  
  
x = geek.constant([[1 + 1j, 2 + 2j, 3 + 3j],
                   [4 + 4j, 5 + 5j, 6 + 6j]])
transposed_tensor = geek.transpose(x)

chevron_right


Output:

array([[1 + 1j, 4 + 4j],
       [2 + 2j, 5 + 5j],
       [3 + 3j, 6 + 6j]])



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.