lasagne embedding layer理解

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)

Xtest=[0212]2×2X_{test}=\left[\begin{matrix}0 & 2 \\ 1 & 2\end{matrix}\right]_{2\times2}
W=[01234567891011121314]3×5W=\left[\begin{matrix}0 & 1 & 2 & 3 & 4 \\ 5 & 6 & 7 & 8 & 9 \\ 10 & 11 & 12 & 13 & 14\end{matrix}\right]_{3\times5}
f(x_test) 就是 XtestX_{test}WW 相乘,但先將 XtestX_{test} 轉成 one-hot 向量:
Xtest=[[[100][001]][[010][001]]]2×2×3X'_{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}
然後再乘,最後結果就是 2×2×52\times2\times5 的。

References

  1. Docs » lasagne.layers » Embedding layers
  2. Lasagne
  3. Theano
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章