pytorch中的詞向量的使用
在pytorch我們使用nn.embedding進行詞嵌入的工作。
具體用法就是:
import torch
word_to_ix={'hello':0,'world':1}
embeds = torch.nn.Embedding(2,5)
hello_idx=torch.LongTensor([word_to_ix['hello']])
hello_embed = embeds(hello_idx)
print(hello_embed)
print(embeds.weight)
tensor([[ 0.6584, 0.2991, -1.2654, 0.9369, 0.6088]], grad_fn=<EmbeddingBackward>)
Parameter containing:
tensor([[ 0.6584, 0.2991, -1.2654, 0.9369, 0.6088],
[ 0.1922, 1.5374, 0.5737, -0.8007, -0.4896]], requires_grad=True)
在torch.nn.Embedding的源代碼中,它是這么解釋,
This module is often used to store word embeddings and retrieve them using indices.
The input to the module is a list of indices, and the output is the corresponding
word embeddings.
對於這個,我的理解是這樣的torch.nn.Embedding 是一個矩陣類,當我傳入參數之后,我可以得到一個矩陣對象,比如上面代碼中的
embeds = torch.nn.Embedding(2,5) 通過這個代碼,我就獲得了一個兩行三列的矩陣對象embeds。這個時候,矩陣對象embeds的輸入就是一個索引列表(當然這個列表
應該是longtensor格式,得到的結果就是對應索引的詞向量)
我們這里有一點需要格外注意,在上面的結果中,有個這個東西 requires_grad=True
我在開始接觸pytorch的時候,對embedding的一個疑惑就是它是如何定義自動更新的。因為現在我們得到的這個詞向量是隨機初始化的結果,
在后續神經網絡反向傳遞過程中,這個參數是需要更新的。
這里我想要點出一點來,就是詞向量在這里是使用標准正態分布進行的初始化。我們可以通過查看源代碼來進行驗證。
在源代碼中
if _weight is None:
self.weight = Parameter(torch.Tensor(num_embeddings, embedding_dim)) ##定義一個Parameter對象
self.reset_parameters() #隨后對這個對象進行初始化
...
...
def reset_parameters(self): #標准正態進行初始化
init.normal_(self.weight)
if self.padding_idx is not None:
with torch.no_grad():
self.weight[self.padding_idx].fill_(0)