在生成器中生成单词向量时,如何将它们转换为原始单词? 我使用pytorch内置的nn.Embedding模块嵌入单词。
答案 0 :(得分:0)
由于您未提供任何代码,因此我在下面的代码中使用注释来回答您的查询。随时为您的特定用例添加更多信息。
import torch
# declare embeddings
embed = torch.nn.Embedding(5,10)
# generate embedding for word [4] in vocab
word = torch.tensor([4])
# search function for searching through embedding
def search(vector, distance_fun):
weights = embed.weight
min = torch.tensor(float('inf'))
idx = -1
v, e = weights.shape
# each vector in embeding is corresponding to one of the word.
# use a distance function to compare with vector
for i in range(v):
dist = distance_fun(vector, weights[i])
if (min<dist):
min = dist
idx = i
return i
# searching with squared distance
search(word, lambda x,y: ((x-y)**2).sum()