【发布时间】:2018-03-03 12:46:06
【问题描述】:
我正在研究词表示的嵌入。在许多 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]
为了确定,我打破了我的想法。首先,上面的Embedding(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]。然后hello 表示由[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
【问题讨论】:
标签: machine-learning nlp deep-learning word2vec word-embedding