百度最近开源了一个新的关于主题模型的项目。文档主题推断工具、语义匹配计算工具以及基于工业级语料训练的三种主题模型: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. MR案例:输出/输入SequenceFile

    SequenceFile文件是Hadoop用来存储二进制形式的key-value对而设计的一种平面文件(Flat File).在SequenceFile文件中,每一个key-value对被看做是一条记 ...

  2. filebeat 乱码

    查看 文件的类型 [root@elk-node-1 rsyslog] # file 192.168.1.16.log 192.168.1.16.log: Non-ISO extended-ASCII ...

  3. pt-table-sync修复mysql主从不一致的数据

    pt-table-sync简介 顾名思义,它用来修复多个实例之间数据的不一致.它可以让主从的数据修复到最终一致,也可以使通过应用双写或多写的多个不相关的数据库实例修复到一致.同时它还内部集成了pt-t ...

  4. poj3977 - subset - the second time - 暴力 + 二分

    2017-08-26 11:38:42 writer:pprp 已经是第二次写这个题了,但是还是出了很多毛病 先给出AC代码: 解题思路: 之前在培训的时候只是笼统的讲了讲怎么做,进行二分对其中一边进 ...

  5. 问下大家,chorme里用开发者工具看headers,点network标签然后刷新网页并没有headers选项,怎么破?

    问下大家,chorme里用开发者工具看headers,点network标签然后刷新网页并没有headers选项,怎么破? 请教个问题 jmeter在Linux服务器压测,抛出很多错误率 但日志中没看到 ...

  6. 对dataframe中某一列进行计数

    本来是一项很简单的任务...但很容易忘记搞混..所以还是记录一下 方法一: df['col'].value_counts() 方法二: groups = df.groupby('col') group ...

  7. Linux SSH隧道技术(端口转发,socket代理)

    动态转发(SOCKS5代理): 命令格式:ssh -D <local port> <SSH Server> ssh -fnND 0.0.0.0:20058 172.16.50. ...

  8. 17.并发容器之ThreadLocal

    1. ThreadLocal的简介 在多线程编程中通常解决线程安全的问题我们会利用synchronzed或者lock控制线程对临界区资源的同步顺序从而解决线程安全的问题,但是这种加锁的方式会让未获取到 ...

  9. 二十七 Python分布式爬虫打造搜索引擎Scrapy精讲—通过自定义中间件全局随机更换代理IP

    设置代理ip只需要,自定义一个中间件,重写process_request方法, request.meta['proxy'] = "http://185.82.203.146:1080&quo ...

  10. 河南省多校联盟二-F 线段树+矩阵

    ---恢复内容开始--- 1284: SP教数学 时间限制: 2 秒  内存限制: 128 MB提交: 24  解决: 4 题目描述 输入 输出 对于每组数据的2操作,输出一行对1e9 + 7取模的答 ...