TensorFlow 中的 tf.transpose()函数
tf.transpose()
是 TensorFlow 中提供的函数。该函数用于转置输入张量。
Syntax: tf.transpose(input_tensor, perm, conjugate)
Parameters:
input_tensor: as the name suggests it is the tensor which is to be transposed.
Type: Tensor
perm: This parameters specifies the permutation according to which the input_tensor is to be transposed.
Type: Vector
conjugate: This parameters is set to True if the input_tensor is of type complex.
Type: Boolean
示例 1:
import tensorflow as geek
x = geek.constant([[1, 2, 3, 4],
[5, 6, 7, 8]])
transposed_tensor = geek.transpose(x)
输出 :
array([[1, 5],
[2, 6],
[3, 7],
[4, 8]])
示例 2:使用 perm 参数:
当此参数通过时,张量沿给定轴转置。简单来说,它定义了转置张量的输出形状。
import tensorflow as geek
x = geek.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]]])
transposed_tensor = geek.transpose(x, perm = [0, 2, 1])
输出:
array([[[ 1, 4],
[ 2, 5],
[ 3, 6]],
[[ 7, 10],
[ 8, 11],
[ 9, 12]],
[[13, 16],
[14, 17],
[15, 18]],
[[19, 22],
[20, 23],
[21, 24]]])
shape (4, 3, 2)
形状是 (4, 3, 2) 因为我们的烫发是 [0, 2, 1]。以下是从 perm 到输入张量形状的映射。
0 => 4
2 => 3
1 => 2
示例 3:现在我们将研究共轭参数
当我们的张量中有复杂的变量时,它被设置为True 。
import tensorflow as geek
x = geek.constant([[1 + 1j, 2 + 2j, 3 + 3j],
[4 + 4j, 5 + 5j, 6 + 6j]])
transposed_tensor = geek.transpose(x)
输出:
array([[1 + 1j, 4 + 4j],
[2 + 2j, 5 + 5j],
[3 + 3j, 6 + 6j]])