如何获得one-hot向量的密集表示 [英] How to get a dense representation of one-hot vectors

查看:33
本文介绍了如何获得one-hot向量的密集表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个 Tensor 包含:

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

如何以本机方式获得密集表示(不使用 numpy 或迭代)?

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

[2,1,0]

tf.one_hot() 做相反的事情,还有 tf.sparse_to_dense() 似乎可以做到,但我不知道如何使用它.

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.

推荐答案

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]

这篇关于如何获得one-hot向量的密集表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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