我在gensim中有一个word2vec模型,训练超过98892个文档.对于句子数组中不存在的任何给定句子(即我训练模型的集合),我需要用该句子更新模型,以便下次查询它会给出一些结果.我是这样做的:
new_sentence = ['moscow', 'weather', 'cold']
model.train(new_sentence)
Run Code Online (Sandbox Code Playgroud)
并将其打印为日志:
2014-03-01 16:46:58,061 : INFO : training model with 1 workers on 98892 vocabulary and 100 features
2014-03-01 16:46:58,211 : INFO : reached the end of input; waiting to finish 1 outstanding jobs
2014-03-01 16:46:58,235 : INFO : training on 10 words took 0.1s, 174 words/s
Run Code Online (Sandbox Code Playgroud)
现在,当我使用类似的new_sentence查询大多数肯定(as model.most_similar(positive=new_sentence)
)时,它会发出错误:
Traceback (most recent call last):
File "<pyshell#220>", line 1, in <module>
model.most_similar(positive=['moscow', 'weather', 'cold'])
File "/Library/Python/2.7/site-packages/gensim/models/word2vec.py", line 405, in most_similar
raise KeyError("word '%s' not in vocabulary" % word)
KeyError: "word 'cold' not in vocabulary"
Run Code Online (Sandbox Code Playgroud)
这表明"冷"这个词不是我训练过的词汇的一部分(我是对的)?
所以问题是:如何更新模型,以便它给出给定新句子的所有可能的相似性?
Rad*_*dim 23
ksi*_*ndi 15
从gensim 0.13.3开始,可以使用gensim进行Word2Vec的在线培训.
model.build_vocab(new_sentences, update=True)
model.train(new_sentences)
Run Code Online (Sandbox Code Playgroud)
如果您的模型是使用C工具load_word2vec_format生成的,则无法更新该模型.请参阅有关在线培训Word2Vec教程的word2vec教程部分:
请注意,无法使用C工具load_word2vec_format()生成的模型恢复训练.您仍然可以使用它们进行查询/相似性,但那里缺少对培训至关重要的信息(词汇树).
归档时间: |
|
查看次数: |
21671 次 |
最近记录: |