Gensim相似性:添加文档或实时训练

4

关于这个项目的一些背景。我有带有标识符和文本的副本,例如{name: "sports-football", text: "与足球运动相关的内容"}

我需要在这个语料库中找到给定文本输入的正确匹配项。 但是,我使用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

非常感谢您帮助我处理这个问题。

期待着令人惊叹的回复。

1个回答

1
后面错误(在稀疏矩阵中出现AssertionError: mismatch between supplied and computed number of non-zeros)很可能来自于警告所暗示的问题 - perwordbound 溢出,使用其未定义值计算的矩阵更新失败。
我建议使用更大的批次(而不是单个查询)更新模型。您正在尝试使用相对较少的单词更新具有不成比例数量的单词、模型中的单词计数。对于浮点数,这可能会导致微妙的错误
请再次尝试使用与模型源数据大小成比例的批次更新模型(例如,其大小的1/10或1/20)。

根据此帖子进行修订:

Melissa Roemmele写道:

顺便说一下,当我尝试在一个词袋语料库上创建LSI索引而没有先将其转换为tf-idf时,我也遇到了这个错误。我可以在词袋模型上构建LSI模型,但是为它构建索引时会出现错误。

您可能需要在将QueryText传递给模型之前先尝试使用tf-idf。


我一定会尝试。但是,这并不能解决我的问题。我需要的是在需要时继续训练模型。那么我需要遵循哪些步骤呢? - Nitheen Rao
这里有一个 GitHub 仓库,如果有人想要查看的话。https://github.com/nithinshiriya/NLPLive - Nitheen Rao
你是否像所引用的回复建议的那样应用了tf-idf? - sophros
这个问题还没有解决。我已经在 GitHub 上查看过 gensim 的问题,但是没有找到解决办法。 - Gaurav Koradiya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接