Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Softmax matrix to 0/1 (OneHot) encoded matrix?

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:

  • Adjust Single Value within Tensor -- TensorFlow
like image 880
Davor Josipovic Avatar asked Jul 29 '16 08:07

Davor Josipovic


Video Answer


1 Answers

Why not combine tf.argmax() with tf.one_hot().

Y = tf.one_hot(tf.argmax(t, dimension = 1), depth = 2)

like image 65
chasep255 Avatar answered Sep 20 '22 21:09

chasep255