Do you want to transpose the input tensor? If yes then tf.transpose() allows you to do so. In this entire tutorial, you will learn how to implement TensorFlow transpose in python using different examples.
Example on Tensorflow Transpose
Before going to the examples part make sure you have properly installed TensorFlow on your system. Also, note that all the examples have been done on Jupyter notebook. So for better understanding, you should do the same on it.
The syntax for the TensorFlow transpose is below.
tf.transpose(
a, perm=None, conjugate=False
)
Here ‘ a‘ is the input tensor. The perm parameter allows the tensor to be transposed along the given axis. It defines the output shape of the transposed tensor. The parameter conjugate is true if the input tensor is of a complex type.
Example 1: Simple Tensorflow transpose on input tensor
In the example, I will create a simple input tensor and pass it to the tf.transpose() function. The output will be the transpose of the input array. The output will also be of tensor type.
Run the below lines of code.
import tensorflow as tf
x = tf.constant([[10, 20, 30], [40, 50, 60]])
tf.transpose(x)
Output

Example 2: Transpose on input tensor using perm
If you pass the perm parameter then it will transpose the input array according to the size defined. For example,

I want to transpose as a 3×3 dimension then I will pass perm=[0,1]. Execute the below lines of code to transpose the tensor.
import tensorflow as tf
x = tf.constant([[10, 20, 30], [40, 50, 60],[70,80,90]])
tf.transpose(x,perm =[0,1])
Output

Example 3: Transpose of Conjugate tensor
The above examples were not for conjugate tensors. But in case the input tensor is conjugate then also you can transpose it. To do so you have to pass conjugate=True as an argument to the tf.transpose().
Let’s create sample conjugate and transpose the tensor. Execute the below lines of code and see the output.
import tensorflow as tf
x = tf.constant([[10 + 1j, 20 + 2j, 30 + 3j], [40 + 4j, 50 + 5j, 60 + 6j]])
tf.transpose(x,conjugate = True)
Output

There are examples of how to implement TensorFlow transpose. I hope you have liked this article. If you have any queries then you can contact us for more help.
Source:
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.