Python 如何选择一个张量中定义的特定值和另一个张量中的数据

Python 如何选择一个张量中定义的特定值和另一个张量中的数据,python,variables,tensorflow,Python,Variables,Tensorflow,我有一个张量变量y tf.shapey=>[140,8],另一个变量x=tf.constant[2,4,5,7],tf.int32 我想为y中的数据选择x中提到的所有行和列[2,4,5,7] 在Matlab中,我可以简单地定义req_data=y[:,x]给我在x中为y数据选择的列。 如何在tensorflow中执行此操作?如果要执行req_data=y[:,x] 首先使用tf.transpose,所以张量的形状是8140 然后使用tf.gather选择数据 因为tf.gather只在轴=0上工

我有一个张量变量y tf.shapey=>[140,8],另一个变量x=tf.constant[2,4,5,7],tf.int32

我想为y中的数据选择x中提到的所有行和列[2,4,5,7]

在Matlab中,我可以简单地定义req_data=y[:,x]给我在x中为y数据选择的列。 如何在tensorflow中执行此操作?

如果要执行req_data=y[:,x] 首先使用tf.transpose,所以张量的形状是8140 然后使用tf.gather选择数据 因为tf.gather只在轴=0上工作,所以先转置 然后调回

a = tf.constant([[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                 [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]])
a_trans = tf.transpose(a)
b = tf.constant([2,4,5,7])
c = tf.gather(a_trans, b)
c_trans = tf.transpose(c)

with tf.Session() as sess:
    print sess.run(c_trans)
    #output [[3  5  6  8]
    #        [13 15 16 18]]