tensorflow 利用placeholder选择每个batch里的sub-tensor 实例

网友投稿 837 2022-11-15

tensorflow 利用placeholder选择每个batch里的sub-tensor 实例

tensorflow 利用placeholder选择每个batch里的sub-tensor 实例

只能reshape成-1,然后gather的时候累加batch_size去取

import tensorflow as tfdef gather_indexes_2d(sequence_tensor, positions): sequence_shape = sequence_tensor.shape.as_list() batch_size = sequence_shape[0] seq_length = sequence_shape[1] flat_offsets = tf.reshape( tf.range(0, batch_size, dtype=tf.int32) * seq_length, [-1]) flat_positions = tf.reshape(positions + flat_offsets, [-1]) flat_sequence_tensor = tf.reshape(sequence_tensor, [batch_size * seq_length]) output_tensor = tf.gather(flat_sequence_tensor, flat_positions) return output_tensorvalue = [[0,1],[2,3],[4,5]]init = tf.constant_initializer(value)v = tf.get_variable('value', shape=[3,2], initializer=init,dtype=tf.int32)p = tf.placeholder(shape=[3], dtype=tf.int32)v_ = gather_indexes_2d(v,p)init = tf.initialize_all_variables()sess = tf.Session()sess.run(init)print(sess.run(v_,feed_dict={p:[1,1,0]}))

打印结果[1 3 4]

rank3的情况:

import tensorflow as tfdef gather_indexes_3d(sequence_tensor, positions): sequence_shape = sequence_tensor.shape.as_list() batch_size = sequence_shape[0] seq_length = sequence_shape[1] width = sequence_shape[2] flat_offsets = tf.reshape( tf.range(0, batch_size, dtype=tf.int32) * seq_length, [-1]) flat_positions = tf.reshape(positions + flat_offsets, [-1]) flat_sequence_tensor = tf.reshape(sequence_tensor, [batch_size * seq_length, width]) output_tensor = tf.gather(flat_sequence_tensor, flat_positions) return output_tensorv = tf.constant([[[1,1],[2,2],[3,3]],[[4,4],[5,5],[6,6]]]) # [2,3,2]p = tf.placeholder(shape=[2], dtype=tf.int32)v_ = gather_indexes_3d(v,p)init = tf.initialize_all_variables()sess = tf.Session()sess.run(init)print(sess.run(v_,feed_dict={p:[1,0]}))

打印结果 [[2 2] [4 4]]

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:transformer里的attention mask产生的 单向双向效果和xlnet里的效果
下一篇:tf.pad 实例 与 使用
相关文章

 发表评论

暂时没有评论,来抢沙发吧~