0

I need to implement a function that converts a TF tensor of probability distributions to sparse integer encoding of categories. I don't think I'm going about it the right way. How do I get the expected output that I'm looking for?

My function:

def into_sparse(x):
    sparseData = tf.sparse.from_dense(x)
    return sparseData

Function call:

y_sparse = into_sparse(y)

Input (y):

tf.Tensor(
[[0.9933 0.     0.0067]
 [0.5065 0.1863 0.3072]
 [0.0751 0.9148 0.0102]
 [0.4307 0.0432 0.5261]], shape=(4, 3), dtype=float32)

Output:

<tensorflow.python.framework.sparse_tensor.SparseTensor at 0x7f3543aaa610>

Expected Output:

<tf.Tensor: shape=(4,), dtype=int64, numpy=array([0, 0, 1, 2])>

1 Answer 1

1

You can use argmax. That will give you the index that contains the maximum value in a dimension.

tf.math.argmax(y_sparse, axis=-1)

See https://www.tensorflow.org/api_docs/python/tf/math/argmax

This is different from the sparse arrays in tf.sparse. Those arrays have many zeros, and storing them in a sparse format rather than dense can save space.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.