tensorflow 将标签向量处理为“多个热编码器" [英] tensorflow manipulate labels vector into "multiple hot encoder"

查看:40
本文介绍了tensorflow 将标签向量处理为“多个热编码器"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能(以一种很好的方式)在 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]

我需要制作一个多热编码器".意思是,对于每一行,我需要 1 到标签的索引减去 1所以所需的结果将是

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]]

谢谢

推荐答案

您可以使用 tf.nn.embeddings_lookup 执行此操作,如下所示:

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)

sess.run(encode_tensors) 的输出:

Output of sess.run(encode_tensors) :

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

希望这有帮助!

这篇关于tensorflow 将标签向量处理为“多个热编码器"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆