I have an array of xyz coordinates of points of shape (nsamples, npoints, 3)
.
I am trying to build a tensorflow graph that selects the two points closest to the origin.
I've gotten this far
r2 = tf.reduce_sum(tf.pow(centeredxyz, 2), axis=2)
idx = tf.nn.top_k(-r2, 2)[1]
This gives me the indexes of the two closest points in the form of a 2D matrix i.e.
[[3, 15], [6, 2], ...]
of shape (nsamples, 2)
.
How can I use these indexes to get back the points from centeredxyz
?
I tried tf.gather_nd
but it considers that I'm asking for the coordinates of the 15th point of the 3rd sample while I'm asking for the 3rd and 15th point of the first sample, 6th and 2nd of the second sample etc.
I tried creating a tf.range
and stacking it to the indexes to obtain [[0, 3], [0, 15], [1, 6], [1, 2], ...]
but it failed because it cannot create a range of unknown dimensions ValueError: Cannot convert an unknown Dimension to a Tensor: ?
So currently I am quite clueless as to what to try next.
答案 0 :(得分:0)
我设法将一个丑陋的版本拼凑在一起。伤害大脑,但似乎有效。
def gather_second_multicol(data, idx):
nsamples = tf.shape(idx)[0]
nselcol = tf.shape(idx)[1]
idx = tf.reshape(idx, [-1, 1])
range = tf.range(nsamples)
range = tf.tile(tf.expand_dims(range, 0), [nselcol, 1])
range = tf.transpose(range)
range = tf.reshape(range, [-1, 1])
idx = tf.concat([range, idx], 1)
gath = tf.gather_nd(data, idx)
return tf.reshape(gath, [-1, nselcol, 3])
def get_closest(centeredxyz):
r2 = tf.reduce_sum(tf.pow(centeredxyz, 2), axis=2)
idx = tf.nn.top_k(-r2, 2)[1]
closest = gather_second_multicol(centeredxyz, idx)
return closest