Average Word Vectors就是簡單的對句子中的所有詞向量取平均。是一種簡單有效的方法,但缺點也是沒有考慮到單詞的順序。
TF-IDF Weighted word vectors是指對句子中的所有詞向量根據TF-IDF權重加權求和,是常用的一種計算sentence embedding的方法,在某些問題上表現很好,相比於簡單的對所有詞向量求平均,考慮到了TF-IDF權重,因此句子中更重要的詞佔得比重就更大。但缺點也是沒有考慮到單詞的順序。
Doc2vec模型是受到了Word2Vec模型的啟發。Word2Vec預測詞向量時,預測出來的詞是含有詞義的,Doc2vec中也是構建了相同的結構,所以Doc2vec克服了詞袋模型中沒有語義的缺點。假設現在存在訓練樣本,每個句子是訓練樣本,和Word2Vec一樣,Doc2vec也有兩種訓練方式,一種是分布記憶的段落向量(Distributed Memory Model of Paragraph Vectors, PV-DM)類似於Word2Vec中的CBOW模型,另一種是分布詞袋版本的段落向量(Distributed Bag of Words version of Paragraph Vector,PV-DBOW)類似於Word2Vec中的Skip-gram模型。
Doc2vec相對於Word2vec不同之處在於,在輸入層增添了一個新的句子向量Paragraph vector,Paragraph vector可以被看作是另一個詞向量,它扮演了一個記憶角色。Average Word Vectors中,使用Word2Vec訓練詞向量,因為每次訓練只會截取句子中一小部分詞訓練,而忽略了除了本次訓練詞以外該句子中的其他詞,這樣僅僅訓練出來每個詞的向量表達,句子只是每個詞的向量累加在一起取平均的一種表達。正如上面所說的Average Word Vectors的缺點,忽略了文本的詞序問題。而Doc2vec中的Paragraph vector則彌補了這方面的不足,它每次訓練也是滑動截取句子中一小部分詞來訓練,Paragraph Vector在同一個句子的若干次訓練中是共享的,所以同一句話會有多次訓練,每次訓練中輸入都包含Paragraph vector。它可以被看作是句子的主旨,有了它,該句子的主旨每次都會被作為輸入的一部分來訓練。這樣每次訓練過程中,不光是訓練了詞,得到了詞向量。同時隨著一句話每次滑動取若干詞訓練的過程中,作為每次訓練的輸入層一部分的共享Paragraph vector,該向量表達的主旨會越來越準確。Doc2vec中PV-DM模型具體的訓練過程和Word2Vec中的CBOW模型訓練方式相同,這裡就不在重複講解了,不明白Word2Vec原理可以看我的這篇文章:深入理解Word2Vec原理解析。
這個段落向量或句向量也可以認為是一個單詞,它的作用相當於是上下文的記憶單元或者是這個段落的主題,所以我們一般叫這種訓練方法為Distributed Memory Model of Paragraph Vectors(PV-DM)。在訓練的時候我們固定上下文的長度,用滑動窗口的方法產生訓練集。段落向量或句向量在該上下文中共享。
訓練模型,在已知的訓練數據中得到詞向量, softmax的參數和,以及段落向量或句向量。推斷過程(inference stage),對於新的段落,得到其向量表達。具體地,在矩陣中添加更多的列,在固定,,的情況下,利用上述方法進行訓練,使用梯度下降的方法得到新的,從而得到新段落的向量表達。2.2 Distributed Bag of Words version of Paragraph Vector
另外一種訓練方法是忽略輸入的上下文,讓模型去預測段落中的隨機一個單詞。就是在每次迭代的時候,從文本中採樣得到一個窗口,再從這個窗口中隨機採樣一個單詞作為預測任務,讓模型去預測,輸入就是段落向量。我們稱這種模型為 Distributed Bag of Words version of Paragraph Vector(PV-DBOW),如下圖所示:
# ---Let’s start implementing--# Import all the dependenciesfrom gensim.models.doc2vec import Doc2Vec, TaggedDocumentfrom nltk.tokenize import word_tokenizeimport nltk nltk.download() # ---Let’s prepare data for training our doc2vec model--data = ["I love machine learning. Its awesome.", "I love coding in python", "I love building chatbots", "they chat amagingly well"] tagged_data = [TaggedDocument(words=word_tokenize(_d.lower()), tags=[str(i)]) for i, _d in enumerate(data)] # ---Lets start training our model--max_epochs = 100vec_size = 20alpha = 0.025 model = Doc2Vec(size=vec_size, alpha=alpha, min_alpha=0.00025, min_count=1, dm=1) '''Note: dm defines the training algorithm. If dm=1 means『distributed memory』(PV-DM) and dm =0 means『distributed bag of words』(PV-DBOW). Distributed Memory model preserves the word order in a document whereas Distributed Bag of words just uses the bag of words approach, which doesn’t preserve any word order.''' model.build_vocab(tagged_data) for epoch in range(max_epochs): print('iteration {0}'.format(epoch)) model.train(tagged_data, total_examples=model.corpus_count, epochs=model.iter) # decrease the learning rate model.alpha -= 0.0002 # fix the learning rate, no decay model.min_alpha = model.alpha model.save("d2v.model")print("Model Saved") # ---Lets play with it--from gensim.models.doc2vec import Doc2Vec model = Doc2Vec.load("d2v.model")# to find the vector of a document which is not in training datatest_data = word_tokenize("I love chatbots".lower())v1 = model.infer_vector(test_data)print("V1_infer", v1) # to find most similar doc using tagssimilar_doc = model.docvecs.most_similar('1')print(similar_doc) # to find vector of doc in training data using tags or in other words, printing the vector of document at index 1 in training dataprint(model.docvecs['1'])
【1】Le Q, Mikolov T. Distributed representations of sentences and documents[C]//International conference on machine learning. 2014: 1188-1196. 【2】基於Doc2vec訓練句子向量 - 灰灰的文章 - 知乎https://zhuanlan.zhihu.com/p/36886191 【3】doc2vec原理及實踐,地址:https://blog.csdn.net/John_xyz/article/details/79208564 【4】https://medium.com/@mishra.thedeepak/doc2vec-simple-implementation-example-df2afbbfbad5