I have a 2D array of values and a 1D array of indices. I want to get the value of each index at each element of the 2D array using tf.gather_nd or some other numpy or tensorflow command like so:
2D_array = np.arange(27).reshape((9, 3))
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17],
[18, 19, 20],
[21, 22, 23],
[24, 25, 26]])
1D_array = np.random.randint(low=0, high=3, size=9)
array([2, 2, 0, 2, 0, 1, 0, 1, 0])
output
[2, 5, 6, 11, 12, 16, 18, 22, 24]
What I currently have is this mess:
output = tf.gather_nd(a, list(zip(np.arange(len(b)), b)))
<tf.Tensor: shape=(9,), dtype=int32, numpy=array([ 2, 5, 6, 11, 12, 16, 18, 22, 24])>
A cleaner solution that avoids using zip and list would be greatly appreciated, as I don't think I'm currently utilizing tf.gather_nd completely correctly.