LDA模型学习(代码)

               

 为了把LDA算法用于文本聚类,我真的是绞尽脑汁。除了去看让我头大的概率论、随机过程、高数这些基础的数学知识,还到网上找已经实现的源代码。

     最先让我看到署光的是Mallet,我研究了大概一个星期,最后决定放弃了。因为Mallet作者提供的例子实在太少了。

      回到了网上找到的这样一段源代码:

   
   
  1. /*  
  2.  * (C) Copyright 2005, Gregor Heinrich (gregor :: arbylon : net) (This file is  
  3.  * part of the org.knowceans experimental software packages.)  
  4.  */ 
  5. /*  
  6.  * LdaGibbsSampler is free software; you can redistribute it and/or modify it  
  7.  * under the terms of the GNU General Public License as published by the Free  
  8.  * Software Foundation; either version 2 of the License, or (at your option) any  
  9.  * later version.  
  10.  */ 
  11. /*  
  12.  * LdaGibbsSampler is distributed in the hope that it will be useful, but  
  13.  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or  
  14.  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more  
  15.  * details.  
  16.  */ 
  17. /*  
  18.  * You should have received a copy of the GNU General Public License along with  
  19.  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple  
  20.  * Place, Suite 330, Boston, MA 02111-1307 USA  
  21.  */ 
  22.  
  23. /*  
  24.  * Created on Mar 6, 2005  
  25.  */ 
  26. package com.xh.lda;  
  27.  
  28. import java.text.DecimalFormat;  
  29. import java.text.NumberFormat;  
  30.  
  31. /**  
  32.  * Gibbs sampler for estimating the best assignments of topics for words and  
  33.  * documents in a corpus. The algorithm is introduced in Tom Griffiths' paper  
  34.  * "Gibbs sampling in the generative model of Latent Dirichlet Allocation"  
  35.  * (2002).  
  36.  *   
  37.  * @author heinrich  
  38.  */ 
  39. public class LdaGibbsSampler {  
  40.  
  41.     /**  
  42.      * document data (term lists)  
  43.      */ 
  44.     int[][] documents;  
  45.  
  46.     /**  
  47.      * vocabulary size  
  48.      */ 
  49.     int V;  
  50.  
  51.     /**  
  52.      * number of topics  
  53.      */ 
  54.     int K;  
  55.  
  56.     /**  
  57.      * Dirichlet parameter (document--topic associations)  
  58.      */ 
  59.     double alpha;  
  60.  
  61.     /**  
  62.      * Dirichlet parameter (topic--term associations)  
  63.      */ 
  64.     double beta;  
  65.  
  66.     /**  
  67.      * topic assignments for each word.  
  68.      */ 
  69.     int z[][];  
  70.  
  71.     /**  
  72.      * cwt[i][j] number of instances of word i (term?) assigned to topic j.  
  73.      */ 
  74.     int[][] nw;  
  75.  
  76.     /**  
  77.      * na[i][j] number of words in document i assigned to topic j.  
  78.      */ 
  79.     int[][] nd;  
  80.  
  81.     /**  
  82.      * nwsum[j] total number of words assigned to topic j.  
  83.      */ 
  84.     int[] nwsum;  
  85.  
  86.     /**  
  87.      * nasum[i] total number of words in document i.  
  88.      */ 
  89.     int[] ndsum;  
  90.  
  91.     /**  
  92.      * cumulative statistics of theta  
  93.      */ 
  94.     double[][] thetasum;  
  95.  
  96.     /**  
  97.      * cumulative statistics of phi  
  98.      */ 
  99.     double[][] phisum;  
  100.  
  101.     /**  
  102.      * size of statistics  
  103.      */ 
  104.     int numstats;  
  105.  
  106.     /**  
  107.      * sampling lag (?)  
  108.      */ 
  109.     private static int THIN_INTERVAL = 20;  
  110.  
  111.     /**  
  112.      * burn-in period  
  113.      */ 
  114.     private static int BURN_IN = 100;  
  115.  
  116.     /**  
  117.      * max iterations  
  118.      */ 
  119.     private static int ITERATIONS = 1000;  
  120.  
  121.     /**  
  122.      * sample lag (if -1 only one sample taken)  
  123.      */ 
  124.     private static int SAMPLE_LAG;  
  125.  
  126.     private static int dispcol = 0;  
  127.  
  128.     /**  
  129.      * Initialise the Gibbs sampler with data.  
  130.      *   
  131.      * @param V  
  132.      *            vocabulary size  
  133.      * @param data  
  134.      */ 
  135.     public LdaGibbsSampler(int[][] documents, int V) {  
  136.  
  137.         this.documents = documents;  
  138.         this.V = V;  
  139.     }  
  140.  
  141.     /**  
  142.      * Initialisation: Must start with an assignment of observations to topics ?  
  143.      * Many alternatives are possible, I chose to perform random assignments  
  144.      * with equal probabilities  
  145.      *   
  146.      * @param K  
  147.      *            number of topics  
  148.      * @return z assignment of topics to words  
  149.      */ 
  150.     public void initialState(int K) {  
  151.         int i;  
  152.  
  153.         int M = documents.length;  
  154.  
  155.         // initialise count variables.  
  156.         nw = new int[V][K];  
  157.         nd = new int[M][K];  
  158.         nwsum = new int[K];  
  159.         ndsum = new int[M];  
  160.  
  161.         // The z_i are are initialised to values in [1,K] to determine the  
  162.         // initial state of the Markov chain.  
  163.  
  164.         z = new int[M][];  
  165.         for (int m = 0; m < M; m++) {  
  166.             int N = documents[m].length;  
  167.             z[m] = new int[N];  
  168.             for (int n = 

猜你喜欢

转载自blog.csdn.net/bgfuufb/article/details/86572498