如何从嵌入向量中获取单词?

How do I get words from an embedded vector?

在生成器中生成词向量时,如何将它们转换成原词? 我使用了pytorch内置的nn.Embedding模块来嵌入单词。

由于您没有提供任何代码,我使用下面的代码和注释来回答您的查询。请随时为您的特定用例添加更多信息。

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()