【发布时间】:2018-07-21 04:50:05
【问题描述】:
在生成器中生成词向量时,如何将它们转换为原始词? 我使用了pytorch内置的nn.Embedding模块来嵌入单词。
【问题讨论】:
在生成器中生成词向量时,如何将它们转换为原始词? 我使用了pytorch内置的nn.Embedding模块来嵌入单词。
【问题讨论】:
由于您没有提供任何代码,我使用下面的代码和 cmets 来回答您的查询。随时为您的特定用例添加更多信息。
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()
【讨论】: