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)
X
t
e
s
t
=
[
0
2
1
2
]
2
×
2
X_{test}=\left[\begin{matrix}0 & 2 \\ 1 & 2\end{matrix}\right]_{2\times2}
Xtest=[0122]2×2
W
=
[
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
]
3
×
5
W=\left[\begin{matrix}0 & 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 & 9 \\ 10 & 11 & 12 & 13 & 14\end{matrix}\right]_{3\times5}
W=⎣⎡05101611271238134914⎦⎤3×5
f(x_test)
就是
X
t
e
s
t
X_{test}
Xtest 和
W
W
W 相乘,但先将
X
t
e
s
t
X_{test}
Xtest 转成 one-hot 向量:
X
t
e
s
t
′
=
[
[
[
1
0
0
]
[
0
0
1
]
]
[
[
0
1
0
]
[
0
0
1
]
]
]
2
×
2
×
3
X'_{test}=\left[\begin{matrix} \left[\begin{matrix}[1 & 0 & 0] \\ [0 & 0 & 1]\end{matrix}\right] \\ \left[\begin{matrix}[0 & 1 & 0] \\ [0 & 0 & 1]\end{matrix}\right] \end{matrix}\right]_{2\times2\times3}
Xtest′=⎣⎢⎢⎡[[1[0000]1]][[0[0100]1]]⎦⎥⎥⎤2×2×3
然后再乘,最后结果就是
2
×
2
×
5
2\times2\times5
2×2×5 的。