【问题标题】:How do I get words from an embedded vector?如何从嵌入向量中获取单词?
【发布时间】:2018-07-21 04:50:05
【问题描述】:

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

【问题讨论】:

    标签: nlp pytorch embedding


    【解决方案1】:

    由于您没有提供任何代码,我使用下面的代码和 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()   
    

    【讨论】:

      猜你喜欢
      • 2018-12-16
      • 1970-01-01
      • 2021-05-04
      • 2021-05-25
      • 2021-07-24
      • 2020-03-28
      • 2016-09-17
      • 2020-10-23
      • 2021-10-20
      相关资源
      最近更新 更多