Suppose I have the following tensor t
as the output of a softmax function:
t = tf.constant(value=[[0.2,0.8], [0.6, 0.4]])
>> [ 0.2, 0.8]
[ 0.6, 0.4]
Now I would like to convert this matrix t
into a matrix that resembles the OneHot encoded matrix:
Y.eval()
>> [ 0, 1]
[ 1, 0]
I am familiar with c = tf.argmax(t)
that would give me the indices per row of t
that should be 1. But to go from c
to Y
seems quite difficult.
What I already tried was converting t
to tf.SparseTensor
using c
and then using tf.sparse_tensor_to_dense()
to get Y
. But that conversion involves quite some steps and seems overkill for the task - I haven't even finished it completely but I am sure it can work.
Is there any more appropriate/easy way to make this conversion that I am missing.
The reason why I need this is because I have a custom OneHot encoder in Python where I can feed Y
. tf.one_hot()
is not extensive enough - doesn't allow custom encoding.
Related questions:
Why not combine tf.argmax() with tf.one_hot().
Y = tf.one_hot(tf.argmax(t, dimension = 1), depth = 2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With