将范围索引转换为张量流中的坐标
Convert range index to coordinates in tensorflow
我要转换这种索引:
a = tf.ones((500, 1000))
a[1:150, 50:700]
进入:
a[idx_x, idx_y]
我们显然可以使用 while
循环来构造这两个向量。但是在 tensorflow 中最有效的方法是什么?
您可以将索引值生成为:
idx_x, idx_y = tf.meshgrid(tf.range(1, 150), tf.range(50, 700), indexing='ij')
那么您可以将它们用作:
b = tf.gather_nd(a, tf.stack([idx_x, idx_y], axis=-1))
我要转换这种索引:
a = tf.ones((500, 1000))
a[1:150, 50:700]
进入:
a[idx_x, idx_y]
我们显然可以使用 while
循环来构造这两个向量。但是在 tensorflow 中最有效的方法是什么?
您可以将索引值生成为:
idx_x, idx_y = tf.meshgrid(tf.range(1, 150), tf.range(50, 700), indexing='ij')
那么您可以将它们用作:
b = tf.gather_nd(a, tf.stack([idx_x, idx_y], axis=-1))