【问题标题】:Gensim.Similarity Add document or Live trainingGensim.Similarity 添加文档或实时培训
【发布时间】:2018-01-23 22:19:37
【问题描述】:

关于这个项目的一些背景知识。我有带有标识符和文本的副本,例如{name: "sports-football", text: "Content related to football sports"}.

我需要在这个语料库中为给定的文本输入找到正确的匹配。 但是,我能够使用 Gensim 实现一些目标。与 LDA 和 LSI 模型相似。

如何使用新文档更新Genism.Similarity 索引。这里的想法是在实时阶段继续训练模型。

这是我遵循的步骤。

QueryText = "瓜迪奥拉将莱昂内尔·梅西调到了 9 号位置,这样他就不必靠得太深了,我认为阿圭罗经常退回到更靠后的位置。"

注意:有些代码只是外行

索引是使用

创建的
`similarities.Similarity(indexpath, model,topics)`
  1. 创建字典

    dictionary = Dictionary(QueryText )

  2. 创建语料库

    corpus = Corpus(QueryText, dictionary)

  3. 创建 LDA 模型

    LDAModel = ldaModel(corpus,dictionary)

更新现有的字典、模型和索引

更新现有字典

existing_dictionary.add_document(dictionary)

更新现有的 LDA 模型

existing_lda_model.update(corpus)

更新现有的相似度索引

existing_index.add_dcoument(LDAModel[corpus])

除了以下警告更新似乎有效。

gensim\models\ldamodel.py:535: RuntimeWarning: overflow encountered in exp2 perwordbound, np.exp2(-perwordbound), len(chunk), corpus_words

让我们对查询文本运行相似度

vec_bow = dictionary.doc2bow(QueryText) 
vec_model = existing_lda_model[vec_bow] 
sims = existing_index[vec_model]

但是,它失败并出现以下错误。

Similarity index with 723 documents in 1 shards (stored under \Files\models\lda_model)
Similarity index with 725 documents in 0 shards (stored under \Files\models\lda_model)
\lib\site-packages\gensim\models\ldamodel.py:535: RuntimeWarning: overflow encountered in exp2
  perwordbound, np.exp2(-perwordbound), len(chunk), corpus_words
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-3-8fe711724367> in <module>()
     45 trigram = Trigram.apply_trigram_model(queryText, bigram, trigram)
     46 vec_bow = dictionry.doc2bow(trigram)
---> 47 vec_model =  lda_model[vec_bow]
     48 print(vec_model)
     49 

~\Anaconda3\envs\lf\lib\site-packages\gensim\models\ldamodel.py in __getitem__(self, bow, eps)
   1103             `(topic_id, topic_probability)` 2-tuples.
   1104         """
-> 1105         return self.get_document_topics(bow, eps, self.minimum_phi_value, self.per_word_topics)
   1106 
   1107     def save(self, fname, ignore=('state', 'dispatcher'), separately=None, *args, **kwargs):

~\Anaconda3\envs\lf\lib\site-packages\gensim\models\ldamodel.py in get_document_topics(self, bow, minimum_probability, minimum_phi_value, per_word_topics)
    944             return self._apply(corpus, **kwargs)
    945 
--> 946         gamma, phis = self.inference([bow], collect_sstats=per_word_topics)
    947         topic_dist = gamma[0] / sum(gamma[0])  # normalize distribution
    948 

~\Anaconda3\envs\lf\lib\site-packages\gensim\models\ldamodel.py in inference(self, chunk, collect_sstats)
    442             Elogthetad = Elogtheta[d, :]
    443             expElogthetad = expElogtheta[d, :]
--> 444             expElogbetad = self.expElogbeta[:, ids]
    445 
    446             # The optimal phi_{dwk} is proportional to expElogthetad_k * expElogbetad_w.

IndexError: index 718 is out of bounds for axis 1 with size 713

我真的很感激,帮助我解决这个问题。 期待精彩的回复。

【问题讨论】:

    标签: python nlp similarity gensim


    【解决方案1】:

    后面的错误(稀疏矩阵中的AssertionError: mismatch between supplied and computed number of non-zeros)很可能来自警告提示的问题 - perwordbound 溢出并且使用其 undefined 值计算的矩阵更新失败。

    我建议使用更大的批次(而不是单个查询)更新模型。模型中的单词数量可能不成比例,您尝试使用相对较少的单词进行更新。对于浮点数,这可能会导致subtle errors

    再次,请尝试使用与模型源数据成比例的批量更新模型(例如,其大小的 1/10、1/20)。


    修订,基于this thread

    梅丽莎·罗梅勒写道:

    仅供参考,当我尝试为 没有首先将其转换为词袋语料库的语料库 TF-IDF。我可以在词袋上构建 LSI 模型,但是构建 它的索引给了我错误。

    您可能想先尝试 tf-idf,然后再将 QueryText 传递给模型。

    【讨论】:

    • 我坚决尝试。但是,它不会解决我的问题。我需要的是在我们需要的时候继续训练模型。那么我需要遵循哪些步骤?。
    • 如果有人想看一下,这里是一个 git hub repo。 github.com/nithinshiriya/NLPLive
    • 您是否按照引用的回复建议应用了 tf-idf?
    • 这个问题还没有解决。我已经在 github 上解决了 gensim 问题,但没有找到运气。
    猜你喜欢
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 2017-10-18
    • 2021-11-28
    • 2012-06-15
    • 2017-12-31
    相关资源
    最近更新 更多