Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tensorflow获取张量中的值索引_Python_Matrix_Tensorflow - Fatal编程技术网

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]

如果打印结果的输出,这将更有帮助