- tf.gather() #从一个维度上,根据索引获取tensor数据的一部分
例如:tf.gather(a, axis = 0, indices = [2, 3])
假设 a.shape ---------> [2, 35, 8]
上述操作,就是在a的0维度上获取,索引为2、3的数据
则tf.gather(a, axis = 0, indices = [2, 3]).shape ---> [2, 35, 8]
2.tf.gather_nd() #从多个维度上,根据索引,返回一个向量
例如:tf.gather_nd(a, [0])
假设 a.shape ----------->[4, 35, 8]
上述操作,就是在a的维度0上面,所以0对应的值
则tf.gather_nd(a, [0]).shape -------> (35, 8)
tf.gather_nd(a, [[0, 0], [1, 1]])
假设 a.shape --------->[4, 35, 8]
分析上述操作:[[0, 0], [1, 1]]
[0, 0] -------> 分别对应a的两个维度0、1
[1, 1]---------> 分别对应a的两个维度0、1
然后把索引数据,合并到一起(增加一个维度),返回
则tf.gather_nd(a, [0, 0], [1, 1]).shape ---->[2, 8]
3、tf.transpose() #转置
例如:a,shape ----> [4, 3, 2, 1]
b = tf.transpose(a)
b.shape --------> [1, 2, 3, 4] #在不指定转置后的维度顺序时,默认颠倒原始维 度顺序
e = tf.transpose(a, perm=[0, 1, 3, 2])
e.shape ----------> [4, 3, 1, 2] #可以通过perm参数指定,转置后的原始维度的 先后顺序
4、tf.squeeze() #移除张量大小为1的维度
例如:a.shape -----> [1, 2, 1, 1, 3]
tf.squeeze(a).shape -----------> [2, 3]
tf.squeeze(a, axis=2).shape ----------> [1, 2, 1, 3] #可以指定维度,当该维度 大小为1的时候被移除,否则报错
5、tf.tile() #对每一个维度指定复制的次数
例如:a.shape --------> [1, 3, 4]
tf.tile(a, [2, 1, 1]).shape ---------> [2, 3, 4] #用一个列表指定每一个维度对 应重复的次数