lasagne.layers.EmbeddingLayer 是用来做 word embedding 的,输入 index 向量,输出 embedding 向量。
参数 input_size
是 vocabulary 大小,output_size
是 embedding 向量长。
文档中的例子:
>>> from lasagne.layers import EmbeddingLayer, InputLayer, get_output
>>> import theano
>>> x = T.imatrix()
>>> l_in = InputLayer((3, )) # 对应 vocabulary 大小
>>> W = np.arange(3*5).reshape((3, 5)).astype('float32')
>>> l1 = EmbeddingLayer(l_in, input_size=3, output_size=5, W=W) # 输入维度 3,输出维度 5
>>> output = get_output(l1, x)
>>> f = theano.function([x], output)
>>> x_test = np.array([[0, 2], [1, 2]]).astype('int32')
>>> f(x_test)
array([[[ 0., 1., 2., 3., 4.],
[ 10., 11., 12., 13., 14.]],
[[ 5., 6., 7., 8., 9.],
[ 10., 11., 12., 13., 14.]]], dtype=float32)
f(x_test)
就是 和 相乘,但先将 转成 one-hot 向量:
然后再乘,最后结果就是 的。