【问题标题】:GenSim Word2Vec unexpectedly pruningGenSim Word2Vec 意外修剪
【发布时间】:2016-03-16 19:49:08
【问题描述】:

我的目标是找到短语的向量表示。下面是我拥有的代码,它部分适用于使用Word2Vec 库提供的Word2Vec 模型的二元组。

from gensim.models import word2vec

def bigram2vec(unigrams, bigram_to_search):
    bigrams = Phrases(unigrams)
    model = word2vec.Word2Vec(sentences=bigrams[unigrams], size=20, min_count=1, window=4, sg=1, hs=1, negative=0, trim_rule=None)
    if bigram_to_search in model.vocab.keys():
        return model[bigram_to_search]
    else:
        return None

问题在于 Word2Vec 模型似乎正在自动修剪一些二元组,即len(model.vocab.keys()) != len(bigrams.vocab.keys())。我试过调整trim_rulemin_count等各种参数,但似乎不影响剪枝。

PS - 我知道要查找的二元组需要使用下划线而不是空格来表示,即调用我的函数的正确方法是 bigram2vec(unigrams, 'this_report')

【问题讨论】:

    标签: gensim word2vec


    【解决方案1】:

    感谢GenSim support forum 的进一步澄清,解决方案是为正在生成的Phrases 设置适当的min_countthreshold 值(有关Phrases 中这些参数的详细信息,请参阅documentation班级)。更正后的解决方案代码如下。

    from gensim.models import word2vec, Phrases
    
    def bigram2vec(unigrams, bigram_to_search):
        bigrams = Phrases(unigrams, min_count=1, threshold=0.1)
        model = word2vec.Word2Vec(sentences=bigrams[unigrams], size=20, min_count=1, trim_rule=None)
        if bigram_to_search in model.vocab.keys():
            return model[bigram_to_search]
        else:
            return []
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-02
      • 2017-03-28
      • 2019-03-15
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      相关资源
      最近更新 更多