简体   繁体   English

从索引列表到单热矩阵

[英]From list of indices to one-hot matrix

What is the best (elegant and efficient) way in Theano to convert a vector of indices to a matrix of zeros and ones, in which every row is the one-of-N representation of an index? 在Theano中将索引向量转换为零和1的矩阵的最佳(优雅和有效)方法是什么,其中每一行都是索引的一个N表示?

v = t.ivector()  # the vector of indices
n = t.scalar()   # the width of the matrix
convert = <your code here>
f = theano.function(inputs=[v, n], outputs=convert)

Example: 例:

n_val = 4
v_val = [1,0,3]
f(v_val, n_val) = [[0,1,0,0],[1,0,0,0],[0,0,0,1]]

I didn't compare the different option, but you can also do it like this. 我没有比较不同的选项,但你也可以这样做。 It don't request extra memory. 它不要求额外的内存。

import numpy as np
import theano

n_val = 4
v_val = np.asarray([1,0,3])
idx = theano.tensor.lvector()
z = theano.tensor.zeros((idx.shape[0], n_val))
one_hot = theano.tensor.set_subtensor(z[theano.tensor.arange(idx.shape[0]), idx], 1)
f = theano.function([idx], one_hot)
print f(v_val)[[ 0.  1.  0.  0.]
 [ 1.  0.  0.  0.]
 [ 0.  0.  0.  1.]]

It's as simple as: 它很简单:

convert = t.eye(n,n)[v]

There still might be a more efficient solution that doesn't require building the whole identity matrix. 仍然可能存在一种更有效的解决方案,不需要构建整个单一矩阵。 This might be problematic for large n and short v's. 这对于大n和短v来说可能是有问题的。

There's now a built in function for this theano.tensor.extra_ops.to_one_hot . 现在有了这个theano.tensor.extra_ops.to_one_hot的内置函数。

y = tensor.as_tensor([3,2,1])
fn = theano.function([], tensor.extra_ops.to_one_hot(y, 4))
print fn()
# [[ 0.  0.  0.  1.]
#  [ 0.  0.  1.  0.]
#  [ 0.  1.  0.  0.]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM