I'm sorry for the long, tedious question title which is hard to understand. Basically, I'd like to implement a function in tensorflow:
e.g. For a tensor A with dimension [10, 10, 7, 1], and an index matrix B = array([[1,3,5],[2,4,6]]). I'd like to extract the elements in A along with axis = 2 (following Python convention, A has 0,1,2,3 four axes) according to the indices in each row of B.
So the results of the example should be a tensor C with dimension [10, 10, 3, 2], where the third dimension is due to selecting elements in A along axis=2 according to indices [1,3,5] or [2,4,6], and the fourth dimension is equal to the first dimension of B (i.e. number of rows of B here), since we did two selections here along that dimension.
Any "tensor favor" clue to implement this in tensorflow, instead of doing it in two steps? I didn't see a way of using tf.gather_nd() or tf.gather() for that. Any idea? Many thanks!
An additional example:
A = [[[1], # A is (3, 5, 1)
[2],
[3],
[4],
[5]]],
[[[10],
[20],
[30],
[40],
[50]]],
[[[100],
[200],
[300],
[400],
[500]]]
B = [[1,4,3], # B is (2,3)
[2,3,5]]
C = [[[1, 2], # C is (3, 3, 2)
[4, 3],
[3, 5]]],
[[[10, 20],
[40, 30],
[30, 50]]],
[[[100, 200],
[400, 300],
[300, 500]]]
A
go in your example? Was it just there to "accomodate" the indexing byB
? viz. ifA
was shaped (10,10,7,5), what shape wouldC
have? – Him