python - Gensim.Similarity 添加文档或实时培训

标签 python nlp similarity gensim

关于这个项目的一些背景。我有带有标识符和文本的副本,例如{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

非常感谢,帮我解决这个问题。 期待精彩的回复。

最佳答案

后来的错误(AssertionError:稀疏矩阵中提供的和计算的非零数之间不匹配)很可能来自警告建议的问题 - perwordbound溢出并且使用其 undefined 值计算的矩阵更新失败。

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

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


修订,基于 this thread :

Melissa Roemmele wrote:

FYI, I also got this error when I tried to create an LSI index for a corpus on a bag-of-words corpus without first transforming it into tf-idf. I could build the LSI model on the bag-of-words but building the index for it gave me the error.

在将 QueryText 传递给模型之前,您可能想先尝试 tf-idf。

关于python - Gensim.Similarity 添加文档或实时培训,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48411780/

相关文章:

gensim - 使用 Spacy 在 doc 中查找最相似的句子

python-3.x - 如何将注意力层添加到 Bi-LSTM

scala - Spark 中通过 DIMSUM 进行余弦相似度计算

string - 查找具有相似文本的文章的算法

python - Scrapy:crawlspider 不生成嵌套回调中的所有链接

java - 使用 GATE twitter 模型的斯坦福 POS 标记器速度很慢

scikit-learn - TfidfVectorizer 如何计算测试数据的分数

python - 通过将三个列表的元素添加在一起来创建新列表 - 关联错误(PYTHON)

python - 无法使用 PySpark pickle listreverseiterator 对象

python - 如何在 Python 中从 3 个列表创建 3 维字典