我正在研究詞表示的嵌入。在許多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
均勻分佈也是普遍的。 – Mehdi