Home > Enterprise >  Tensorflow: find points in one set closest to each point of another set of different size
Tensorflow: find points in one set closest to each point of another set of different size

Time:11-07

I want to find the points in one set (interior) closest to each point of another set (boundary) of different (smaller) size. I can do that like this:

interior = tf.random.uniform(shape=[18,3], minval=-1.0, maxval=1.0)
boundary = tf.random.uniform(shape=[12,3], minval=-1.0, maxval=1.0)

for i in range(12):
    interior[tf.argmin(tf.reduce_sum(tf.math.squared_difference(interior, boundary[i]),1))]

Is there a way to do this faster? For instance, without the for loop, but for all points in the boundary at the same time.

CodePudding user response:

We can use broadcasting,

indices = tf.argmin(tf.reduce_sum(tf.math.squared_difference(tf.expand_dims(interior, 1), tf.expand_dims(boundary, 0)), 2), axis=0)

and then

tf.gather(interior, indices)
  • Related