我的目标是找到短语的向量表示。下面是我所拥有的代码,它使用Word2Vec库提供的GenSim模型部分地用于大写图。
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_rule、min_count,但它们似乎不影响剪枝。
PS -我知道要查找的二进制图需要用下划线而不是空格来表示,也就是说调用我的函数的正确方法是bigram2vec(unigrams, 'this_report')。
发布于 2016-03-16 11:49:08
由于GenSim支持论坛的进一步澄清,解决方案是为正在生成的Phrases设置适当的min_count和threshold值(有关Phrases类中这些参数的详细信息,请参阅文档 )。修正后的解决方案代码如下。
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 []https://stackoverflow.com/questions/36013137
复制相似问题