This way, you can get from m to r through rows and cols as follows:import numpy as npimport tensorflow as tfm = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])rows = np.array([[0, 1], [0, 1], [1, 0], [0, 0]])cols = np.array([[2, 1], [1, 2], [0, 2], [0, 0]])x = tf.placeholder('float32', (None, None))idx1 = tf.placeholder('int32', (None, None))idx2 = tf.placeholder('int32', (None, None))result = tf.gather_nd(x, tf.stack((idx1, idx2), -1))with tf.Session() as sess: r = sess.run(result, feed_dict={ x: m, idx1: rows, idx2: cols, })print(r)输出:[[ 3. 6.] [ 2. 7.] [ 5. 3.] [ 1. 1.]] 这篇关于TensorFlow - 类似 numpy 的张量索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-03 10:07