Python Tensorflow获取张量中的值索引
给定一个矩阵和向量,我想找到矩阵对应行中值的索引Python Tensorflow获取张量中的值索引,python,matrix,tensorflow,Python,Matrix,Tensorflow,给定一个矩阵和向量,我想找到矩阵对应行中值的索引 m = tf.constant([[0, 2, 1],[2, 0, 1]]) # matrix y = tf.constant([1,2]) # values whose indices should be found 理想输出为[2,0],因为y的第一个值1位于m的第一个向量的索引2处。y的第二个值2位于第二个向量m的索引0处 我只找到一个解决方案。但我不知道有没有更好的 m = tf.constant([[0, 2, 1],[2, 0,
m = tf.constant([[0, 2, 1],[2, 0, 1]]) # matrix
y = tf.constant([1,2]) # values whose indices should be found
理想输出为[2,0],因为y的第一个值1位于m的第一个向量的索引2处。y的第二个值2位于第二个向量m的索引0处 我只找到一个解决方案。但我不知道有没有更好的
m = tf.constant([[0, 2, 1],[2, 0, 1]]) # matrix
y = tf.constant([1,2]) # values whose indices should be found
y = tf.reshape(y, (y.shape[0], 1)) # [[1], [2]]
cols = tf.where(tf.equal(m, y))[:,-1] # [2,0]
init = tf.global_variables_initializer()
with tf.Session() as sess:
init.run()
print(sess.run(cols))
以上输出:
[2,0]
如果打印结果的输出,这将更有帮助