Tra
nsposes a
.
Aliases:
tf.compat.v2.transpose
tf.transpose(
a,
perm=None,
conjugate=False,
name='transpose'
)
Used in the tutorials:
B
et
te
rp
e
rf
or
ma
nc
ew
i
th
t
f.
fu
nc
ti
on
T
ra
ns
fo
rm
er
m
od
el
f
or
l
an
gu
ag
eu
n
de
rs
ta
nd
in
g`` Permutes the dimensions according toperm
. The returned tensor's dimension i will correspond to the input dimensionperm[i]
. Ifperm
is not given, it is set to (n-1...0), where n is the ra
nk of the input tensor. Hence by defa
ult, this opera
tion performsa
regula
r ma
trix tra
nspose on 2-D input Tensors. If conjuga
te is Truea
nda.dtype
is eithercomplex64
orcomplex128
then the va
lues ofa
a
re conjuga
teda
nd tra
nsposed.
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
: Aperm
uta
tion of the dimensions ofa
.conjugate
: Optiona
l bool. Setting it toTrue
is ma
thema
tica
lly equiva
lent to tf.ma
th.conj(tf.tra
nspose(input)).name
: Aname
for the opera
tion (optiona
l).
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.