lasagne embedding layer理解

本文详细介绍了Lasagne库中的EmbeddingLayer,一种用于词嵌入的层,它接受索引向量作为输入并输出相应的嵌入向量。通过一个具体示例,展示了如何设置层的参数,如词汇表大小和嵌入向量长度,并使用Theano进行函数计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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=051016112712381349143×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 的。

References

  1. Docs » lasagne.layers » Embedding layers
  2. Lasagne
  3. Theano
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值