gather函数
gather(tensor_a,tensor_b),根据tensor_b调整保留第2个纬度的顺序,如果输入2维或多维tensor_b会返回多次调整结果,值调整第2维不调整其他纬度,例:
1 import tensorflow as tf
2 tensor_a = tf.Variable([[[1,2],[3,4],[5,6]],[[4,5],[6,7],[8,9]]])
3 tensor_b = tf.Variable([[1,0],[0,1]],dtype=tf.int32)
4 tensor_c = tf.Variable([0,0],dtype=tf.int32)
5 with tf.Session() as sess:
6 sess.run(tf.global_variables_initializer())
7 print('*************')
8 print(sess.run(tf.gather(tensor_a,tensor_b)))
9 print('*************')
10 print(sess.run(tf.gather(tensor_a,tensor_c)))
*************
[[[[4 5]
[6 7]
[8 9]]
[[1 2]
[3 4]
[5 6]]]
[[[1 2]
[3 4]
[5 6]]
[[4 5]
[6 7]
[8 9]]]]
*************
[[[1 2]
[3 4]
[5 6]]
[[1 2]
[3 4]
[5 6]]]
batch_gather函数
batch_gather(tensor_a,tensor_b)调整多个纬度,从第2个纬度开始,tensor_b有几个纬度就可以调整几个(当然tensor_a纬度必须足够多),例子:
1 import tensorflow as tf
2 tensor_a = tf.Variable([[[1,2],[3,4],[5,6]],[[4,5],[6,7],[8,9]]])
3 tensor_b = tf.Variable([[1,0,2],[2,1,0]],dtype=tf.int32)
4 tensor_c = tf.Variable([0,0],dtype=tf.int32)
5 with tf.Session() as sess:
6 sess.run(tf.global_variables_initializer())
7 print('*************')
8 print(sess.run(tf.batch_gather(tensor_a,tensor_b)))
9 print('*************')
10 print(sess.run(tf.gather(tensor_a,tensor_c)))
*************
[[[3 4]
[1 2]
[5 6]]
[[8 9]
[6 7]
[4 5]]]
*************
[[[1 2]
[3 4]
[5 6]]
[[1 2]
[3 4]
[5 6]]]
|