我正在研究文字表示的嵌入。在许多dnn库中,它们支持嵌入层。这是一个很好的教程。
Word Embeddings: Encoding Lexical Semantics
Embedding(2, 5)
,但不确定其初始计算。我也不知道如何学习嵌入的权重。
word_to_ix = {"hello": 0, "world": 1}
embeds = nn.Embedding(2, 5) # 2 words in vocab, 5 dimensional embeddings
lookup_tensor = torch.LongTensor([word_to_ix["hello"]])
hello_embed = embeds(autograd.Variable(lookup_tensor))
print(hello_embed)
--------
Variable containing:
-2.9718 1.7070 -0.4305 -2.2820 0.5237
[torch.FloatTensor of size 1x5]
我打破了我的想法来确定。首先,上
嵌入(2,5)
是形状的矩阵
(2, 5)
.
Embedding(2, 5) =
[[0.1,-0.2,0.3,0.4,0.1],
[-0.2,0.1,0.8,0.2,0.3]] # initiated by some function, like random normal distribution
hello
是
[1, 0]
你好
[1, 0].dot(Embedding(2, 5)) = [0.1,-0.2,0.3,0.4,0.1]
. 这实际上是嵌入的第一行。我理解对了吗?
更新
https://github.com/chainer/chainer/blob/adba7b846d018b9dc7d19d52147ef53f5e555dc8/chainer/links/connection/embed_id.py#L58