KeyError: “word 'word' not in vocabulary” in word2vec

半世苍凉 提交于 2020-08-08 06:37:08

问题


I am using word2vec, wiki corpus I trained, what can I do if the word I input not in vocabulary in word2vec?

Test it a bit:

model = word2vec.Word2Vec.load('model/' + 'wiki_chinese_word2vec.model')    
model['boom']

Error:

KeyError("word '%s' not in vocabulary" % word)


回答1:


Use try & except to handle exceptions in Python. try block executes normally. If any exception or error occurs then except block will be executed.

try:
        c = model['boom']
except KeyError:
        print "not in vocabulary"
        c = 0



回答2:


when you say

what can I do if the word I input not in vocabulary in word2vec

I interpret it two ways

1) How to escape this exception: You can use try , catch

2) How to handle this exception: retrain the model with the missing words accommodated again.You have an option to incrementally train the model , with new corpus ,in word2vec, please go through the gensim tutorial in detail.

I presume that "boom" is not available in the corpus that you have passed, hence the trouble



来源:https://stackoverflow.com/questions/41133844/keyerror-word-word-not-in-vocabulary-in-word2vec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!