Gensim庫之Doc2Vec模型詳解
models.doc2vec – Doc2vec paragraph embeddings: TaggedDocument: 對于輸入的文檔 text,轉換為:TaggedDocument(text, [i])的形式,i為文檔編号 class gensim.models.doc2vec.Doc2Vec( documents=None, 輸入語料庫,是TaggedDocument清單 corpus_file=None, Path to a corpus file in LineSentence format. dm_mean=None, 取0,則使用上下文詞向量的和。取1,使用平均值。 僅當dm在非串聯模式下使用時适用(dm_concat=0)。 dm=1, 選擇訓練算法,dm = 1 使用PV-DM;dm = 0,使用PV-DBOW dbow_words=0, 取1,則與dbow-doc向量訓練同時訓練單詞向量(以Skip-gram的方式); 取0,則僅訓練文檔向量(更快)。 dm_concat=0, 取1時,則使用上下文向量的串聯,取0時求和/平均值; negative = 0, >0,使用負抽樣(噪聲詞在5~20之間);=0,不适用噪聲詞; epochs (int, optional),疊代次數; hs ({1,0}, optional), 取 1, 使用hierarchical softmax . 取 0, 且negative 非0, 使用負抽樣 . dm_tag_count=1, docvecs=None, docvecs_mapfile=None, comment=None, trim_rule=None, callbacks=(), **kwargs)
使用Doc2Vec()方法訓練得到的model中包含以下對象:
(1)wv(Word2VecKeyedVectors):
word2vec對象存儲單詞和向量之間的映射。用于對向量進行查找、距離、相似性計算等操作。
方法:
① closer_than(entity1, entity2)
Get all entities that are closer to entity1 than entity2 is to entity1.
② cosine_similarities(vector_1, vectors_all)
Compute cosine similarities between one vector and a set of other vectors.
③ distance(w1, w2)
Compute cosine distance between two words.
④ distances(word_or_vector, other_words=())
Compute cosine distances from given word or vector to all words in other_words.
If other_words is empty, return distance between word_or_vectors and all words
in vocab.
⑤ get_vector(word)
Get the entity’s representations in vector space, as a 1D numpy array.
⑥ most_similar_cosmul(positive=None, negative=None, topn=10)
Find the top-N most similar words, using the multiplicative combination objective.
⑦ most_similar_to_given(entity1, entities_list)
Get the entity from entities_list most similar to entity1.
⑧ n_similarity(ws1, ws2)
Compute cosine similarity between two sets of words.
⑨ relative_cosine_similarity(wa, wb, topn=10)
Compute the relative cosine similarity between two words given top-n similar words;
⑩ save(path) Save KeyedVectors. load(path)Load KeyedVectors.
⑪ wmdistance(document1, document2)
Compute the Word Mover’s Distance(詞移距離) between two documents.
⑫ word_vec(word, use_norm=False)
Get word representations in vector space, as a 1D numpy array.
(2)docvecs(Doc2VecKeyedVectors):
此對象包含段落向量。記住,這個模型和word2vec之間的唯一差別是,除了詞向量之外,我們還包括段落嵌入
來捕獲段落。
該對象中的方法基本與WV中的方法相同;
(3)vocabulary(Doc2VecVocab):
這個對象表示模型的詞彙表(字典)。除了跟蹤所有獨特的單詞之外,這個對象還提供了額外的功能,比如按頻率
對單詞排序,或者丢棄非常罕見的單詞。
Doc2Vec的方法:
①most_similar(**kwargs) Deprecated, use self.wv.most_similar() instead.
②most_similar_cosmul(**kwargs) Deprecated, use self.wv.most_similar_cosmul() instead.
③n_similarity(**kwargs) Deprecated, use self.wv.n_similarity() instead.