TuoTuo
Stable 0.2.7 release
Tuotuo用Python编写的主题建模库。 Tuotuo也是一个可爱的男孩,我的儿子,现在已经6个月大了。
使用包装管理器PIP安装Tuotuo。您可能会在这里找到PYPI分布。
pip install TuoTuo --upgrade当前,库仅支持通过潜在Dirichlet分配(LDA)进行主题建模。众所周知,可以使用Gibbs采样和变异推理来实现LDA,我们选择后者,因为这在数学上更复杂
import torch as tr
from tuotuo . generator import doc_generator
gen = doc_generator (
M = 100 ,
# we sample 100 documents
L = 20 ,
# each document would contain 20 pre-defined words
topic_prior = tr . tensor ([ 1 , 1 , 1 , 1 , 1 ], dtype = tr . double )
# we use a exchangable Dirichlet Distribution as our topic prior,
# that is a uniform distribution on 5 topics
)
train_docs = gen . generate_doc () from tuotuo . lda_model import LDASmoothed
import matplotlib . pyplot as plt
lda = LDASmoothed (
num_topics = 5 ,
)
perplexes = lda . fit (
train_docs ,
sampling = False ,
verbose = True ,
return_perplexities = True ,
)
plt . plot ( perplexes )
= >= >= >= >= >= >= >= >
Topic Dirichlet Prior , Alpha
1
Exchangeable Word Dirichlet Prior , Eta
1
Var Inf - Word Dirichlet prior , Lambda
( 5 , 40 )
Var Inf - Topic Dirichlet prior , Gamma
( 100 , 5 )
Init perplexity = 84.99592157507153
End perplexity = 45.96696541539976 
for topic_index in range ( lda . _lambda_ . shape [ 0 ]):
top5 = np . argsort ( lda . _lambda_ [ topic_index ,:],)[ - 5 :]
print ( f"Topic { topic_index } " )
for i , idx in enumerate ( top5 ):
print ( f"Top { i + 1 } -> { lda . train_doc . idx_to_vocab [ idx ] } " )
print ()
= >= >= >= >= >= >= >= >
Topic 0
Top 1 -> physical
Top 2 -> quantum
Top 3 -> research
Top 4 -> scientst
Top 5 -> astrophysics
Topic 1
Top 1 -> divorce
Top 2 -> attorney
Top 3 -> court
Top 4 -> bankrupt
Top 5 -> contract
Topic 2
Top 1 -> content
Top 2 -> Craftsmanship
Top 3 -> concert
Top 4 -> asymmetrical
Top 5 -> Symmetrical
Topic 3
Top 1 -> recreation
Top 2 -> FIFA
Top 3 -> football
Top 4 -> Olympic
Top 5 -> athletics
Topic 4
Top 1 -> fever
Top 2 -> appetite
Top 3 -> contagious
Top 4 -> decongestant
Top 5 -> injection正如我们可以从前5个单词中看到的那样,我们可以轻松地实现以下映射:
主题0->科学主题1->法律主题2->艺术主题3->体育主题4->健康
欢迎拉动请求。对于重大更改,请先开设一个问题,以讨论您想更改的内容。
由于没有可用的成熟主题建模库,因此我们还在寻找想在以下方向上做出贡献的合作者:
这部分完成了大部分工作,我们仍然需要研究:
扩展库以支持此ICML论文之后的神经变性推断:文本处理的神经变异推断
扩展培训以支持此ACL论文:通过增强学习的神经主题模型支持加强学习
麻省理工学院