Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tensorflow manipulate labels vector into "multiple hot encoder"

Tags:

tensorflow

is it possible (in a nice way, that is) in tensorflow to achieve the next functionality:

assume we have a dense vector of tags

labels = [0,3,1,2,0]

I need to make a "multiple hot encoder" of it. meaning, for each row I need 1's up to the index of the label minus 1 so the required result will be

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

thanks

like image 222
user2717954 Avatar asked Jun 08 '26 20:06

user2717954


1 Answers

You could do this using tf.nn.embeddings_lookup as shown here:

embeddings = tf.constant([[0,0,0], [0,0,1], [0,1,1], [1,1,1]])
labels = [0,3,1,2,0]
encode_tensors = tf.nn.embedding_lookup(embeddings,labels)

Output of sess.run(encode_tensors) :

array([[0, 0, 0],
   [1, 1, 1],
   [0, 0, 1],
   [0, 1, 1],
   [0, 0, 0]], dtype=int32)

Hope this helps !

like image 198
Harsha Pokkalla Avatar answered Jun 10 '26 10:06

Harsha Pokkalla