I would like to create a function, that for a given 1d-tensor outputs the mask, where on the places, corresponding to the top k values there are 1 and 0 elsewhere.
Namely, I have for example:
tensor = [1, 0, 7, 5, 2, 3] : get_largest_mask(tensor, 3) = [0, 0, 1, 1, 0, 1]
I've created the following function:
def get_largest_mask(tensor, n_to_keep):
# tensor 1-d tensor
values, indices = tf.math.top_k(tensor, k=n_to_keep)
mask = tf.zeros(tf.size(tensor))
mask = tf.tensor_scatter_nd_update(mask, [[idx] for idx in indices], tf.ones(n_to_keep))
return mask
However for the case of interest it works rather slowly, and as I've measured most of the time is dominated by tf.tensor_scatter_nd_update. What would be the faster alternative?
The typical size of tensor is 10^3-10^4 elements and k is of order `10^2-10^3'