欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

理解和探索多维度张量的几何特性

最编程 2024-02-20 14:13:46
...

Tensor是Tensorflow中最基础的数据结构,常常翻译为张量,可以理解为n维数组或矩阵,相关函数:

constant(value, dtype=None, shape=None, name='Const', verify_shape=False)

三维方向定义,适用于2维和3维

0、零维张量

import tensorflow as tf

#零维张量
const0 = tf.constant(1, tf.float16)
print(const0)

运行结果:

Tensor("Const:0", shape=(), dtype=float16)

1、一维张量

#一维张量,长度为4
const1 = tf.constant([1, 2, 3, 4], tf.float16)
print(const1)

运行结果:

Tensor("Const_1:0", shape=(4,), dtype=float16)

几何表示:

一维张量没有行和列的概念,只有长度的概念。上述的const1就是长度为4的一维张量,或者称为向量。 上面的图仅为示意,代表一维张量只有axis=0这个方向,并不是指这是一个4行的向量。事实上,tensorflow在做一些运算时,反而经常把1行N列的二维张量简化成一个长度为N的一维向量。

2、二维张量

#二维张量,3行4列
const2 = tf.constant([
                     [1, 2, 3, 4],
                     [5, 6, 7, 8],
                     [9, 10, 11, 12]
                     ], tf.float16)
print(const2)

运行结果:

Tensor("Const_2:0", shape=(3, 4), dtype=float16)

几何表示:

3、三维张量

#三维张量,3行4列深度为2的张量
const3 = tf.constant([
                     [[ 1,  2], [ 3,  4], [ 5,  6], [ 7,  8]],
                     [[11, 12], [13, 14], [15, 16], [17, 18]],
                     [[21, 22], [23, 24], [25, 26], [27, 28]]
                     ], tf.float16)
print(const3)

运行结果:

Tensor("Const_3:0", shape=(3, 4, 2), dtype=float16)

几何表示:

4、四维张量(图像仅用于理解,坐标系axis已经不再适用)

#四维张量
const4 = tf.constant([
                     #第一个3行4列深度为2的三维张量
                     [[[1,  2], [ 3,  4], [ 5,  6], [ 7,  8]],
                     [[11, 12], [13, 14], [15, 16], [17, 18]],
                     [[21, 22], [23, 24], [25, 26], [27, 28]]
                     ],
                     #第二个3行4列深度为2的三维张量
                     [[[1,  2], [ 3,  4], [ 5,  6], [ 7,  8]],
                     [[11, 12], [13, 14], [15, 16], [17, 18]],
                     [[21, 22], [23, 24], [25, 26], [27, 28]]]
                     ], tf.float16)
print(const4)

运行结果:

Tensor("Const_4:0", shape=(2, 3, 4, 2), dtype=float16)

几何表示:

这个图上的axis不对,行应为axis=1,以此类推

如何判断一个张量的batch数、行、列、深度:

const4 = tf.constant([
                     #第一个3行4列深度为2的三维张量
                     [[[1,  2], [ 3,  4], [ 5,  6], [ 7,  8]],
                     [[11, 12], [13, 14], [15, 16], [17, 18]],
                     [[21, 22], [23, 24], [25, 26], [27, 28]]
                     ],
                     #第二个3行4列深度为2的三维张量
                     [[[1,  2], [ 3,  4], [ 5,  6], [ 7,  8]],
                     [[11, 12], [13, 14], [15, 16], [17, 18]],
                     [[21, 22], [23, 24], [25, 26], [27, 28]]]
                     ], tf.float16)

从左边开始数连续[的数量,最多有X个[说明是X维张量。上面的例子就是4维张量。 以三维以上的张量为例: 从左边开始数连续的[,最后一个[对应的]中一共两个元素,分别为1, 2,说明深度为2。 接下来向左边数上一级[对应的]中一共有4个元素[1, 2], [ 3, 4], [ 5, 6], [ 7, 8],说明列为4。 同理继续数上一级,得到3行,2个batch。

小结:

shape属性中的元素大于等于3时,可以用3维空间来理解。 shape=(3, 4, 2)时,表示3行4列深度为2的张量 shape=(2, 3, 4, 2)时,表示有2个 3行4列深度为2的张量 shape=(6, 2, 3, 4, 2)时,表示有6个四维张量,这个四维张量又可以表示为2个 3行4列深度为2的张量。

shape中的属性分别与axis=0axis=1axis=2axis=3……对应,以此类推。当维度超过3时,上图几何中的坐标系表示就已经错误了。但是对于理解多维是有帮助的。