Reputation: 73
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])>
Upvotes: 0
Views: 196
Reputation: 19250
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.
Upvotes: 1