重要的是經過實踐更深刻地瞭解貝葉斯思想,先淺淺地瞭解下LDA。html
很是好!https://arxiv.org/pdf/1908.03142.pdf【LDA精講】python
From: http://blog.csdn.net/huagong_adu/article/details/7937616/git
傳統方法的缺陷:github
傳統判斷兩個文檔類似性的方法是經過查看兩個文檔共同出現的單詞的多少,如TF-IDF等,這種方法沒有考慮到文字背後的語義關聯,可能在兩個文檔共同出現的單詞不多甚至沒有,但兩個文檔是類似的。web
在主題模型中,主題表示一個概念、一個方面,表現爲一系列相關的單詞,是這些單詞的條件機率。形象來講,主題就是一個桶,裏面裝了出現機率較高的單詞,這些單詞與這個主題有很強的相關性。算法
怎樣才能生成主題?dom
對文章的主題應該怎麼分析?機器學習
首先,能夠用生成模型來看文檔和主題這兩件事。ide
所謂生成模型,就是說,咱們認爲一篇文章的每一個詞都是經過「以必定機率選擇了某個主題,並從這個主題中以必定機率選擇某個詞語」這樣一個過程獲得的。wordpress
那麼,若是咱們要生成一篇文檔,它裏面的每一個詞語出現的機率爲:
這個機率公式能夠用矩陣表示:
」文檔-詞語」矩陣表示每一個文檔中每一個單詞的詞頻,即出現的機率;
」主題-詞語」矩陣表示每一個主題中每一個單詞的出現機率;
」文檔-主題」矩陣表示每一個文檔中每一個主題出現的機率。
給定一系列文檔,經過對文檔進行分詞,計算各個文檔中每一個單詞的詞頻就能夠獲得左邊這邊」文檔-詞語」矩陣。
主題模型就是經過左邊這個矩陣進行訓練,學習出右邊兩個矩陣。(左邊 ==> 右邊)
主題模型有兩種:pLSA(ProbabilisticLatent Semantic Analysis)和LDA(Latent Dirichlet Allocation),下面主要介紹LDA。
LDA介紹
如何生成M份 N個單詞 的 文檔,LatentDirichlet Allocation這篇文章介紹了3方法:
方法一:unigram model
該模型使用下面方法生成1個文檔:
For each of the N words w_xth: Choose a word w_xth ~ p(w);
其中 N 表示要生成的文檔的單詞的個數,
w_xth表示生成的第n個單詞w,
p(w)表示單詞w的分佈,能夠經過語料進行統計學習獲得,好比給一本書,統計各個單詞在書中出現的機率(詞頻?)。
這種方法經過訓練語料得到一個單詞的機率分佈函數,而後根據這個機率分佈函數每次生成一個單詞,使用這個方法M次生成M個文檔。其圖模型以下圖所示:
方法二:Mixture of unigram
unigram模型的方法的缺點 就是生成的文本沒有主題,過於簡單,mixture of unigram方法對其進行了改進,該模型使用下面方法生成1個文檔:
Choose a topicz ~ p(z); For each ofthe N words w_n: Choose a word w_n ~ p(w|z);
其中z表示一個主題,
p(z)表示主題的機率分佈,
z經過p(z)按機率產生;
N和w_n同上;
p(w|z)表示給定z時w的分佈,能夠當作一個k×V的矩陣,
k爲主題的個數,
V爲單詞的個數,每行表示這個主題對應的單詞的機率分佈,即主題z所包含的各個單詞的機率,經過這個機率分佈按必定機率生成每一個單詞。
這種方法首先選選定一個主題z,主題z對應一個單詞的機率分佈p(w|z),每次按某個主題的分佈生成一個單詞,使用M次這個方法生成M份不一樣的文檔。其圖模型以下圖所示:
從上圖能夠看出,z在w所在的長方形外面,表示z生成一份N個單詞的文檔時主題z只生成一次,即只容許一個文檔只有一個主題,這不太符合常規狀況,一般一個文檔可能包含多個主題。
方法三:LDA(Latent Dirichlet Allocation)
LDA方法使生成的文檔能夠包含多個主題,該模型使用下面方法生成1個文檔:
Chooseparameter θ ~ p(θ); For each ofthe N words w_nth: Choose a topic z_nth ~ p(z|θ); Choose a word w_nth ~ p(w|z);
θ 是一個主題向量,向量的每一列表示每一個主題在文檔出現的機率,該向量爲非負歸一化向量;
p(θ) 是θ的分佈,具體爲Dirichlet分佈,即分佈的分佈; // --> 不少小分佈 組合在一塊兒,每一個分佈出一個值,這些值構成一個集合,這個集合又屬於某一種分佈。
N 表示要生成的文檔的單詞的個數。
w_xth 表示生成的第n個單詞w。
z_nth 表示選擇的主題。
p(z|θ) 表示給定θ時,主題z的機率分佈,具體爲θ的值,即 p(z=i|θ)= θ_i;
p(w|z) 表示給定z時w的分佈,能夠當作一個k×V的矩陣,
這種方法首先選定一個主題向量θ,肯定每一個主題被選擇的機率。
而後在生成每一個單詞的時候,
其圖模型以下圖所示:
從上圖可知LDA的聯合機率爲:
把上面的式子對應到圖上,能夠大體按下圖理解:
從上圖能夠看出,LDA的三個表示層被三種顏色表示出來:
1. corpus-level(紅色) :α和β表示語料級別的參數,也就是每一個文檔都同樣,所以生成過程只採樣一次。
2.document-level(橙色):θ是文檔級別的變量,每一個文檔對應一個θ,也就是每一個文檔產生各個主題z的機率是不一樣的,全部生成每一個文檔採樣一次θ。
3. word-level(綠色) :z和w都是單詞級別變量,z由θ生成,w由z和β共同生成,一個 單詞w對應一個主題z。
經過上面對LDA生成模型的討論,能夠知道LDA模型主要是從給定的輸入語料中學習訓練兩個控制參數α和β,學習出了這兩個控制參數就肯定了模型,即可以用來生成文檔。其中α和β分別對應如下各個信息:
把w當作觀察變量,θ和z當作隱藏變量,就能夠經過EM算法學習出α和β,
求解過程當中遇到後驗機率p(θ,z|w)沒法直接求解,須要找一個似然函數下界來近似求解,
原文使用基於分解(factorization)假設的變分法(varialtional inference)進行計算,用到了EM算法。
每次E-step輸入α和β,計算似然函數,M-step最大化這個似然函數,算出α和β,不斷迭代直到收斂。
( 詳見 Chapter [PGM] )
相關連接 【將LDA打通,是個耗時耗力的事兒】
一、Blei的LDA代碼(C):http://www.cs.princeton.edu/~blei/lda-c/index.html 二、D.Bei的主頁:http://www.cs.princeton.edu/~blei/publications.html 三、Gibbs LDA++ by Xuan-Hieu Phan and Cam-Tu Nguyen(C++):http://gibbslda.sourceforge.net/ 四、用GibbsLDA作Topic Modeling (教程 by Lu Heng):http://weblab.com.cityu.edu.hk/blog/luheng/2011/06/ 五、Daichi Mochihashi(C,Matlab) :http://chasen.org/~daiti-m/dist/lda/ 六、Griffiths和Steyvers的Topic Modeling工具箱:http://psiexp.ss.uci.edu/research/programs_data/toolbox.htm 7.shuyo的LDA(python):https://shuyo.wordpress.com/2011/05/18/latent-dirichlet-allocation-in-python/ 8.python庫(lda 1.0.2):https://pypi.python.org/pypi/lda 9.圖像主題方面的LDA實現(python):http://www.mblondel.org/journal/2010/08/21/latent-dirichlet-allocation-in-python/ 十、一些博客: (1)【轉】LDA必讀的資料 http://www.xperseverance.net/blogs/2012/03/657/ (2)我愛機器學習小站裏面LDA部分:http://www.52ml.net/tags/lda/page/7 (3)基於LDA的Topic Model變形 http://www.cnblogs.com/wentingtu/archive/2013/06/02/3113422.html (4)Topic modeling LDA by Blei(對Blei關於LDA的一些文章的評論)http://blog.csdn.net/pirage/article/details/8889951 (5)Latent dirichlet allocation note(裏面有對Daichi Mochihashi寫的LDA代碼應用的教程)http://blog.csdn.net/wangran51/article/details/7408399 (6)Blei教學LDA視頻:http://videolectures.NET/mlss09uk_blei_tm/ 主題模型及其變種的實現代碼彙總 1.MALLET:實現語言,Java,實現模型,LDA,HLDA,Pachinko Allocation Model,此外,還實現了HMM,最大熵馬爾科夫模型和條件隨機場; 2.Shuyo的github代碼:實現語言,Python,實現模型,LDA,Dirichlet Process Gaussian Mixture Model,online HDP,HDPLDA,Interactive Topic Model,Labeled LDA 地址:https://github.com/shuyo/iir/tree/master/lda 3.arongdari的github代碼:實現語言,python,實現模型,LDA,Correlated Topic Model,Relational topic model,Author-Topic model,HMM-LDA,Discrete Infinite logistic normal,Supervised Topic Model,Hierarchical Dirichlet process,Hierarchical Dirichlet scaling process 地址:https://github.com/arongdari/python-topic-model 4.Gensim:實現語言,Python,實現模型,LDA,Dynamic Topic Model,Dynamic Influence Model,HDP,LSI,Random Projections,深度學習的word2vec,paragraph2vec。 官網地址:http://radimrehurek.com/gensim/index.html github代碼地址:https://github.com/piskvorky/gensim 5.ahmaurya的github代碼:實現語言,Python,實現模型,Topic Over Time github代碼地址:https://github.com/ahmaurya/topics_over_time 6.Blei實驗室的代碼:實現語言,Python,實現模型,online lda,online HDP,turbo topic model,topic model visualization engine,實現語言,C,實現模型,correlated topic model,discrete infinite logistic normal,HLDA,lda,實現語言C++,實現模型,ctr,class-slda,Dynamic Topic model and the influence model,實現語言R,實現模型 lda github代碼地址:http://www.cs.columbia.edu/~blei/topicmodeling_software.html 7.中國科學技術信息研究所徐碩老師的PDF,對LDA,TOT,AT模型如何使用gibbs sampling求參進行了細緻推導,並依據求參結果給出僞代碼。 地址:http://blog.sciencenet.cn/blog-611051-582492.html [轉]LDA的必讀文章和相關代碼 LDA和HLDA: (1)D. M. Blei, et al., "Latent Dirichlet allocation," Journal of Machine Learning Research, vol. 3, pp. 993-1022, 2003. (2)T. L. Griffiths and M. Steyvers, "Finding scientific topics," Proceedings of the National Academy of Sciences, vol. 101, pp. 5228-5235, 2004. (3)D. M. Blei, et al., "Hierarchical Topic Models and the Nested Chinese Restaurant Process," NIPS, 2003. (4)Blei的LDA視頻教程:http://videolectures.net/mlss09uk_blei_tm/ (5)Teh的關於Dirichlet Processes的視頻教程:http://videolectures.Net/mlss07_teh_dp/ (6)Blei的畢業論文:http://www.cs.princeton.edu/~blei/papers/Blei2004.pdf (7)Jordan的報告:http://www.icms.org.uk/downloads/mixtures/jordan_talk.pdf (8)G. Heinrich, "Parameter Estimation for Text Analysis," http://www.arbylon.net/publications/text-est.pdf 基礎知識: (1)P. Johnson and M. Beverlin, 「Beta Distribution,」 http://pj.freefaculty.org/ps707/Distributions/Beta.pdf (2)M. Beverlin and P. Johnson, 「The Dirichlet Family,」 http://pj.freefaculty.org/stat/Distributions/Dirichlet.pdf (3)P. Johnson, 「Conjugate Prior and Mixture Distributions」,http://pj.freefaculty.org/stat/TimeSeries/ConjugateDistributions.pdf (4)P.J. Green, 「Colouring and Breaking Sticks:Random Distributions and Heterogeneous Clustering」,http://www.maths.bris.ac.uk/~mapjg/papers/GreenCDP.pdf (5)Y. W. Teh, "Dirichlet Process", http://www.gatsby.ucl.ac.uk/~ywteh/research/npbayes/dp.pdf (6)Y. W. Teh and M. I. Jordan, "Hierarchical Bayesian Nonparametric Models with Applications,」 http://www.stat.berkeley.edu/tech-reports/770.pdf (7)T. P. Minka, "Estimating a Dirichlet Distribution", http://research.microsoft.com/en-us/um/people/minka/papers/dirichlet/minka-dirichlet.pdf (8)北郵論壇的LDA導讀:[導讀]文本處理、圖像標註中的一篇重要論文Latent Dirichlet Allocation,http://bbs.byr.edu.cn/article/PR_AI/2530?p=1 (9)Zhou Li的LDA Note:http://lsa-lda.googlecode.com/files/Latent Dirichlet Allocation note.pdf (10)C. M. Bishop, 「Pattern Recognition And Machine Learning,」 Springer, 2006. 代碼: (1)Blei的LDA代碼(C):http://www.cs.princeton.edu/~blei/lda-c/index.html (2)BLei的HLDA代碼(C):http://www.cs.princeton.edu/~blei/downloads/hlda-c.tgz (3)Gibbs LDA(C++):http://gibbslda.sourceforge.net/ (4)Delta LDA(Python):http://pages.cs.wisc.edu/~andrzeje/research/deltaLDA.tgz (5)Griffiths和Steyvers的Topic Modeling工具箱:http://psiexp.ss.uci.edu/research/programs_data/toolbox.htm (6)LDA(Java):http://www.arbylon.net/projects/ (7)Mochihashi的LDA(C,Matlab):http://chasen.org/~daiti-m/dist/lda/ (8)Chua的LDA(C#):http://www.mysmu.edu/phdis2009/freddy.chua.2009/programs/lda.zip (9)Chua的HLDA(C#):http://www.mysmu.edu/phdis2009/freddy.chua.2009/programs/hlda.zip 其餘: (1)S. Geman and D. Geman, "Stochastic Relaxation, Gibbs Distributions, and the Bayesian Restoration of Images," Pattern Analysis and Machine Intelligence, IEEE Transactions on, vol. PAMI-6, pp. 721-741, 1984. (2)B. C. Russell, et al., "Using Multiple Segmentations to Discover Objects and their Extent in Image Collections," in Computer Vision and Pattern Recognition, 2006 IEEE Computer Society Conference on, 2006, pp. 1605-1614. (3)J. Sivic, et al., "Discovering objects and their location in images," in Computer Vision, 2005. ICCV 2005. Tenth IEEE International Conference on, 2005, pp. 370-377 Vol. 1. (4)F. C. T. Chua, "Summarizing Amazon Reviews using Hierarchical Clustering,"http://www.mysmu.edu/phdis2009/freddy.chua.2009/papers/amazon.pdf (5)F. C. T. Chua, "Dimensionality Reduction and Clustering of Text Documents,」http://www.mysmu.edu/phdis2009/freddy.chua.2009/papers/probabilisticIR.pdf (6)D Bacciu, "Probabilistic Generative Models for Machine Vision,"http://www.math.unipd.it/~sperduti/AI09/bacciu_unipd_handouts.pdf