Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Function

tf.transpose

Transposes a Tensor by permuting its dimensions. By default, it reverses all dimensions; for complex64 or complex128 inputs, conjugate=True also applies complex conjugation.

How it works

The operation returns a tensor whose dimension at position i comes from the input dimension indexed by perm[i]. Without perm, TensorFlow reverses every dimension; conjugate=True additionally conjugates complex64 or complex128 values while transposing them.

Syntax

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

Parameters

NameTypeDescription
aTensorA Tensor.
permTensor or sequence of integers, default NoneA vector that specifies a permutation of the dimensions of a.
conjugatebool, default FalseIf True, conjugates complex64 or complex128 values while transposing.
namestring, default 'transpose'Optional name for the operation.

Returns

A transposed Tensor.

Key points

  • The function transposes a Tensor and returns the reordered Tensor.
  • Each output axis i is populated from the input axis selected by perm[i].
  • Leaving perm unset reverses the complete axis order based on the tensor rank.
  • For a matrix, the default behavior exchanges rows and columns.
  • The conjugation option applies to complex64 and complex128 inputs.
  • For a 2-D tensor, explicitly setting perm to [1, 0] matches the default transpose.

Common mistakes

  • This is a permutation operation, not a strided view: TensorFlow returns a new tensor with reordered items.
  • When supplied, perm must be a vector describing a valid permutation of the input dimensions.
  • For complex tensors, conjugate=True combines conjugation with transposition; it is equivalent to conjugating the transposed result.

Example: Transpose a matrix

import tensorflow as tf

x = tf.constant([[1, 2, 3], [4, 5, 6]])
y = tf.transpose(x)
print(y.numpy())

Reverses the two dimensions of a 2-D tensor, changing its shape from (2, 3) to (3, 2).

When to use it

Swap rows and columns of a 2-D tensor

Matrix manipulation

Reorder batch, spatial, channel, or feature axes

Batch data layouts

Compute a conjugate transpose for complex tensors

Complex linear algebra

Interview questions

QWhat does perm mean in tf.transpose?

tf.transpose reorders tensor dimensions according to perm. Output dimension i corresponds to input dimension perm[i]; if perm is omitted, dimensions are reversed.

Follow-up: How would you transpose only the last two dimensions of a rank-3 tensor? Use perm=[0, 2, 1], preserving dimension 0 while swapping dimensions 1 and 2.

QWhat happens when perm is omitted?

For a rank-n tensor, omitting perm uses the order (n-1,...,0). Thus a rank-2 matrix is transposed from shape (rows, columns) to (columns, rows).

Follow-up: What is the default permutation for a rank-4 tensor? It is [3, 2, 1, 0].

QWhat does conjugate=True do?

conjugate=True applies complex conjugation as part of the transpose when the input dtype is complex64 or complex128. For non-complex inputs, it does not add a meaningful conjugation step.

Follow-up: How can the same result be expressed explicitly? Apply tf.transpose first and then tf.math.conj to the result.

  • tf.math.conj
  • tf.reshape
  • tf.image.transpose

Source: tf.transpose. Checked 2026-10-15.