Generate a tensor from two matrices full of indexes (using tf.gather)

What is the Tensorflow equivalent of this Numpy code? The line in question is the last line of this code snippet. I think tf.gather or tf.gather_nd could be used, but I not sure how to combine the two matrices, indexMat1 and indexMat2, into coordinate pairs to pass into tf.gather.

pad_img = input
#pad_img.shape[0] == num of images
#pad_img.shape[1] == num of channels (i.e. RGB colors)
#pad_img.shape[2] == num pixels for image width
#pad_img.shape[3] == num pixels for image height

make select_img

each column of select_img is a flattened convolution window

select_img includes all of the possible convolution windows in pad_img

indexMat2, indexMat3 are coordinate pairs that map pixels from pad_img to select_img

indexMat2 = index matrix calculation
indexMat3 = index matrix calculation

########################################

What is the TF equivalent of the next Numpy line?

########################################
select_img=pad_img[:, :, indexMat2, indexMat3]

Hi @scott_c,

Sorry for the delay in response.

Equivalent TF code would be select_img = tf.gather_nd(pad_img, tf.stack([tf.range(tf.shape(pad_img)[0])[:, tf.newaxis], indexMat2, indexMat3], axis=-1)) where it gathers elements from the tensor pad_img based on indices formed by combining the batch indices from tf.range with the values from indexMat2 and indexMat3.

In addition, use tf.gather for getting entire rows or slices from a tensor and use tf.gather_nd for getting specific elements using multidimensional index. Kindly refer this gist for sample implementation.

Hope this helps.Thank You.