2016-07-07 97 views
1

我用MySentences類從目錄中的所有文件中提取句子,並使用這個句子進行訓練a word2vec模型。 我的數據集沒有標籤。Gensim Doc2Vec - 將語料庫句子傳遞給Doc2Vec函數

class MySentences(object): 
    def __init__(self, dirname): 
     self.dirname = dirname 

    def __iter__(self): 
     for fname in os.listdir(self.dirname): 
      for line in open(os.path.join(self.dirname, fname)): 
       yield line.split() 

sentences = MySentences('sentences') 
model = gensim.models.Word2Vec(sentences) 

現在我想使用該類做出doc2vec模型。我讀了Doc2Vec參考頁。 Doc2Vec()函數獲取的句子作爲參數,但它不接受上述判決變量,並返回錯誤:

AttributeError: 'list' object has no attribute 'words' 

問題是什麼?什麼是該參數的正確類型?

更新:

我認爲,未標記的數據的問題。看來doc2vec需要標記數據。

+0

上面的代碼運行找到我的模型! python 2.7和gensim 0.12 – kampta

+0

你是對的。我想用這個類來創建一個doc2vec模型。 –

回答

1

沒有理由使用額外的類來解決問題。在庫的新更新中,添加了一個新函數TaggedLineDocument以將句子轉換爲向量。

sentences = TaggedLineDocument(INPUT_FILE) 

然後,培養

model = Doc2Vec(alpha=0.025, min_alpha=0.025) 
model.build_vocab(sentences) 

for epoch in range(10): 
    model.train(sentences) 
    model.alpha -= 0.002 
    model.min_alpha = model.alpha 
    print epoch