Transposes a.
Aliases:
tf.compat.v2.transpose
tf.transpose(
a,
perm=None,
conjugate=False,
name='transpose'
)
Used in the tutorials:
Betterperformancewithtf.functionTransformermodelforlanguageunderstanding`` Permutes the dimensions according toperm. The returned tensor's dimension i will correspond to the input dimensionperm[i]. Ifpermis not given, it is set to (n-1...0), where n is the rank of the input tensor. Hence by default, this operation performsaregular matrix transpose on 2-D input Tensors. If conjugate is Trueanda.dtypeis eithercomplex64orcomplex128then the values ofaare conjugatedand transposed.
For example:
x = tf.constant([[1, 2, 3], [4, 5, 6]])
tf.transpose(x) # [[1, 4]
# [2, 5]
# [3, 6]]
# Equivalently
tf.transpose(x, perm=[1, 0]) # [[1, 4]
# [2, 5]
# [3, 6]]
# If x is complex, setting conjugate=True gives the conjugate transpose
x = tf.constant([[1 + 1j, 2 + 2j, 3 + 3j],
[4 + 4j, 5 + 5j, 6 + 6j]])
tf.transpose(x, conjugate=True) # [[1 - 1j, 4 - 4j],
# [2 - 2j, 5 - 5j],
# [3 - 3j, 6 - 6j]]
# 'perm' is more useful for n-dimensional tensors, for n > 2
x = tf.constant([[[ 1, 2, 3],
[ 4, 5, 6]],
[[ 7, 8, 9],
[10, 11, 12]]])
# Take the transpose of the matrices in dimension-0
# (this common operation has a shorthand `linalg.matrix_transpose`)
tf.transpose(x, perm=[0, 2, 1]) # [[[1, 4],
# [2, 5],
# [3, 6]],
# [[7, 10],
# [8, 11],
# [9, 12]]]
Args:
a: ATensor.perm: Apermutation of the dimensions ofa.conjugate: Optional bool. Setting it toTrueis mathematically equivalent to tf.math.conj(tf.transpose(input)).name: Anamefor the operation (optional).
Returns:
A transposed Tensor.
Numpy Compatibility
In numpy transposes are memory-efficient constant time operations as they simply return a new view of the same data with adjusted strides.
TensorFlow does not support strides, so transpose returns a new tensor with the items permuted.