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 向量:
然後再乘,最後結果就是 的。