今天向各位分享关于人工智能的专业技术贴,TensorFlow中张量Tensor的转置函数tf.transpose()的用法,重点介绍其参数perm及原理。原文来自我的好友,可以关注他的知乎号,李景文。
Tensor 的阶
在开始介绍转置函数之前,我们先来看一下Tensor的阶
当张量Tensor为一个标量时,即不带方向的纯量,其阶为0;
x0 = tf.constant(1)
print(x0) # 输出 tf.Tensor(1, shape=(), dtype=int32)
当Tensor为一个向量时,如[1, 2, 3]时,其阶为1;
x1 = tf.constant([1, 2, 3])
print(x1) # 输出 tf.Tensor([1 2 3], shape=(3,), dtype=int32)
当Tensor为矩阵时,其阶为2,如
x2 = tf.constant([[1, 2], [3, 4]])
print(x2) # 输出 tf.Tensor([[1 2] [3 4]], shape=(2, 2), dtype=int32)
而3阶Tensor可以被认为是一个立方体的数字集合,由多个小立方体组成,每个小立方体上存放了一个数字,如下图所示:
x3 = tf.constant([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10,