索引与切片
Indexing 索引
- Basic indexing
- Same with Numpy
- [idx, idx,…]
- start:end
- start🔚step
- …
import tensorflow as tf
import numpy as np
Basic indexing
a=tf.ones([1,5,5,3])
a.shape
TensorShape([1, 5, 5, 3])
a[0][0]
<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)>
a[0][0][0]
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([1., 1., 1.], dtype=float32)>
a[0][0][0][2]
<tf.Tensor: shape=(), dtype=float32, numpy=1.0>
Numpy-style indexing
a=tf.random.normal([4,28,28,3])
a[1].shape
TensorShape([28, 28, 3])
a[1,2].shape
TensorShape([28, 3])
a[1,2,3].shape
TensorShape([3])
a[1,2,3]
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([-0.1556891 , -0.03499719, 1.491767 ], dtype=float32)>
a[1,2,3,2].shape
TensorShape([])
a[1,2,3,2]
<tf.Tensor: shape=(), dtype=float32, numpy=1.491767>
start:end
a=tf.range(10)
a
<tf.Tensor: shape=(10,), dtype=int32, numpy=array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int32)>
a[0]
<tf.Tensor: shape=(), dtype=int32, numpy=0>
a[-1:]
<tf.Tensor: shape=(1,), dtype=int32, numpy=array([9], dtype=int32)>
a[-2:]
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([8, 9], dtype=int32)>
a[:2]
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([0, 1], dtype=int32)>
a[:-1]
<tf.Tensor: shape=(9,), dtype=int32, numpy=array([0, 1, 2, 3, 4, 5, 6, 7, 8], dtype=int32)>
Indexing by 综合使用
a=tf.random.normal([4,28,28,3])
a.shape
TensorShape([4, 28, 28, 3])
后置切片
a[0].shape
TensorShape([28, 28, 3])
a[0,:,:,:].shape
TensorShape([28, 28, 3])
a[0,1,:,:].shape
TensorShape([28, 3])
a[0,1,1,:].shape
TensorShape([3])
前置切片
a[:,:,:,:].shape
TensorShape([4, 28, 28, 3])
a[:,:,:,0].shape
TensorShape([4, 28, 28])
a[:,:,:,2].shape
TensorShape([4, 28, 28])
前后切片
a[:,0,:,:].shape
TensorShape([4, 28, 3])
Indexing by ::
a=tf.random.normal([4,28,28,3])
a.shape
TensorShape([4, 28, 28, 3])
a[0:2,:,:,:].shape
TensorShape([2, 28, 28, 3])
a[:,0:28:2,0:28:2,:].shape
TensorShape([4, 14, 14, 3])
a[:,:14,:14,:].shape
TensorShape([4, 14, 14, 3])
a[:,14:,14:,:].shape
TensorShape([4, 14, 14, 3])
a[:,::2,::2,:].shape
TensorShape([4, 14, 14, 3])
::-1 倒序
a=tf.range(4)
a
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([0, 1, 2, 3], dtype=int32)>
a[::-1]
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([3, 2, 1, 0], dtype=int32)>
a[::-2]
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([3, 1], dtype=int32)>
a[2::-2]
<tf.Tensor: shape=(2,), dtype=int32, numpy=array([2, 0], dtype=int32)>
… 省略号的方式
a=tf.random.normal([2,4,28,28,3])
a.shape
TensorShape([2, 4, 28, 28, 3])
a[0].shape
TensorShape([4, 28, 28, 3])
a[0,:,:,:,:].shape
TensorShape([4, 28, 28, 3])
a[0,...].shape
TensorShape([4, 28, 28, 3])
a[0,:,...].shape
TensorShape([4, 28, 28, 3])
a[:,:,:,:,0].shape
TensorShape([2, 4, 28, 28])
a[...,0].shape
TensorShape([2, 4, 28, 28])
a[0,...,2].shape
TensorShape([4, 28, 28])
a[1,0,...,2].shape
TensorShape([28, 28])
Selective Indexing 选择性索引
- tf.gather
- tf.gather_nd
- tf.boolean_ma
tf.gather
- data: [classes, students, subjects]
a=tf.random.normal([4,35,8])
a.shape
TensorShape([4, 35, 8])
tf.gather(a, axis=0,indices=[2,3]).shape
TensorShape([2, 35, 8])
a[2:4].shape
TensorShape([2, 35, 8])
tf.gather(a, axis=0,indices=[2,1,3,0]).shape
TensorShape([4, 35, 8])
tf.gather(a, axis=1,indices=[2,3,7,9,16]).shape
TensorShape([4, 5, 8])
tf.gather(a, axis=2,indices=[2,3,7]).shape
TensorShape([4, 35, 3])
tf.gather_nd
- data: [classes, students, subjects]
- What if sample several students and their several subjects?如果抽样几个学生和他们的几个科目呢?
- 𝑎𝑎 = 𝑡𝑓. 𝑔𝑎𝑡ℎ𝑒𝑟(𝑎, 𝑎𝑥𝑖𝑠,[𝑠𝑒𝑣𝑒𝑟𝑎𝑙 𝑠𝑡𝑢𝑑𝑒𝑛𝑡𝑠])
- 𝑎𝑎𝑎 = 𝑡𝑓. 𝑔𝑎𝑡ℎ𝑒𝑟(𝑎𝑎, 𝑎𝑥𝑖𝑠,[𝑠𝑒𝑣𝑒𝑟𝑎𝑙 𝑠𝑢𝑏𝑗𝑒𝑐𝑡𝑠])
a=tf.random.normal([4,35,8])
a.shape
TensorShape([4, 35, 8])
aa=tf.gather(a,axis=1,indices=[1,3,5,7,8,12,16])
aa.shape
TensorShape([4, 7, 8])
aaa=tf.gather(aa,axis=2,indices=[0,1,2])
aaa.shape
TensorShape([4, 7, 3])
tf.gather_nd
- data: [classes, students, subjects]
- What if sample several (classes and students)?如果抽样几个(班级和学生)呢
- 𝑓𝑜𝑟 𝑖𝑛𝑠𝑡𝑎𝑛𝑐𝑒:
- [class1_student1, class2_studnet2, class3_student3, class4_student4]
- → [4, 8]
a=tf.random.normal([4,35,8])
a.shape
TensorShape([4, 35, 8])
tf.gather_nd(a, [0]).shape
TensorShape([35, 8])
tf.gather_nd(a, [0,1]).shape
TensorShape([8])
tf.gather_nd(a, [0,1,2]).shape
TensorShape([])
tf.gather_nd(a, [0,1,2])
<tf.Tensor: shape=(), dtype=float32, numpy=0.012457304>
tf.gather_nd(a, [[0,1,2]]).shape
TensorShape([1])
tf.gather_nd(a, [[0,1,2]])
<tf.Tensor: shape=(1,), dtype=float32, numpy=array([0.0124573], dtype=float32)>
更复杂的使用方式
tf.gather_nd(a, [[0,0],[1,1]]).shape
TensorShape([2, 8])
tf.gather_nd(a, [[0,0],[1,1],[2,2]]).shape
TensorShape([3, 8])
tf.gather_nd(a, [[0,0,0],[1,1,1],[2,2,2]]).shape
TensorShape([3])
tf.gather_nd(a, [[[0,0,0],[1,1,1],[2,2,2]]]).shape
TensorShape([1, 3])
tf.gather_nd
- recommended ind
- [[0], [1],…]
- [[0,0], [1,1],…]
- [[0,0,0], [1,1,1],…]
- 推荐2层的写法:[[0]]
tf.boolean_mask
a=tf.random.normal([4,28,28,3])
a.shape
TensorShape([4, 28, 28, 3])
tf.boolean_mask(a, mask=[True, True,False,False]).shape
TensorShape([2, 28, 28, 3])
tf.boolean_mask(a, mask=[True, True,False],axis=3).shape
TensorShape([4, 28, 28, 2])
a=tf.ones([2,3,4])
tf.boolean_mask(a, mask=[[True,False,False],[False, True,True]])
<tf.Tensor: shape=(3, 4), dtype=float32, numpy=
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]], dtype=float32)>