tf.transpose(a, perm=None, name='transpose')
Transposes a. Permutes the dimensions according to perm.
The returned tensor's dimension i will correspond to the input dimension perm[i]. If perm is not given, it is set to (n-1...0), where n is the rank of the input tensor. Hence by default, this operation performs a regular matrix transpose on 2-D input Tensors.
For example:
# 'x' is [[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]]
# 'perm' is more useful for n-dimensional tensors, for n > 2
# 'x' is [[[1 2 3]
# [4 5 6]]
# [[7 8 9]
# [10 11 12]]]
# Take the transpose of the matrices in dimension-0
tf.transpose(b, perm=[0, 2, 1]) ==> [[[1 4]
[2 5]
[3 6]]
[[7 10]
[8 11]
[9 12]]]
Args:
•a: A Tensor.
•perm: A permutation of the dimensions of a.
•name: A name for the operation (optional).
Returns:
A transposed Tensor.
按照翻译来解释,就是转置的意思.如果把一个矩阵
[[1 2 0]
[3 4 0]] 转置,那么很好理解,就是
[ [1 3]
[2 4]
[0 0]
]
这是二维的矩阵,一旦这个矩阵的维度大于等于3维,那么必须定义怎么转置,这就是perm参数的意义.假如一个矩阵有n维,那么perm就是一个大小为n的列表,其值就是[0:n-1]的一个排列.perm中,位置i的值j代表转置后的矩阵j维就是转置前矩阵i维的值.
例子:
arr = np.zeros([2, 1 ,3])
arr
after_transpose = tf.transpose(arr, [1, 2, 0])
print(after_transpose)
# Tensor("transpose:0", shape=(1, 3, 2), dtype=float64)