胡正 楊志勇
關(guān)鍵詞: 詞向量; 詞嵌入; 上下文窗口; 自然語言處理; 神經(jīng)網(wǎng)絡(luò); 深度學(xué)習(xí)
中圖分類號: TN912.34?34; TP391.1 ? ? ? ? ? ? 文獻(xiàn)標(biāo)識碼: A ? ? ? ? ? ? ? ? ?文章編號: 1004?373X(2019)06?0146?03
Abstract: The word vector accuracy affects the operation of natural language processing tasks considerably. Word vectors are generated by the means of word embedding. In word embedding methods, the target words and their contexts are treated as inputs of the training. As a result, context determination has an important influence on word embedding. Therefore, the influence of variant context windows on word embedding accuracy is studied by using the word2vec word embedding method in this paper. A series of experiments were carried out according to the context windows with variant widths, offsets and weights. The experimental results show that, the variations of the context windows do not have a significant effect on the overall accuracy of training results, but have a significant effect on various specific words, so it is concluded that quite many words have their own demands in suitability of context windows, so it is difficult for a unified context window to implement the optimal training for all words.
Keywords: word vector; word embedding; context window; natural language processing; neural network; deep learning
近年來,深度學(xué)習(xí)在自然語言處理領(lǐng)域取得了諸多進(jìn)展。在這些自然語言處理任務(wù)中,都將詞向量作為其基礎(chǔ)。詞嵌入(Word Embedding)是一類語言建模技術(shù),通過詞嵌入可將詞匯表中的單詞和短語映射到n維詞向量上(維度一般遠(yuǎn)小于詞匯表)。2000年Bengio等人采用詞嵌入并結(jié)合機(jī)器學(xué)習(xí)取得了較為突出的研究成果[1]。將詞向量作為輸入,可以很好地實(shí)現(xiàn)語法分析[2]、語義分析[3]、命名實(shí)體識別[4]等任務(wù)。詞嵌入的實(shí)現(xiàn)方法有若干種,包括用神經(jīng)網(wǎng)絡(luò)產(chǎn)生[5?6]、對詞共生矩陣降維[7?9]、通過概率模型計(jì)算[10]以及顯式上下文表征[11]等。其共同點(diǎn)都是依據(jù)目標(biāo)單詞與上下文單詞的出現(xiàn)概率或次數(shù)來構(gòu)建詞向量的。
其中word2vec是目前被廣泛應(yīng)用的基于神經(jīng)網(wǎng)絡(luò)的詞嵌入方法,其特點(diǎn)是借由神經(jīng)網(wǎng)絡(luò)隱含層來發(fā)現(xiàn)數(shù)據(jù)特征,即產(chǎn)生詞向量各個(gè)維度中的數(shù)據(jù),這是一種在非監(jiān)督學(xué)習(xí)中常用的特征取得方式。word2vec包含兩個(gè)可選算法:Skip?gram和CBOW[5,12],所得到的詞向量不僅可以表征單詞之間的相似性,亦能表征單詞對之間的關(guān)系、對應(yīng)關(guān)系[5]。如“man,woman”和“king,queen”,其詞向量的關(guān)系可表示為:[Vecking-Vecman≈Vecqueen-Vecwoman,]即進(jìn)行[Vecking-Vecman+Vecwoman]的詞向量運(yùn)算后,其結(jié)果最為接近的詞向量是[Vecqueen]。
這種向量運(yùn)算(稱其為類比運(yùn)算)可以在單詞的類比關(guān)系、對應(yīng)關(guān)系上得到很好的驗(yàn)證[13],一般來說其結(jié)果的正確率[11,14]可以達(dá)到40%~60%。類比運(yùn)算是否正確體現(xiàn)了詞向量表征語義是否準(zhǔn)確。通過在各種變體上下文窗口下進(jìn)行詞嵌入,并使用類比運(yùn)算研究其所得到的詞向量的影響。
詞嵌入將目標(biāo)單詞及其上下文作為訓(xùn)練的輸入,上下文所在的連續(xù)文本區(qū)間也就是上下文窗口。在訓(xùn)練中,一般對各個(gè)上下文詞向量賦予一個(gè)權(quán)值[p∈0,1],且權(quán)值隨著距離目標(biāo)單詞的距離變大而變小。目標(biāo)單詞一般位于窗口的中心,設(shè)目標(biāo)單詞的上文或下文單詞數(shù)[w]為上下文窗口寬度。則包括目標(biāo)單詞在內(nèi),上下文窗口內(nèi)的單詞數(shù)為[2w+1]。在word2vec中,窗口內(nèi)單詞的權(quán)值[p]與其距離[d]的關(guān)系為:[p=w-dw],且[d∈[0,w-1]]。當(dāng)它與目標(biāo)單詞相鄰時(shí):[d=0,p=1;]而距離最遠(yuǎn)時(shí)[d=w-1]且[p=1w],這樣的窗口稱為遞減權(quán)值窗口,如果權(quán)值不變則稱為固定權(quán)值窗口。
在詞嵌入中,確定了語料庫和上下文窗口也就確定了訓(xùn)練的輸入,因此上下文窗口的選擇將對詞嵌入的結(jié)果起到至關(guān)重要的影響。在各種變體窗口下,詞嵌入訓(xùn)練結(jié)果的變化是主要研究內(nèi)容。本文在一系列不同的上下文窗口寬度和形態(tài)的條件下進(jìn)行了詞嵌入訓(xùn)練,并且通過類比測試來評價(jià)詞向量的準(zhǔn)確性。
本文使用目前廣泛采用的維基百科英文語料庫進(jìn)行詞嵌入,其開放性使得相關(guān)研究的可重復(fù)性較好。語料庫是在對維基百科英文頁面?zhèn)浞葸M(jìn)行文本無關(guān)信息篩除得到,其文件大小為12.1 GB,包含2 113 849 195個(gè)單詞。
使用基于Python語言的word2vec來進(jìn)行實(shí)驗(yàn),詞匯表為語料庫中出現(xiàn)次數(shù)不小于10 000的單詞所構(gòu)成。采用的測試數(shù)據(jù)為word2vec項(xiàng)目的類比數(shù)據(jù)集,共19 545個(gè)類比。該數(shù)據(jù)集是詞向量中廣泛被使用的一個(gè)測試數(shù)據(jù)集,其中的類比關(guān)系包含:首都與國家(如:“Athens,Greece與Beijing,China”),角色關(guān)系(如:“boy,girl與brother,sister”),形容詞與副詞(如:“amazing,amazingly與happy,happily”)等14個(gè)類型。
本文測試了在不同窗口寬度下的詞向量準(zhǔn)確性。采用遞減對稱窗口時(shí),詞向量的準(zhǔn)確性隨著窗口寬度的增加而增加,且增幅逐漸減小直至逐漸下降見圖1。這主要是由于距離越遠(yuǎn)的單詞與目標(biāo)單詞逐漸失去關(guān)聯(lián)。
而采用定值窗口與遞減窗口相比,它在較快增加后更快地進(jìn)入下降過程,如圖2所示。這主要是因?yàn)榫嚯x過遠(yuǎn)的單詞與目標(biāo)單詞之間關(guān)聯(lián)度過低,而較高的權(quán)值加大了噪聲信息。如果考慮窗口寬度無限大這一極限情況,每個(gè)單詞的訓(xùn)練輸入將是其他所有單詞,詞向量將失去意義。
本文還進(jìn)行了非對稱窗口下的測試,對于不同寬度和不同偏移量的窗口進(jìn)行測試,偏移量大于0代表窗口向下文偏移,反之向上文偏移,實(shí)驗(yàn)結(jié)果如圖3所示。在采用非對稱窗口時(shí),詞向量的準(zhǔn)確性在相對偏移量較小時(shí)沒有明顯變化,而相對偏移量較大時(shí)略微降低。例如在窗口單邊寬度為8,偏移為0時(shí),正確率約為54.649%。而窗口偏移為-7時(shí),其正確率約為52.960%;窗口偏移為7時(shí),其正確率約為52.769%,兩者略低于偏移量為0的情況且彼此非常接近。從實(shí)驗(yàn)結(jié)果可以看出:訓(xùn)練結(jié)果的好壞與輸入的文本內(nèi)容本身基本無關(guān),而與偏移量的絕對值有關(guān)。例如,在窗口偏移分別為-7和7時(shí),窗口內(nèi)容僅有12.5%是相同的,而兩者的訓(xùn)練效果相近。
以上的測試觀察和對比了詞向量準(zhǔn)確性的高低。對于類比測試的具體差異,本文進(jìn)一步觀察了7種不同寬度對稱窗口的測試結(jié)果與寬度為1窗口的測試結(jié)果的具體比較,如圖4所示。
通過測試可知:結(jié)果的正確率并非隨著窗口寬度的增加而單純的增加,而是在增加大量正確測試結(jié)果的同時(shí),也增加了大量的錯誤結(jié)果??梢?,在以上過程中并非對于所有單詞有一致的上下文窗口尋優(yōu)方法。
通過測試可知,上下文窗口的選擇對于詞嵌入的結(jié)果有較大影響:遞減窗口能夠得到的詞向量準(zhǔn)確性高于定值窗口;在合理的范圍內(nèi)(上下文窗口包含的單詞與目標(biāo)單詞可能存在相關(guān)性的范圍),窗口越大得到的準(zhǔn)確性越高;窗口偏移量較小時(shí)的訓(xùn)練效果比偏移量較大時(shí)得到的準(zhǔn)確性更高。通過進(jìn)一步的測試發(fā)現(xiàn)各種上下文窗口只對詞匯表中某些單詞有更好的訓(xùn)練結(jié)果,即大量的單詞擁有各自不同最優(yōu)上下文窗口。
參考文獻(xiàn)
[1] BENGIO Y, DUCHARME R, VINCENT P, et al. A neural probabilistic language model [J]. Journal of machine learning research, 2003, 3: 1137?1155.
[2] SOCHER R, BAUER J, MANNING C D, et al. Parsing with compositional vector grammars [C]// Proceedings of 51st Annual Meeting of the Association for Computational Linguistics. [S.l.: s.n.], 2013: 455?465.
[3] SOCHER R, PERELYGIN A, WU J Y, et al. Recursive deep models for semantic compositionality over a sentiment treebank [J/OL]. [2017?03?13]. https://nlp.stanford.edu/~socherr/EMNLP2013_RNTN.pdf.
[4] SIEN?NIK S K. Adapting word2vec to named entity recognition [C]// Proceedings of the 20th Nordic Conference of Computational Linguistics. Vilnius: Link?ping University Electronic Press, 2015: 239?243.
[5] MIKOLOV T, SUTSKEVER I, CHEN K, et al. Distributed representations of words and phrases and their compositionality [J]. Advances in neural information processing systems, 2013, 26: 3111?3119.
[6] BARKAN O. Bayesian neural word embedding [J/OL]. [2016?03?21]. https://arxiv.org/ftp/arxiv/papers/1603/1603.06571.pdf.
[7] L?BRET R, COLLOBERT R. Word embeddings through Hellinger PCA [J/OL]. [2017?01?04]. https://arxiv.org/pdf/1312.5542.pdf.
[8] LEVY O, GOLDBERG Y. Neural word embedding as implicit matrix factorization [J]. Advances in neural information processing systems, 2014, 3: 2177?2185.
[9] LI Y T, XU L L, TIAN F, et al. Word embedding revisited: a new representation learning and explicit matrix factorization perspective [C]// Proceedings of 24th International Conference on Artificial Intelligence. Buenos Aires: AAAI Press, 2015: 3650?3656.
(上接第148頁)
[10] GLOBERSON A, CHECHIK G, PEREIRA F, et al. Euclidean embedding of co?occurrence data [J]. Journal of machine learning research, 2007, 8(4): 2265?2295.
[11] LEVY O, GOLDBERG Y. Linguistic regularities in sparse and explicit word representations [C]// Proceedings of Eighteenth Conference on Computational Natural Language Learning. [S.l.: s.n.], 2014: 171?180.
[12] MIKOLOV T, CHEN K, CORRADO G, et al. Efficient estimation of word representations in vector space [J/OL]. [2013?09?07]. https://arxiv.org/pdf/1301.3781.pdf.
[13] ZHILA A, YIH W, MEEK C, et al. Combining heterogeneous models for measuring relational similarity [C]// Proceedings of Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies. [S.l.: s.n.], 2013: 1000?1009.
[14] MIKOLOV T, YIH W T, ZWEIG G. Linguistic regularities in continuous space word representations [C]// Proceedings of Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies. Atlanta: Association for Computational Linguistics, 2013: 746?751.