I hope some one can help me on this. I want to make a tensor from the highest values in dimension -1 of another tensor, with the same size while other values set to zero.
For example, given a tensor X of size [2,3,4,5], assume we want to compute the 2 largest elements in the last channel :
vals, indices = tf.nn.top_k(X, k=2, sorted=False)
Obviously, shape of vals and indices are [2,3,4,2].
Now, we want to make the tensor Y, with shape [2,3,4,5] while only 2 values in each channel with location of indices having values in vals and the rest set to zero. I think I should use tf.sparse_to_dense, but I do not know how exactly.
Second Question : What if the dimension 0 is the number of batches and it is dynamic, i.e., it is not given :
X.shape = (?,3,4,5)
Note : This is useful in making spatially sparse layers and sparse auto-encoders.
Thanks in advance.