上下文化主题模型(CTM)是一个主题模型家族,使用语言的预培训表示(例如,BERT)来支持主题建模。有关详细信息,请参见论文:
我们的新主题建模家庭支持许多不同的语言(即一种由拥抱面模型支持的语言),并有两个版本:组合tm将上下文嵌入与好的旧单词结合在一起,以使更连贯的主题; Zeroshottm是任务的理想主题模型,在该任务中,您可能会在测试数据中缺少单词,并且,如果接受多语言嵌入培训,则继承了作为多语言主题模型的属性!
最大的优势是您可以将不同的嵌入式用于CTM。因此,当出现新的嵌入方法时,您可以在代码中使用它并改善结果。我们不再受弓的限制。
我们也有小猫!一个新的子模块,可用于创建人类的分类器,以快速对您的文档进行分类并创建命名群集。
您可以查看我们的中等博客文章,也可以从我们的COLAB教程之一开始:
| 姓名 | 关联 |
|---|---|
| Wikipedia数据(Preproc+Save+VIZ)上的TM组合(稳定v2.3.0 ) | |
| 零射击跨语言主题建模(Preproc+viz)(稳定v2.3.0 ) | |
| Kitty:循环分类器中的人(高级用法)(稳定v2.2.0 ) | |
| SuperCTM和β-CTM(高级使用)(稳定的V2.2.0 ) |
重要的是:如果要使用CUDA,则需要安装与您的发行版匹配的CUDA系统的正确版本,请参见Pytorch。
使用PIP安装软件包
pip install -U contextualized_topic_models要考虑的一个重要方面是您要使用的网络:结合上下文化嵌入式和弓(组合tm)的网络,或仅使用上下文化嵌入式(zeroshottm)的网络。
但是请记住,您只能使用Zeroshottm模型进行零击的跨语性主题建模。
上下文化主题模型还支持监督(SUPERCTM)。您可以在文档中阅读有关此信息的更多信息。
我们还拥有小猫:您可以用来在文档的循环分类中进行更简单的人。进行文档过滤可能非常有用。它还可以在跨语言环境中工作,因此您可以用不知道的语言过滤文档!
如果您发现这有用,则可以引用以下论文:)
Zeroshottm
@inproceedings {bianchi-etal-2021-Cross,
title =“带有零拍学习的跨语性上下文化主题模型”,
作者=“ Bianchi,Federico和Terragni,Silvia和Hovy,Dirk和
Nozza,Debora和Fersini,Elisabetta”,
BookTitle =“计算语言学协会欧洲分会第16届会议论文集:主要卷”,
月= APR,
年=“ 2021”,
地址=“在线”,
发布者=“计算语言学协会”,
url =“ https://www.aclweb.org/anthology/2021.eacl-main.143”,
页=“ 1676--1683”,
}
组合
@inproceedings {bianchi-etal-2021-pre,
title =“预训练是一个热门话题:上下文化的文档嵌入会改善主题连贯性”,
作者=“ Bianchi,Federico和
Terragni,Silvia和
Hovy,Dirk”,
BookTitle =“计算语言学协会第59届年会和第11届国际自然语言处理联合会议(第2卷:简短论文)的会议记录”,
月=八月,
年=“ 2021”,
地址=“在线”,
发布者=“计算语言学协会”,
url =“ https://aclanthology.org/2021.acl-short.96”,
doi =“ 10.18653/v1/2021.acl-short.96”,
页=“ 759--766”,
}
以下一些示例使用多语言嵌入模型paraphrase-multilingual-mpnet-base-v2 。这意味着您将要使用的表示形式是虚假的。但是,您可能需要更广泛的语言覆盖范围或一种特定的语言。请参阅文档中的页面,以查看如何为另一种语言选择模型。在这种情况下,您可以检查Sbert以找到使用的理想模型。
在这里,您可以阅读有关语言特定和穆利特语的更多信息。
您绝对应该查看文档,以更好地了解这些主题模型的工作方式。
这是您可以使用组合的TM的方法。这是一个标准主题模型,也使用上下文化的嵌入。 CombinedTM的好处是,它使您的话题更加连贯(请参阅纸张https://arxiv.org/abs/2004.03974)。 N_COMPONENTS = 50指定主题的数量。
from contextualized_topic_models . models . ctm import CombinedTM
from contextualized_topic_models . utils . data_preparation import TopicModelDataPreparation
from contextualized_topic_models . utils . data_preparation import bert_embeddings_from_file
qt = TopicModelDataPreparation ( "all-mpnet-base-v2" )
training_dataset = qt . fit ( text_for_contextual = list_of_unpreprocessed_documents , text_for_bow = list_of_preprocessed_documents )
ctm = CombinedTM ( bow_size = len ( qt . vocab ), contextual_size = 768 , n_components = 50 ) # 50 topics
ctm . fit ( training_dataset ) # run the model
ctm . get_topics ( 2 )高级注释: TM组合将弓与Sbert结合在一起,这一过程似乎增加了预测主题的连贯性(https://arxiv.org/pdf/2004.03974.pdf)。
我们的Zeroshottm可用于零摄主题建模。它可以处理训练阶段未使用的单词。更有趣的是,该模型可用于跨语性主题建模(请参见下一节)!请参阅论文(https://www.aclweb.org/anthology/2021.eacl-main.143)
from contextualized_topic_models . models . ctm import ZeroShotTM
from contextualized_topic_models . utils . data_preparation import TopicModelDataPreparation
from contextualized_topic_models . utils . data_preparation import bert_embeddings_from_file
text_for_contextual = [
"hello, this is unpreprocessed text you can give to the model" ,
"have fun with our topic model" ,
]
text_for_bow = [
"hello unpreprocessed give model" ,
"fun topic model" ,
]
qt = TopicModelDataPreparation ( "paraphrase-multilingual-mpnet-base-v2" )
training_dataset = qt . fit ( text_for_contextual = text_for_contextual , text_for_bow = text_for_bow )
ctm = ZeroShotTM ( bow_size = len ( qt . vocab ), contextual_size = 768 , n_components = 50 )
ctm . fit ( training_dataset ) # run the model
ctm . get_topics ( 2 )如您所见,处理文本的高级API非常易于使用。 Text_for_bert应该用于将未预处理的文档列表传递给模型。相反,对于text_for_bow ,您应该传递用于构建弓的预处理文本。
高级注释:这样,Sbert可以使用文本中的所有信息来生成表示形式。
一旦训练了模型,就很容易获得主题!
ctm . get_topics ()转换方法将为您照顾大多数事情,例如,仅考虑模型在训练中看到的单词来产生相应的弓。但是,这在与Zeroshottm打交道时会带来一些颠簸,就像我们在下一节中所示。
但是,如果您愿意,可以手动加载嵌入式(请参阅本文档的高级部分)。
如果使用组合TM,则需要包括弓的测试文本:
testing_dataset = qt . transform ( text_for_contextual = testing_text_for_contextual , text_for_bow = testing_text_for_bow )
# n_sample how many times to sample the distribution (see the doc)
ctm . get_doc_topic_distribution ( testing_dataset , n_samples = 20 ) # returns a (n_documents, n_topics) matrix with the topic distribution of each document如果使用ZeroshottM,则无需使用testing_text_for_bow,因为如果使用不同的测试文档,这将创建一个不同尺寸的弓。因此,做到这一点的最佳方法是仅通过输入中要给出的文本传递到contexual模型:
testing_dataset = qt . transform ( text_for_contextual = testing_text_for_contextual )
# n_sample how many times to sample the distribution (see the doc)
ctm . get_doc_topic_distribution ( testing_dataset , n_samples = 20 )一旦您使用多语言嵌入培训了Zeroshottm模型,就可以使用此简单管道来预测用不同语言的文档的主题(只要该语言被释义 - 词汇量化词语介绍了MPNET-BASE-V2 )。
# here we have a Spanish document
testing_text_for_contextual = [
"hola, bienvenido" ,
]
# since we are doing multilingual topic modeling, we do not need the BoW in
# ZeroShotTM when doing cross-lingual experiments (it does not make sense, since we trained with an english Bow
# to use the spanish BoW)
testing_dataset = qt . transform ( testing_text_for_contextual )
# n_sample how many times to sample the distribution (see the doc)
ctm . get_doc_topic_distribution ( testing_dataset , n_samples = 20 ) # returns a (n_documents, n_topics) matrix with the topic distribution of each document高级注释:我们不需要传递西班牙语袋:两种语言的单词袋将是不可比的!出于兼容原因,我们将其传递给了模型,但是您无法获得模型的输出(即,训练有素的语言的预测弓),并将其与测试语言之一进行比较。
您是否需要一个快速脚本来运行预处理管道?我们让你覆盖了!加载您的文档,然后使用我们的简单处理类。它将自动过滤单词,并删除训练后空的文档。预处理方法将返回预处理和未加工文档。我们通常将未经加工的伯特(Bert)和单词袋的预处理进行了预处理。
from contextualized_topic_models . utils . preprocessing import WhiteSpacePreprocessing
documents = [ line . strip () for line in open ( "unpreprocessed_documents.txt" ). readlines ()]
sp = WhiteSpacePreprocessing ( documents , "english" )
preprocessed_documents , unpreprocessed_corpus , vocab , retained_indices = sp . preprocess ()您是否有自定义嵌入式,并想将它们用于更快的结果?只要给他们猫咪!
from contextualized_topic_models . models . kitty_classifier import Kitty
import numpy as np
# read the training data
training_data = list ( map ( lambda x : x . strip (), open ( "train_data" ). readlines ()))
custom_embeddings = np . load ( 'custom_embeddings.npy' )
kt = Kitty ()
kt . train ( training_data , custom_embeddings = custom_embeddings , stopwords_list = [ "stopwords" ])
print ( kt . pretty_print_word_classes ())注意:自定义嵌入必须是numpy.Arrays。
此包是由CookieCutter和Audreyr/CookieCutter-Pypackage项目模板创建的。为了简化库的使用,我们还包括了RBO软件包,所有权利保留给了该软件包的作者。
请记住,这是一个研究工具:)