Tensorflow: How to get all possible indices for a tensor

Viewed 137

I'm trying to populate a 6D sparse tensor in tensorflow with data from a 4D dense tensor. Since this is more complicated than the builtin sparse to dense function can handle, I'm building this myself, utilizing the SparseTensor constructor which takes a 2D tensor of indices. In my case this tensor would have dimension [num_elements, 6] What I would like to do is get all possible indices of the dense tensor in the form of [num_elements, 4], which I can then concatenate with a separate tensor of dimensions [num_elements, 2] to get my 6D indices. The only problem is I'm unsure of how to get every valid index in the 4D tensor. I can think of 2 approaches, either directly obtaining it from some part of the API I'm missing, or generating it by creating a tensor with all possible permutations. In fact assuming fixed input size this array of indices will always be the same and thus could be a constant. Any suggestions for implementing either approach?

1 Answers

From the source code of the from dense function I found this

indices = array_ops.where_v2(
        math_ops.not_equal(tensor, array_ops.constant(0, tensor.dtype)))

which should meet my needs.

Related