Tensorflow Transpose Implementation in Python with Examples

Tensorflow Transpose Implementation in Python with Examples

Popular Domains for just 99 Cents at Namecheap!

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

Simple transpose of tensor
Simple transpose of tensor

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,

transpose of tensor with perm parameter
transpose of a tensor with perm parameter

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

transpose of tensor with perm parameter
Transpose of a tensor with perm parameter

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

Transpose of Conjugate tensor
Transpose of Conjugate tensor

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:

Tensorflow Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner