import tensorflow as tfa = tf.ones([1, 5, 5, 3])
a.shapea[0][0]
numpy : 索引a = tf.random.normal([4, 28, 28, 3])
a.shapea[1].shapea[1, 2].shape
a[1][2][3].shape
a[1, 2, 3, 2].shape
一维切片
a = tf.range(10)
aa[-1:]
a[-2:]
a[:2]
a[:-1]
多维切片
a = tf.random.normal([4, 28, 28, 3])
a.shapea[0].shapea[0, :, :, :].shapea[0, 1, :, :].shapea[:, :, :, 0].shapea[:, :, :, 2].shapea[:, 0, :, :].shape
步长::step
a = tf.random.normal([4, 28, 28, 3])
a.shapea[0:2, :, :, :].shapea[:, 0:28:2, 0:28:2, :].shapea[:, :14, :14, :].shapea[:, 14:, 14:, :].shapea[:, ::2, ::2, :].shape
倒序::-1
a = tf.range(4)
aa[::-1]a[::-2]a[2::-2]
省略号...
a = tf.random.normal([2, 4, 28, 28, 3])
a.shapea[0].shapea[0, :, :, :, :].shapea[0, ...].shapea[:, :, :, :, 0].shapea[..., 0].shapea[0, ..., 2].shapea[1, 0, ..., 0].shape
gathera = tf.random.normal([4, 35, 8])
a.shapetf.gather(a, axis=0, indices=[2, 3]).shapea[2:4].shapetf.gather(a, axis=0, indices=[2, 1, 3, 0]).shapetf.gather(a, axis=1, indices=[2, 3, 7, 9, 16]).shapetf.gather(a, axis=2, indices=[2, 3, 7]).shapeaa = tf.gather(a,axis,[several students])
aaa = tf.gather(aa,axis,[several subjects])
gather_nda = tf.random.normal([4, 35, 8])
a.shapetf.gather_nd(a, [0]).shape # [[0],[],[]]tf.gather_nd(a, [0, 1]).shapetf.gather_nd(a, [0, 1, 2]).shapetf.gather_nd(a, [[0, 1, 2]]).shapetf.gather_nd(a, [[0, 0], [1, 1]]).shapetf.gather_nd(a, [[0, 0], [1, 1], [2, 2]]).shape# 第一个班级第一个学生的第一门课
# 第二个班级第二个学生的第二门课
# 第三个班级第三个学生的第三门课
tf.gather_nd(a, [[0, 0, 0], [1, 1, 1], [2, 2, 2]]).shapetf.gather_nd(a, [[[0, 0, 0], [1, 1, 1], [2, 2, 2]]]).shape
boolean_mask
a = tf.random.normal([4, 28, 28, 3])
a.shapetf.boolean_mask(a, mask=[True, True, False, False]).shapetf.boolean_mask(a, mask=[True, True, False], axis=3).shapea = tf.ones([2, 3, 4])
a.shape# [2,3],还剩下4,三个True,因此是3*4True
tf.boolean_mask(a, mask=[[True, False, False], [False, True, True]]).shape