Open In App

tf.transpose() function in TensorFlow

Last Updated : 22 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In TensorFlow tf.transpose() is used to rearrange the dimensions of a tensor. It works just like the transpose of a matrix where rows become columns and columns become rows. If you’re working with higher-dimensional data like 3D or 4D tensors tf.transpose() allow you to shuffle the dimensions however you want. The syntax for tf.transpose() is:

Syntax: tf.transpose(input_tensor, perm=None,conjugate=False)

where:

  • input_tensor: it is the tensor which is to be transposed.
  • perm: This parameters specifies the permutation according to which the input_tensor is to be transposed.
  • conjugate: This parameters is set to True if the input_tensor is of type complex.

1. Swapping Rows and Columns in a 2D Tensor

Create a simple 2D tensor which is like a matrix of rows and columns. We will create a constant 2D tensor using tf.constant. Then we’ll use tf.transpose() to swap its rows and columns.

Python
import tensorflow as tf

matrix = tf.constant([[1, 2, 3], [4, 5, 6]])
print("Original Matrix:")
print(matrix.numpy())

transposed_matrix = tf.transpose(matrix)
print("\nTransposed Matrix:")
print(transposed_matrix.numpy())

Output:

Screenshot-2025-04-20-180034
Transpose 2D Matrix

Here the rows and columns of the matrix are swapped. This is the most common use case for transposing.

2. Rearranging Dimensions in a 3D Tensor

A 3D tensor could represent something like a collection of images, video frames or other multi-dimensional data. In the below example we’ll manually specify how to rearrange the axes using the perm argument.

Python
tensor = tf.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]]])

print("Original Shape:", tensor.shape)

transposed_tensor = tf.transpose(tensor, perm=[2, 0, 1])
print("\nTransposed Shape:", transposed_tensor.shape)

Output:

Transpose-3D-matrix
Transpose 3D matrix

The original tensor has 2 blocks each with 3 rows and 4 columns. After transposing we have 4 blocks, each with 2 rows and 3 columns

3. Transposing Without the perm Argument

When you use tf.transpose() without the perm argument TensorFlow will automatically reverse the order of the axes of the tensor.

Python
tensor = tf.constant([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print("Original Shape:", tensor.shape)

transposed_tensor = tf.transpose(tensor)
print("\nTransposed Shape:", transposed_tensor.shape)

Output:

Screenshot-2025-04-20-185614
Tensorflow Default

In the above output reversing the shape (2, 2, 2) still gives (2, 2, 2) so visually it looks the same but the order of the axes has been flipped:

  • Axis 0 becomes axis 2
  • Axis 1 stays in the middle
  • Axis 2 becomes axis 0

With these methods we can apply transpose to a tensor and can change its dimension according to our needs.


Next Article
Practice Tags :

Similar Reads