天天看点

Tensorflow2.0——2、索引与切片Basic indexingNumpy-style indexingstart:endstart: end :step: : -1 (逆序切片)… (省略号)Selective Indexing(灵活性大)

Basic indexing

●●●
In [4]: a=tf.ones([1,5,5,3] ) //定义一个dim=4的tensor
In [5]: a[0][0] //取出2维向量,形状5行3列
<tf.Tensor: id=16, 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 [6]: a[0][0][0] //取出a[0][0]的第0行
0ut[6]: <tf.Tensor: id=29, shape=(3,), dtype float32, numpy=array([1., 1., 1.],
dtype= float32)>

In [7]: a[0][0][0][2] //取出a[0][0][0]的第二个数,scalar
Out[7]: <tf.Tensor: id=46, shape=(), dtype= float32, numpy=1.0> 

           
Tensorflow2.0——2、索引与切片Basic indexingNumpy-style indexingstart:endstart: end :step: : -1 (逆序切片)… (省略号)Selective Indexing(灵活性大)

Numpy-style indexing

In [8]: a=tf . random.normal( [4,28,28,3]) //4张照片,像素28*28,3色,rgb
In [9]: a[1] . shape //取第2张照片,
0ut[9]: TensorShape( [28, 28, 3])

In [10]: a[1 ,2] . shape //取第2张照片,像素第三行
Out[10]: TensorShape( [28, 3])

In [11]: a[1,2,3] . shape //取第2张照片,像素第三行,第三列
0ut[11]: Tensor Shape([3])

In [12]: a[1 ,2,3,2] .shape //取第2张照片,像素第三行,第三列,指定色为blue通道
Out[12]: TensorShape([])

           

start:end

Tensorflow2.0——2、索引与切片Basic indexingNumpy-style indexingstart:endstart: end :step: : -1 (逆序切片)… (省略号)Selective Indexing(灵活性大)

注意事项:

a[-1:]返回的是向量a[9] vector

a[-1]返回的是标量9 scalar

start: end :step

间隔取出切片

start🔚step

Tensorflow2.0——2、索引与切片Basic indexingNumpy-style indexingstart:endstart: end :step: : -1 (逆序切片)… (省略号)Selective Indexing(灵活性大)

: : -1 (逆序切片)

Tensorflow2.0——2、索引与切片Basic indexingNumpy-style indexingstart:endstart: end :step: : -1 (逆序切片)… (省略号)Selective Indexing(灵活性大)

… (省略号)

Tensorflow2.0——2、索引与切片Basic indexingNumpy-style indexingstart:endstart: end :step: : -1 (逆序切片)… (省略号)Selective Indexing(灵活性大)

Selective Indexing(灵活性大)

■ tf.gather

■ tf.gather_ nd

■ tf.boolean_ mask

tf.gather(无序排列 indices)

eg:

■data: [classes, students, subjects]

. [ 4 , 35 , 8]

In [46]: tf.gather(a, axis=0, indices=[2, 3]). shape
0ut[46]: TensorShape([2, 35,8])

In [47]: a[2:4]. shape
0ut[47]: Tensor Shape([2, 35,8])

In [48]: tf .gather(a, axis=0, indices=[2,1,3,0]). shape
0ut[48]: Tensor Shape([4, 35,8])

In [49]: tf. gather (a,axis=1, indices=[2,3,7,9,16]). shape
0ut[49]: Tensor Shape([4, 5,8])

In [50]: tf.gather(a, axis=2, indices=[2,3,7]). shape
Out[50]: Tensor Shape([4, 35,3])

           

tf.gather_ nd

采样7个学生3门成绩

a= [ 4 , 35 , 8]

串行取样

Tensorflow2.0——2、索引与切片Basic indexingNumpy-style indexingstart:endstart: end :step: : -1 (逆序切片)… (省略号)Selective Indexing(灵活性大)

多维指定n个index

Tensorflow2.0——2、索引与切片Basic indexingNumpy-style indexingstart:endstart: end :step: : -1 (逆序切片)… (省略号)Selective Indexing(灵活性大)

eg:

In [55]: a. shape
Out[55]: Tensor.Shape([4, 35,8])

In [60]: tf.gather_ nd(a, [0]). shape
Out[60]: Tensor Shape([35,8])

In [61]: tf.gather_ nd(a, [0, 1]). shape
0ut[61]: Tensor Shape([8])

In [62]: tf.gather_nd(a, [0, 1,2]). shape
Out[62]: Tensor.Shape([])

//注意该段代码与62行代码区别
//[0,1,2]是指取第1个班第2个学生的第3门课程成绩放在a[]中
//[[0,1,2]]是指a[]是一个成绩的标量,对于该方法的调用就是指得到一个标量额dim,而标量dim就是1维
In [63]: tf.gather. _nd(a, [[0,1,2]]). shape
Out[63]: Tensor Shape([1])

           
In [55]: a. shape
Out[55]: Tensor .Shape([4, 35,8])

//两个学生8门课的成绩
In [56]: tf.gather_ nd(a, [[0,0],[1,1]). shape
Out[56]: Tensor.Shape([2, 8])

In [57]: tf.gather_ nd(a, [[0,0],[1, 1],[2,2]]). shape
Out[57]: Tensor.Shape([3, 8])

//采取三个样本 [0,0,0]就是采取的一个标量,总共3个
In [58]: tf.gather_ _nd(a, [[0,0,0],[1, 1, 1],[2,2,2]]). shape
Out[58]: Tensor .Shape([3])

//	[[scalar,scalar,scalar]]	所有scalar均为成绩标量
//	shape为1行3列					
In [59]: tf.gather_ _nd(a, [[[0,0,0],[1,1,1],[2,2,2]]). shape
Out[59]: Tensor .Shape([1, 3])

           

tf.boolean_ mask

In [75]: a. shape
Out[75]: Tensor .Shape([4, 28, 28, 3])

//未定义axis就是去最外围的axis=0 (即第一列4)√ √ × ×(第一列中有2个取到)
In [76]: tf.boolean _mask(a, mask=[True,True,False,False]).shape
Out[76]: Tensor .Shape([2, 28, 28,3])

//取rgb中的rg,b不要,故只有2个
In [77]: tf. boolean_ mask(a, mask=[True,True,False],axis=3). shape
0ut[77]: Tensor .Shape([4, 28,28,2])

//mask选择[2,3]进行判断
In [78]: a=tf .ones([2,3,4])
In [79]: tf. boolean_ mask(a, mask=[[True,False,False],[False, True,True]])
<tf. Tensor: id=354, shape=(3, 4),dtype=f loat32, numpy=
array([[1., 1., 1., 1.],
	   [1., 1., 1., 1.],
       [1., 1., 1., 1.]], dtype=f loat32)>

           
Tensorflow2.0——2、索引与切片Basic indexingNumpy-style indexingstart:endstart: end :step: : -1 (逆序切片)… (省略号)Selective Indexing(灵活性大)

继续阅读