两个形状为T1 = N * D,T2 = M * D的张量流张量; M <N。T1在T2中有行。为T2中的每一行查找T1张量中的行索引

时间:2019-02-01 14:04:09

标签: python tensorflow

我有两个张量T1(N * D尺寸)和T2(M * D尺寸)(M小于N)。 T2行保证在T1中。对于T2的每一行,有没有办法找到T1的其中行相匹配的指数? 我能够使用急切的执行解决问题。

import tensorflow as tf
import numpy as np
tf.enable_eager_execution()
x = tf.random_normal([15,3])
y = x[:2] # first two entries 
y= tf.concat([y,x[8:9]], 0)
output = []
for row in x:
   if row.numpy() in y.numpy():
     output.append(True)
   else:
     output.append(False)

有人可以在不急于执行的情况下提供执行帮助吗? 如果T1和T2是批处理,我们如何执行相同的操作?即T1-B * N * D和T2-B * M * D

P.S。我们如何在Tensorflow中搜索一行?

1 个答案:

答案 0 :(得分:0)

这是您可以执行的操作:

import tensorflow as tf

def find_row_indices(t1, t2):
    # Compare every pair of rows
    eq = tf.equal(tf.expand_dims(t1, -3), tf.expand_dims(t2, -2))
    # Find where all the elements in two rows match
    matches = tf.reduce_all(eq, axis=-1)
    # Find indices where match occurs
    idx = tf.argmax(tf.cast(matches, tf.uint8), axis=-1)
    # Find where there has been no match
    has_match = tf.reduce_any(matches, axis=-1)
    # Return match index of -1 if no match found
    return tf.where(has_match, idx, -tf.ones_like(idx))

# Test
with tf.Graph().as_default():
    tf.set_random_seed(100)
    x = tf.random_normal([15, 3])
    y = x[:2]
    y = tf.concat([y, x[8:9]], 0)
    idx = find_row_indices(x, y)
    with tf.Session() as sess:
        print(sess.run(idx))
        # [0 1 8]

它具有平方空间和存储成本,因为它将每一对行相互比较,因此在某些情况下拥有两个非常大的输入可能会出现问题。另外,如果存在多个匹配索引,则此方法不能保证将返回其中一个。

编辑:上面的函数也可以用于具有更大初始尺寸的数组。例如:

import tensorflow as tf

with tf.Graph().as_default():
    tf.set_random_seed(100)
    x = tf.random_normal([4, 15, 3])
    y = tf.gather_nd(x, [[[ 0,  3], [ 0,  6], [ 0,  2]],
                         [[ 1, 10], [ 1,  5], [ 1, 12]],
                         [[ 2,  8], [ 2,  1], [ 2,  0]],
                         [[ 3,  9], [ 3, 14], [ 3,  4]]])
    idx = find_row_indices(x, y)
    with tf.Session() as sess:
        print(sess.run(idx))
        # [[ 3  6  2]
        #  [10  5 12]
        #  [ 8  1  0]
        #  [ 9 14  4]]