import tensorflow as tf
a=tf.random.normal([4,28,28,3])
a[1].shape
a[1,2].shape
a[1,2,3].shape
a[1,2,3,2].shape
''' start:end '''
b=tf.range(10)
b[-2:]
b[:3]
''' : 的运用-->全选 '''
a[:,0,:,:].shape
''' start:end:step ::step '''
a[0:2,0:27:2,:,:] #切片是左闭右开
a[:,::2,:,:] #从0-27,步长为 2
''' 倒序采样 '''
b[::-2] # 9,7,5,3,1
b[4::-2] # 4,2,0
b[3::2] # 3,5,7,9
b[3:9:2] # 3,5,7 不能取到 最后一个元素
''' 省略号的运用 ... '''
a[0,...].shape
a[0,...,1].shape
''' tf.gather tf.gather_nd tf.boolean_mask '''
c=tf.random.normal([4,35,8]) # 4个班级,每个班级35人,8个科目的成绩单
tf.gather(c,axis=0,indices=[0,2]).shape # 1,3两班的成绩单
tf.gather_nd(c,[[0,1,2],[1,2,3]]).shape # 选取特定班级,学生,科目
tf.boolean_mask(a,mask=[True,False,True],axis=3).shape
d=tf.ones([2,3,4])
tf.boolean_mask(d,mask=[[True,True,False],[False,True,True]]).shape # 4行4列