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>

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
注意事項:
a[-1:]傳回的是向量a[9] vector
a[-1]傳回的是标量9 scalar
start: end :step
間隔取出切片
start🔚step
: : -1 (逆序切片)
… (省略号)
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]
串行取樣
多元指定n個index
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)>