Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a dense representation of one-hot vectors

Tags:

tensorflow

Suppose a Tensor containing :

[[0 0 1]
 [0 1 0]
 [1 0 0]]

How to get the dense representation in a native way (without using numpy or iterations) ?

[2,1,0]

There is tf.one_hot() to do the inverse, there is also tf.sparse_to_dense() that seems to do it but I was not able to figure out how to use it.

like image 484
znat Avatar asked Oct 10 '16 18:10

znat


2 Answers

tf.argmax(x, axis=1) should do the job.

like image 148
Kilian Batzner Avatar answered Sep 17 '22 10:09

Kilian Batzner


vec = tf.constant([[0, 0, 1], [0, 1, 0], [1, 0, 0]])
locations = tf.where(tf.equal(vec, 1))
# This gives array of locations of "1" indices below
# => [[0, 2], [1, 1], [2, 0]])

# strip first column
indices = locations[:,1]
sess = tf.Session()
print(sess.run(indices))
# => [2 1 0]
like image 41
Yaroslav Bulatov Avatar answered Sep 21 '22 10:09

Yaroslav Bulatov