2017-08-27 45 views
2

我用自己的詞彙訓練word2vec時出現錯誤。我也不明白爲什麼會發生。如何用你自己的詞彙訓練word2vec

代碼:

from gensim.models import word2vec 
import logging 
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO) 

sentences = word2vec.LineSentence('test_data') 

model = word2vec.Word2Vec(sentences, size=20) 
model.build_vocab(sentences,update=True) 
model.train(sentences) 

print model.most_similar(['course']) 

它拋出一個錯誤

2017-08-27 16:50:04,590 : INFO : precomputing L2-norms of word weight vectors 
Traceback (most recent call last): 
    File "tryword2vec.py", line 23, in <module> 
    print model.most_similar(['course']) 
    File "/usr/local/lib/python2.7/dist-packages/gensim/models/word2vec.py", line 1285, in most_similar 
    return self.wv.most_similar(positive, negative, topn, restrict_vocab, indexer) 
    File "/usr/local/lib/python2.7/dist-packages/gensim/models/keyedvectors.py", line 97, in most_similar 
    raise KeyError("word '%s' not in vocabulary" % word) 
KeyError: "word 'course' not in vocabulary" 

TEST_DATA包含:

工程證書學士是一門課程。 M.Tech是一門課程。我是一門課程。 B.Tech是一門課程。文學學士是一門課程。時裝設計是一門 課程。多媒體是一門課程。機械ENGG是一門課程。計算機 科學是一門課程。電子是一個線索。工程是一門課程。 MBA是一門課程。 BBA是一門課程。

任何幫助表示讚賞?

回答

0

您沒有收到錯誤的原因是因爲單詞當然不在詞彙表中。而現在的詞是當然。

有一段時間「。」在課程結束時。

檢查你的詞彙量 model.wv.vocab

{u'a': <gensim.models.keyedvectors.Vocab at 0x7fe086c461d0>, 
u'course.': <gensim.models.keyedvectors.Vocab at 0x7fe0b4704f90>, 
u'is': <gensim.models.keyedvectors.Vocab at 0x7fe086ba0d10>} 

,做隱藏API鍵。

+0

沒錯。爲什麼只有'一個','課程','是'在詞彙中可見,而不是Engg的學士學位,B.Tech,ME等。有什麼方法可以檢查它。 –

相關問題