给定一个矩阵和向量,我想找到矩阵对应行中的值的索引。
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]
关于python - Tensorflow 获取张量中值的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47759777/