Tensor索引切片

索引与切片:获取Tensor中的一部分数据

Basic indexing

最基础的索引方式,索引方式比较单一,只能取具体某个

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
In [2]: a = tf.ones([1,5,5,3])
...: a[0][0]
Out[2]:
<tf.Tensor: shape=(5, 3), dtype=float32, numpy=
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=float32)>

In [4]: a[0][0][0]
Out[4]: <tf.Tensor: shape=(3,), dtype=float32, numpy=array([1., 1., 1.], dtype=float32)>

In [5]: a[0][0][0][2]
Out[5]: <tf.Tensor: shape=(), dtype=float32, numpy=1.0>

Numpy indexing

1
2
3
4
5
6
In [6]: a= tf.random.normal([4,28,28,3])
In [8]: a[1].shape
Out[8]: TensorShape([28, 28, 3])

In [10]: a[1,2,3,2].shape
Out[10]: TensorShape([])

切片[start:end]

[start:end) 不包含左边的

1
2
3
4
5
6
7
8
9
10
In [11]: a = tf.range(10)

In [13]: a[-1:]
Out[13]: <tf.Tensor: shape=(1,), dtype=int32, numpy=array([9], dtype=int32)>
In [14]: a[-2:]
Out[14]: <tf.Tensor: shape=(2,), dtype=int32, numpy=array([8, 9], dtype=int32)>
In [15]: a[:2]
Out[15]: <tf.Tensor: shape=(2,), dtype=int32, numpy=array([0, 1], dtype=int32)>
In [16]: a[2:-1]
Out[16]: <tf.Tensor: shape=(7,), dtype=int32, numpy=array([2, 3, 4, 5, 6, 7, 8], dtype=int32)>
1
2
3
4
5
6
7
8
9
10
a= tf.random.normal([4,28,28,3])

In [20]: a[0,:,:,:].shape
Out[20]: TensorShape([28, 28, 3])
In [21]: a[0,1,:,:].shape
Out[21]: TensorShape([28, 3])
In [23]: a[:,:,:,2].shape #取所有图片的的B通道
Out[23]: TensorShape([4, 28, 28])
In [24]: a[:,0,:,:].shape #取所有照片第0行所有列的RGB三通道
Out[24]: TensorShape([4, 28, 3])

间隔切片 [::]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
In [25]: a[0:2,:,:,:].shape #取01两张图片
Out[25]: TensorShape([2, 28, 28, 3])

In [26]: a[:,0:28:2,0:28:2,:].shape #间隔取
Out[26]: TensorShape([4, 14, 14, 3])

In [28]: a[:,14:,14:,:].shape #取14之后
Out[28]: TensorShape([4, 14, 14, 3])

In [29]: a[:,::2,::2,:].shape #间隔取
Out[29]: TensorShape([4, 14, 14, 3])

In [30]: a[:,2:26:2,2:26:2,:].shape
Out[30]: TensorShape([4, 12, 12, 3])

倒序

注意,倒序中start是要开始倒叙的第一个

1
2
3
4
5
6
7
8
9
10
In [31]: b=tf.range(4)

In [32]: b[::-1]
Out[32]: <tf.Tensor: shape=(4,), dtype=int32, numpy=array([3, 2, 1, 0], dtype=int32)>
In [33]: b[::-2]
Out[33]: <tf.Tensor: shape=(2,), dtype=int32, numpy=array([3, 1], dtype=int32)>
In [36]: b[2::-2] # 从第2个开始往前走
Out[36]: <tf.Tensor: shape=(2,), dtype=int32, numpy=array([2, 0], dtype=int32)>
In [37]: b[3:1:-1] #从第3个开始往前走
Out[37]: <tf.Tensor: shape=(2,), dtype=int32, numpy=array([3, 2], dtype=int32)>

… 采样

1
2
3
4
5
6
In [40]: a= tf.random.normal([2,4,28,28,3])

In [44]: a[...,0].shape
Out[44]: TensorShape([2, 4, 28, 28])
In [45]: a[1,2,...,0].shape
Out[45]: TensorShape([28, 28])

Selective indexing

例如a[4,28,28,3] ->取每张图片的3,27,9,13行中所有列的三通道 ->[4,4,28,3]

tf.gather

  • Data:[classes,student,subject] ->[4,35,8]
1
2
3
4
5
6
7
8
9
10
11
In [55]: a= tf.random.normal([4,35,8],mean=70,stddev =10,dtype=tf.float32)

#Tensor为a,维度是1,索引号为23的班级全部学生成绩
In [56]: tf.gather(a,axis=0,indices=[2,3]).shape
Out[56]: TensorShape([2, 35, 8])
#按照2130的顺序
In [59]: tf.gather(a,axis=0,indices=[2,1,3,0]).shape
Out[59]: TensorShape([4, 35, 8])
#抽取所有班级学号为237916的学生
In [61]: tf.gather(a,axis=1,indices=[2,3,7,9,16]).shape
Out[61]: TensorShape([4, 5, 8])

tf.gather_nd

  • 获取不同班级的某些不同学生的成绩
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#0号班级所有科目成绩 [0]理解为[[0],[],[]]联合索引的index组合
In [62]: tf.gather_nd(a,[0]).shape
Out[62]: TensorShape([35, 8])
#01
In [63]: tf.gather_nd(a,[0,1]).shape
Out[63]: TensorShape([8])
#0307
In [64]: tf.gather_nd(a,[0,30,7]).shape
Out[64]: TensorShape([])
#返回具体科目成绩但是一维的
In [65]: tf.gather_nd(a,[[0,30,7]]).shape
Out[65]: TensorShape([1])

#00号和11
In [76]: tf.gather_nd(a, [[0,0],[1,1]]).shape
Out[76]: TensorShape([2, 8])
In [80]: tf.gather_nd(a, [[0,0,2],[1,2,3]]).shape
Out[80]: TensorShape([2])
In [81]: tf.gather_nd(a, [[[0,0,2],[1,2,3]]]).shape
Out[81]: TensorShape([1, 2])

tf.boolean_mask

选True的位置

1
2
3
4
5
In [88]: tf.boolean_mask(a,mask=[True,True,False,False],axis=0).shape
Out[88]: TensorShape([2, 28, 28, 3])

In [89]: tf.boolean_mask(a,mask=[True,True,False],axis=3).shape
Out[89]: TensorShape([4, 28, 28, 2])
1
2
3
4
5
6
7
8
In [90]: a = tf.ones([2,3,4])
In [94]: tf.boolean_mask(a,mask=[[True,True,False],[False,True,True]])
Out[94]:
<tf.Tensor: shape=(4, 4), dtype=float32, numpy=
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]], dtype=float32)>
打赏
  • 版权声明: 本博客所有文章除特别声明外,均采用 Apache License 2.0 许可协议。转载请注明出处!

请我喝杯咖啡吧~

支付宝
微信