使用DTM ( Dynamic Topic Models )进行主题演化实验

最近想研究下Dynamic Topic Models(DTM),论文看了看,文科生的水平确实是看不懂,那就实验一下吧,正好Blei的主页上也提供了相应的C++工具, http://www.cs.princeton.edu/~blei/topicmodeling.html,dtm这个代码放在google code中,下载需要fq。

下载了之后看了看,C++确实是不懂,但是在github上搜了一遭,也没找到完美的java版本,所以只能硬着头皮用C++了。

同时也去网上找找看看有没有人做过类似的工作,一搜确实是有,但是不多:

1、一哥们的实验,在linux下,http://www.jgoodwin.net/experimenting-with-dynamic-topic-models/

2、一些人的疑问,有没有python版本,答案是没有。http://stackoverflow.com/questions/22469506/are-there-any-efficient-python-libraries-for-dynamic-topic-models-preferably-ex

3、邮件列表 https://lists.cs.princeton.edu/pipermail/topic-models/

 

看了这么多,也没看明白是什么意思,还是自己慢慢搞吧。

第1步:安装系统

  下载个centos5.5,这个在readme文件中有说明,作者就是这个版本编译的,我安装到了VMware中,当然还有另外的两个版本可以用。

第2步:编译文件

  这个在readme文件中也有说明,把文件放到相应目录,make一下就行了。

第3步:作者建议先用他的文件里面带的一个小例子试验一下。

  在dtm/sample.sh文件中有说明。

   (1)输入文件。

  两个输入文件是必须的,这些文件在dtm/example文件夹下面可以看到,是test-mult.dat和test-seq.dat。

     a:foo-mult.dat ,用来表示文档和词的关系

   每个文档一行,每一行形式是: unique_word_count index1:count1 index2:count2 ... indexn:counnt

   用中文来说就是:该文章的总词数(不重复) 词1编号(用数字表示编号):词1频次 词2编号:词2频次   词n编号:词n频次

   例如:11 288:1 1248:1 5:1 1063:2 269:1 654:1 656:2 532:1 373:1 1247:1 543:1  

   表示这篇一共有11个不重复的词,第228个词出现1次,1248个词出现1次,这些词是所有文档中统一编号的。

         需要注意的一点是:该文件中文档是按时间顺序排列的,时间最早的在最上面,时间最晚的在最下面。

  b:foo-deq.dat ,这文件是用来划分时间窗的。

    文件格式如下:

        Number_Timestamps(时间窗总数)
        number_docs_time_1(第一个时间窗的文档数,就是从第一个到第几个文档划分到第一个时间窗,我们如果按年来划分,就把每年的文档数写到这里就行)
         ...
        number_docs_time_i
        ...
        number_docs_time_NumberTimestamps

           作者提供的例子,第一行表示分为10个时间窗,第二行表示第一个时间窗有25个文档。(看样子估计也是按年划分的):

          10
          25
          50
          75
          100
          100
          100
          100
          125
          150
          175

     当上面两个文件搞定后。作者说还有两个文件虽然不是必须的,但是也是很有用的。

     C: 词典文件

   文档集合中涉及的所有的词,按照上面的词的序号排列。

     d:文档信息文件

   每行表示一个文档的基本信息,按照文档a中的顺序排列。

        上述文件都可以用text2ldac生成,在https://github.com/JoKnopp/text2ldac下载,用python打开。

    使用方法,在命令行中,找到text2ldac.py目录,运行 python text2ldac.py -o ./out -e txt ./in

    out文件夹为输出文件位置,in文件夹为输入文件位置。 txt为仅处理txt文件

  (2)运行程序

   作者在readme文件中说,通过运行./main --help命令可以查看所有选项和解释,下面是运行该命令后的结果,太多了啊

  

Flag initialize_lda is of type bool, but its default value is not a boolean. NOTE: This will soon be a compilations error!main: Warning: SetUsageMessage() never called

Flags from ../lib/util/gflags-1.1/src/gflags.cc:
-flagfile (load flags from file) type: string default: ""
-fromenv (set flags from the environment [use 'export FLAGS_flag1=value'])
type: string default: ""
-tryfromenv (set flags from the environment if present) type: string
default: ""
-undefok (comma-separated list of flag names that it is okay to specify on
the command line even if the program does not define a flag with that
name. IMPORTANT: flags in this list that have arguments MUST use the
flag=value format) type: string default: ""

Flags from ../lib/util/gflags-1.1/src/gflags_completions.cc:
-tab_completion_columns (Number of columns to use in output for tab
completion) type: int32 default: 80
-tab_completion_word (If non-empty, HandleCommandLineCompletions() will
hijack the process and attempt to do bash-style command line flag
completion on this value.) type: string default: ""

Flags from ../lib/util/gflags-1.1/src/gflags_reporting.cc:
-help (show help on all flags [tip: all flags can have two dashes])
type: bool default: true
-helpfull (show help on all flags -- same as -help) type: bool
default: false
-helpmatch (show help on modules whose name contains the specified substr)
type: string default: ""
-helpon (show help on the modules named by this flag value) type: string
default: ""
-helppackage (show help on all modules in the main package) type: bool
default: false
-helpshort (show help on only the main module for this program) type: bool
default: false
-helpxml (produce an xml version of help) type: bool default: false
-version (show version and build info and exit) type: bool default: false

Flags from data.c:
-influence_flat_years (How many years is the influence nonzero?If nonpositive, a lognormal distribution is used.)

    type: int32 default: -1
-influence_mean_years (How many years is the mean number of citations?)
  type: double default: 20
-influence_stdev_years (How many years is the stdev number of citations?)
  type: double default: 15
-max_number_time_points (Used for the influence window.) type: int32
  default: 200
-resolution (The resolution. Used to determine how far out the beta mean should be.)

   type: double default: 1
-sigma_c (c stdev.) type: double default: 0.050000000000000003
-sigma_cv (Variational c stdev.) type: double
  default: 9.9999999999999995e-07
-sigma_d (If true, use the new phi calculation.) type: double
  default: 0.050000000000000003
-sigma_l (If true, use the new phi calculation.) type: double
  default: 0.050000000000000003
-time_resolution (This is the number of years per time slice.) type: double
  default: 0.5

Flags from gsl-wrappers.c:
-rng_seed (Specifies the random seed. If 0, seeds pseudo-randomly.)
  type: int64 default: 0

Flags from lda-seq.c:
-fix_topics (Fix a set of this many topics. This amounts to fixing these topics' variance at 1e-10.)

  type: int32 default: 0
-forward_window (The forward window for deltas. If negative, we use a beta with mean 5.)

   type: int32 default: 1
-lda_sequence_max_iter (The maximum number of iterations.)

  type: int32
  default: 20
-lda_sequence_min_iter (The maximum number of iterations.)

   type: int32 default: 1
-normalize_docs (Describes how documents's wordcounts are considered for finding influence. Options are "normalize", "none", "occurrence", "log", or "log_norm".)

  type: string default: "normalize"
-save_time (Save a specific time. If -1, save all times.)

  type: int32 default: 2147483647

Flags from lda.c:
-lambda_convergence (Specifies the level of convergence required for lambda in the phi updates.)

  type: double default: 0.01

Flags from main.c:
-alpha () type: double default: -10
-corpus_prefix (The function to perform. Can be dtm or dim.)

   type: string default: ""
-end () type: int32 default: -1
-heldout_corpus_prefix () type: string default: ""
-heldout_time (A time up to (but not including) which we wish to train, and at which we wish to test.) type: int32 default: -1
-initialize_lda (If true, initialize the model with lda.) type: bool
default: false
-lda_max_em_iter () type: int32 default: 20
-lda_model_prefix (The name of a fit model to be used for testing likelihood. Appending "info.dat" to this should give the name of the
file.) type: string default: ""
-mode (The function to perform. Can be fit, est, or time.) type: string
default: "fit"
-model (The function to perform. Can be dtm or dim.)

  type: string default: "dtm"
-ntopics () type: double default: -1
-outname () type: string default: ""
-output_table () type: string default: ""
-params_file (A file containing parameters for this run.) type: string
default: "settings.txt"
-start () type: int32 default: -1
-top_chain_var () type: double default: 0.0050000000000000001
-top_obs_var () type: double default: 0.5

   输入下面的命令(后面的注释是我自己加的,。如果影响运行请去掉)

./main                /*main函数*/
--ntopics=20      /*每个时间窗生成20个主题*/                 
--mode=fit        /*这个应该有dim和fit两个选项*/     
--rng_seed=0
--initialize_lda=true
--corpus_prefix=example/test
--outname=example/model_run
--top_chain_var=0.005
--alpha=0.01
--lda_sequence_min_iter=6
--lda_sequence_max_iter=20
--lda_max_em_iter=10

                    

       (2)输出结果。  上面文件完成之后,通过运行程序生成下面的文件,并且可以通过R查看结果,我们就可以用这个结果进行分析。

   a topic-???-var-e-log-prob.dat:      

      主要是 e-betas(词在每个主题内每个时间段的分布),一行是一个词。

      从文件中,我们看以看到每行只有一个数字。

      可以在dtmexamplemodel_runlda-seq中看到例子,他这个应该是有48240行,应该是有4824个词,每个时间窗内有4824个词??。作者同时给出了在R中查看这些矩阵的方法。比如查看某个词在某个主题的某一个时间段的概率。

        b gam.dat 

                 gammas数据。表示文档与主题的关联。

   

原文地址:https://www.cnblogs.com/todoit/p/4057619.html