I have two rank-2 tensors arr1
and arr2
of shape m
by n
. The tensor arr2
is boolean; precisely one entry in each of its rows is True
. I want to extract a new rank-1 tensor arr3
of length m
, where the i
th entry of arr3
is equal to the entry from the i
th row of arr1
corresponding to where the i
th row of arr2
is equal to True
.
In numpy
, I can do this as follows:
arr1 = np.array([[1,2],
[3,4]])
arr2 = np.array([[0,1],
[1,0]], dtype="bool")
arr3 = arr1[arr2]
Can I do something similar in tensorflow
? I know that I could eval()
my tensors and then use numpy
functions, but that seems inefficient.
This question suggests the use of tf.gather
and tf.select
, but it does not deal with collapsing the dimensionality of the output like my question does.