【问题标题】:pyLDAvis: Validation error on trying to visualize topicspyLDAvis:尝试可视化主题时出现验证错误
【发布时间】:2018-06-08 11:35:01
【问题描述】:

我尝试使用 gensim 为 300000 条记录生成主题。在尝试可视化主题时,我收到验证错误。我可以在模型训练后打印主题,但使用 pyLDAvis 失败

# Running and Training LDA model on the document term matrix.
ldamodel1 = Lda(doc_term_matrix1, num_topics=10, id2word = dictionary1, passes=50, workers = 4)

(ldamodel1.print_topics(num_topics=10, num_words = 10))
 #pyLDAvis
d = gensim.corpora.Dictionary.load('dictionary1.dict')
c = gensim.corpora.MmCorpus('corpus.mm')
lda = gensim.models.LdaModel.load('topic.model')

#error on executing this line
data = pyLDAvis.gensim.prepare(lda, c, d)

在 pyLDAvis 上运行后尝试出现以下错误

---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
<ipython-input-53-33fd88b65056> in <module>()
----> 1 data = pyLDAvis.gensim.prepare(lda, c, d)
      2 data

C:\ProgramData\Anaconda3\lib\site-packages\pyLDAvis\gensim.py in prepare(topic_model, corpus, dictionary, doc_topic_dist, **kwargs)
    110     """
    111     opts = fp.merge(_extract_data(topic_model, corpus, dictionary, doc_topic_dist), kwargs)
--> 112     return vis_prepare(**opts)

C:\ProgramData\Anaconda3\lib\site-packages\pyLDAvis\_prepare.py in prepare(topic_term_dists, doc_topic_dists, doc_lengths, vocab, term_frequency, R, lambda_step, mds, n_jobs, plot_opts, sort_topics)
    372    doc_lengths      = _series_with_name(doc_lengths, 'doc_length')
    373    vocab            = _series_with_name(vocab, 'vocab')
--> 374    _input_validate(topic_term_dists, doc_topic_dists, doc_lengths, vocab, term_frequency)
    375    R = min(R, len(vocab))
    376 

C:\ProgramData\Anaconda3\lib\site-packages\pyLDAvis\_prepare.py in _input_validate(*args)
     63    res = _input_check(*args)
     64    if res:
---> 65       raise ValidationError('\n' + '\n'.join([' * ' + s for s in res]))
     66 
     67 

ValidationError: 
 * Not all rows (distributions) in topic_term_dists sum to 1.

【问题讨论】:

  • 从训练文档切换到另一组文档时遇到了同样的问题。你确定它是同一个字典。可能是从旧版本加载的。
  • 检查你的语料库是否包含 NaNs、Nones、'-'s 等。这通常是因为 LDA、NMF 等不知道如何处理太短或其他的文档无效。

标签: python nlp lda topic-modeling


【解决方案1】:

这是因为 pyLDAvis 程序期望模型中的所有文档主题至少在语料库中出现一次。当您在制作语料库/文本之后和制作模型之前进行一些预处理时,可能会发生这种情况。

模型内部词典中的一个词在您提供的词典中未使用会导致此操作失败,因为现在概率略小于 1。

您可以通过将缺失的单词添加到您的语料库字典(或将单词添加到语料库并从中制作字典)来解决此问题,或者您可以将此行添加到 site-packages\pyLDAvis\gensim.py 代码在“assert topic_term_dists.shape[0] == doc_topic_dists.shape[1]”之前(应该是~第 67 行)

topic_term_dists = topic_term_dists / topic_term_dists.sum(axis=1)[:, None]

假设您的代码运行到该点,这应该重新规范主题分布,而不会丢失 dict 项。但请注意,最好将所有术语都包含在语料库中。

【讨论】:

    【解决方案2】:

    我遇到了同样的验证错误。

    我的问题是,即使 PyLDAVIS 经历了规范化步骤(请参阅 sklearn.py_row_norm)以确保 doc_topic_diststopic_term_dists 概率总和为 1,如果没有的话您的文档实际上出现在文档术语矩阵(例如 0 的矩阵)中,那么此函数不能确保您的概率等于 1。您的概率之和只能为 0!

    对您的文档向量求和。如果确实有 0,那么您可能想要删除该行/文档。

    np.sum(lda.transform(docu_term_matrix),axis=1)
    

    【讨论】:

      【解决方案3】:

      在我过滤字典后,这发生在我的 HDPModel 中 - 我留下了很多零长度文档,从而产生了这个错误。我在通过corpora.MmCorpus.serialize(args.save_folder + '/gensim.mm', (x for x in corpus if len(x) &gt; 0)) 将我的 MmCorpus 保存到磁盘之前消除了它们,这解决了稍后运行 HDP 时的问题。 corpus 是我的文本文档的生成器。

      【讨论】:

        【解决方案4】:

        我遇到了这个问题,并通过在生成语料库之前从字典中过滤掉频率非常低的单词来解决它。

        dictionary.filter_extremes(no_below=2, no_above=1.0).

        我怀疑由于浮点近似,对许多极低概率求和不会为 1。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-04-06
          • 2021-05-24
          • 1970-01-01
          • 2021-02-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多