百度最近开源了一个新的关于主题模型的项目。文档主题推断工具、语义匹配计算工具以及基于工业级语料训练的三种主题模型:Latent

Dirichlet Allocation(LDA)、SentenceLDA 和Topical Word Embedding(TWE)。

.

一、Familia简介

帮Familia,打个小广告~ Familia的github

主题模型在工业界的应用范式可以抽象为两大类: 语义表示和语义匹配。

  • 语义表示 (Semantic Representation)

    对文档进行主题降维,获得文档的语义表示,这些语义表示可以应用于文本分类、文本内容分析、CTR预估等下游应用。

  • 语义匹配 (Semantic Matching)

计算文本间的语义匹配度,我们提供两种文本类型的相似度计算方式:

- 短文本-长文本相似度计算,使用场景包括文档关键词抽取、计算搜索引擎查询和网页的相似度等等。
- 长文本-长文本相似度计算,使用场景包括计算两篇文档的相似度、计算用户画像和新闻的相似度等等。

Familia自带的Demo包含以下功能:

  • 语义表示计算

利用主题模型对输入文档进行主题推断,以得到文档的主题降维表示。

  • 语义匹配计算

    计算文本之间的相似度,包括短文本-长文本、长文本-长文本间的相似度计算。

  • 模型内容展现

    对模型的主题词,近邻词进行展现,方便用户对模型的主题有直观的理解。

.


二、Topical Word Embedding(TWE)

Zhiyuan Liu老师的文章,paper下载以及github

In this way, contextual word embeddings can be flexibly obtained to measure contextual word similarity. We can also build document representations.

且有三款:TWE-1,TWE-2,TWE-3,来看看和传统的skip-gram的结构区别:

在多标签文本分类的精确度:

百度开源项目 Familia中TWE模型的内容展现:

请输入主题编号(0-10000):    105
Embedding Result              Multinomial Result
------------------------------------------------
对话                                    对话
磋商                                    合作
合作                                    中国
非方                                    磋商
探讨                                    交流
对话会议                                联合
议题                                    国家
中方                                    讨论
对话会                                  支持
交流                                    包括

第一列为基于embedding的结果,第二列为基于多项分布的结果,均按照在主题中的重要程度从大到小的顺序排序。

来简单看一下train文件:

import gensim #modified gensim version
import pre_process # read the wordmap and the tassgin file and create the sentence
import sys
if __name__=="__main__":
    if len(sys.argv)!=4:
        print "Usage : python train.py wordmap tassign topic_number"
        sys.exit(1)
    reload(sys)
    sys.setdefaultencoding('utf-8')
    wordmapfile = sys.argv[1]
    tassignfile = sys.argv[2]
    topic_number = int(sys.argv[3])
    id2word = pre_process.load_id2word(wordmapfile)
    pre_process.load_sentences(tassignfile, id2word)
    sentence_word = gensim.models.word2vec.LineSentence("tmp/word.file")
    print "Training the word vector..."
    w = gensim.models.Word2Vec(sentence_word,size=400, workers=20)
    sentence = gensim.models.word2vec.CombinedSentence("tmp/word.file","tmp/topic.file")
    print "Training the topic vector..."
    w.train_topic(topic_number, sentence)
    print "Saving the topic vectors..."
    w.save_topic("output/topic_vector.txt")
    print "Saving the word vectors..."
    w.save_wordvector("output/word_vector.txt")

.


三、SentenceLDA

paper链接 + github:balikasg/topicModelling

SentenceLDA是什么?

an extension of LDA whose goal is to overcome this limitation by incorporating the structure of

the text in the generative and inference processes.

SentenceLDA和LDA区别?

LDA and senLDA differ in that the second assumes a very strong dependence of the latent topics between the words of sentences, whereas the first ssumes independence between the words of documents in general

SentenceLDA和LDA两者对比实验:

We illustrate the advantages of sentenceLDA by comparing it with LDA using both intrinsic (perplexity) and extrinsic (text classification) evaluation tasks on different text collections

原作者的github的结果:

https://github.com/balikasg/topicModelling/tree/master/senLDA

截取一部分code:

import numpy as np, vocabulary_sentenceLayer, string, nltk.data, sys, codecs, json, time
from nltk.tokenize import sent_tokenize
from lda_sentenceLayer import lda_gibbs_sampling1
from sklearn.cross_validation import train_test_split, StratifiedKFold
from nltk.stem import WordNetLemmatizer
from sklearn.utils import shuffle
from functions import *

path2training = sys.argv[1]
training = codecs.open(path2training, 'r', encoding='utf8').read().splitlines()

topics = int(sys.argv[2])
alpha, beta = 0.5 / float(topics), 0.5 / float(topics)

voca_en = vocabulary_sentenceLayer.VocabularySentenceLayer(set(nltk.corpus.stopwords.words('english')), WordNetLemmatizer(), excluds_stopwords=True)

ldaTrainingData = change_raw_2_lda_input(training, voca_en, True)
ldaTrainingData = voca_en.cut_low_freq(ldaTrainingData, 1)
iterations = 201

classificationData, y = load_classification_data(sys.argv[3], sys.argv[4])
classificationData = change_raw_2_lda_input(classificationData, voca_en, False)
classificationData = voca_en.cut_low_freq(classificationData, 1)

final_acc, final_mif, final_perpl, final_ar, final_nmi, final_p, final_r, final_f = [], [], [], [], [], [], [], []
start = time.time()
for j in range(5):
    perpl, cnt, acc, mif, ar, nmi, p, r, f = [], 0, [], [], [], [], [], [], []
    lda = lda_gibbs_sampling1(K=topics, alpha=alpha, beta=beta, docs= ldaTrainingData, V=voca_en.size())
    for i in range(iterations):
        lda.inference()
        if i % 5 == 0:
            print "Iteration:", i, "Perplexity:", lda.perplexity()
            features = lda.heldOutPerplexity(classificationData, 3)
            print "Held-out:", features[0]
            scores = perform_class(features[1], y)
            acc.append(scores[0][0])
            mif.append(scores[1][0])
            perpl.append(features[0])
    final_acc.append(acc)
    final_mif.append(mif)
    final_perpl.append(perpl)

来看看百度开源项目的最终效果,LDA和SentenceLDA的内容展现:

LDA结果:

请输入主题编号(0-1999): 105
--------------------------------------------
对话    0.189676
合作    0.0805558
中国    0.0276284
磋商    0.0269797
交流    0.021069
联合    0.0208559
国家    0.0183163
讨论    0.0154165
支持    0.0146714
包括    0.014198

第二列的数值表示词在该主题下的重要程度。

SentenceLDA结果:

请输入主题编号(0-1999): 105
--------------------------------------------
浙江    0.0300595
浙江省  0.0290975
宁波    0.0195277
记者    0.0174735
宁波市  0.0132504
长春市  0.0123353
街道    0.0107271
吉林省  0.00954326
金华    0.00772971
公安局  0.00678163

.


四、CopulaLDA

SentenceLDA和CopulaLDA同一作者,可见github:balikasg/topicModelling

没细看,来贴效果好了:







.


参考文献:

Familia一个中文主题建模工具包

主题模型︱几款新主题模型——SentenceLDA、CopulaLDA、TWE简析与实现的更多相关文章

  1. Async 、 Await 的异步编程(.NET 4.5 新异步模型) [转自MSDN]

    使用异步编程,可以避免性能瓶颈和增强应用程序的总体响应能力. 但是,编写异步应用程序的以前的技术可能比较复杂,使它们难以编写,调试和维护. Visual Studio 2012 引入了一个简化的方法, ...

  2. 推荐5 款WordPress主题后台选项开发框架

    在开发WordPress 主题的时候,借用成熟的WordPress 主题后台选项开发框架可以为我们省下不少功夫.相信你接触过不少国人做的所谓“原创”主题,一看后台都是千篇一律的界面(连CSS 都懒得改 ...

  3. Dubbo 新编程模型之外部化配置

    外部化配置(External Configuration) 在Dubbo 注解驱动例子中,无论是服务提供方,还是服务消费方,均需要转配相关配置Bean: @Bean public Applicatio ...

  4. 开源介绍·新款简约、实用与大气的Hexo新主题:BMW

    这是一个简约.大气.实用的Hexo新主题:BMW

  5. 第三十二节,使用谷歌Object Detection API进行目标检测、训练新的模型(使用VOC 2012数据集)

    前面已经介绍了几种经典的目标检测算法,光学习理论不实践的效果并不大,这里我们使用谷歌的开源框架来实现目标检测.至于为什么不去自己实现呢?主要是因为自己实现比较麻烦,而且调参比较麻烦,我们直接利用别人的 ...

  6. 为 Drupal 7 构建一个新主题

    主题解释了 Drupal 网站的用户界面 (UI).虽然主题结构并没有明显的变化,但 Drupal 版本 7 配备了一个新的主题实现方法.本文演示了如何创建一个新的 Drupal 7 主题. Drup ...

  7. Cocos2d-x v3.11 中的新内存模型

    Cocso2d-x v3.11 一项重点改进就是 JSB 新内存模型.这篇文章将专门介绍这项改进所带来的新研发体验和一些技术细节. 1. 成果 在 Cocos2d-x v3.11 之前的版本中,使用 ...

  8. Hexo折腾记--小白修改新主题

    UPDATE 2019.5.28 不好意思我又换了个新主题ARIA啦...这回没有个人定制了 前言 如果您曾经来过我的博客,就会发现我的个人博客(https://rye-catcher.github. ...

  9. 使用VS2012主题插件创建自己的主题

    上篇文章讲了如何更换VS2012的主题,具体内容请参考:Vistual Studio 2012更换皮肤.可是上面的步骤仅仅让我们可选择的主题是增多了,我们可不可以自己创建自己的主题呢? 答案是肯定的, ...

随机推荐

  1. 20145303《Java程序设计》实验三实验报告

    20145303<Java程序设计>实验三实验报告 ssh公钥配置及git安装: eclipse中git配置: 队友链接: http://www.cnblogs.com/5321z/p/5 ...

  2. shall的过去式和should怎么区分

    shall的过去式是should,但是怎么和情态动词的should区分啊,答得好我会提高悬赏!!! shall 将来时,用于第一人称:I shall be back in a minute.用来表示征 ...

  3. Linux内核分析方法谈

    本文来自 http://blog.csdn.net/ouyang_linux007/article/details/7422346 Linux的最大的好处之一就是它的源码公开.同时,公开的核心源码也吸 ...

  4. SVN错误:Failed to load JavaHL Library

    环境:jdk1.7(64bit),eclipse4.4(64bit),SVN1.10.3 问题:在利用subclipse同步资源时,报出错误提示 Failed to load JavaHL Libra ...

  5. C#实现日历样式的下拉式计算器

    C#实现日历样式的下拉式计算器 原文地址:http://developer.51cto.com/art/201508/487486.htm 如果我们正在做一个类似于库存控制和计费系统的项目,有些部分可 ...

  6. 使用httpclient提交表单数据加号(+)会被自动替换成空格的坑

    坑的场景: 今天使用httpclient-4.5.3版本,发送如下报文: { "idNo": "7+6+0+2ce722a546b39463bd62817fe57f8&q ...

  7. js 光标选中 操作

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 虚拟机中的Linux安装VMware Tools的方法

    先检查虚拟机是否能上网 一:安装VMware Tools的之前必装的工具套件方法如下: Centos安装VMware Tools: [root@piaoyun-vm vmware-tools-dist ...

  9. js来监控复制粘贴

    平时我们在复制网页上面代码到控制台调试时,有时会出现复制过来的代码后面加上了一下描述信息(作者.版权等信息),每次需要删除才能运行,所以今天看看怎么能保证我们粘贴的代码不携带这些信息呢? (funct ...

  10. Android-----代码实现打开手机第三方应用APP

    最近做一个项目,有一个需要启动第三方应用,和微信的地图查看差不多,需要启动高德,百度或腾讯地图来查看:特来分享,希望有所帮助. 案例效果如图: 要想启动第三方:首先要知道他的包名 一:高德 高德:co ...