3 张量操作
3.1 索引
Tensorflow中,支持基本的[i][j]…的标准索引方式,也支持通过逗号分隔索引号的索引方式[i, j, k]。假设如下为图片数据
In [3]: x = tf.random.normal([4, 32, 32, 3])
- 取第一张图片数据
In [4]: x[0]
Out[4]:
<tf.Tensor: id=15, shape=(32, 32, 3), dtype=float32, numpy=
- 取第一张图片的第二行
In [5]: x[0][1]
Out[5]:
<tf.Tensor: id=23, shape=(32, 3), dtype=float32, numpy=
- 取第1张图片,第2行,第3列的像素
In [6]: x[0][1][2]
Out[6]: <tf.Tensor: id=35, shape=(3,), dtype=float32, numpy=array([-0.11009463, 0.0398486 , 1.8434385 ], dtype=float32)>
- 取第三张图片,第2行,第1列元素,第2个通道的颜色强度值
In [7]: x[2][1][0][1]
Out[7]: <tf.Tensor: id=51, shape=(), dtype=float32, numpy=0.96755>
- 取第2张图