我阅读以下代码来学习doc2vec模型。每个文档被定义为两行之间的文本/行:
这是我的代码:
path='/home/work/Step2/test-input/html'
alldocs = [] # will hold all docs in original order
for fname in os.listdir(path):
with open(path+'/'+fname) as alldata:
for line in alldata:
docId= line
print docId
context= alldata.next()
#print context
tokens = gensim.utils.to_unicode(context).split()
end=alldata.next()
alldocs.append(LabeledSentence(tokens[:],[docId]))
model = Doc2Vec(alpha=0.025, min_alpha=0.025) # use fixed learning rate
model.build_vocab(alldocs)
for epoch in range(10):
model.train(alldocs)
model.alpha -= 0.002 # decrease the learning rate
model.min_alpha = model.alpha # fix the learning rate, no decay
# store the model to mmap-able files
model.save(path+'/my_html_model.doc2vec')
但是当我写 model.docvecs [' clueweb09-en0001-01-34238'] 但是当我写 model.docvecs [0]时,我收到了错误我得到了结果。
这是我得到的错误:
Traceback (most recent call last):
File "getLearingDoc.py", line 40, in <module>
print model.docvecs['clueweb09-en0001-01-34238']
File "/home/flashkar/anaconda/lib/python2.7/site-packages/gensim/models/doc2vec.py", line 341, in __getitem__
return self.doctag_syn0[self._int_index(index)]
File "/home/flashkar/anaconda/lib/python2.7/site-packages/gensim/models/doc2vec.py", line 315, in _int_index
return self.max_rawint + 1 + self.doctags[index].offset
KeyError: 'clueweb09-en0001-01-34238'
我没有python和gensim的经验请告诉我如何解决这个问题。
答案 0 :(得分:0)
您确定在培训期间是否提供了完全 'clueweb09-en0001-01-34238'
的标签 - 没有流浪换行/等等?
您可以在model.docvecs.doctags
dict的键或列表model.docvecs.offset2doctag
中查看模型已知的所有字符串文档标签。