如何在Gensim的LdaModel中记录纪元

时间:2019-04-15 17:34:09

标签: python python-3.x gensim

我试图在LdaModel中显示学习进度,但是我在网上找到的每个样本都会引发异常:

l =  gensim.models.callbacks.CoherenceMetric(corpus=common_corpus, logger='shell')
lda = gensim.models.ldamodel.LdaModel(doc_term_matrix, num_topics=genres_count, id2word = common_corpus, passes=150, callbacks=[l])

投掷:

  File "<ipython-input-165-6ad0e2e8516c>", line 2, in <module>
    lda = gensim.models.ldamodel.LdaModel(doc_term_matrix, num_topics=genres_count, id2word = common_corpus, passes=150, callbacks=[l])

  File "C:\Users\me\AppData\Local\Continuum\anaconda3\lib\site-packages\gensim\models\ldamodel.py", line 371, in __init__
    self.update(corpus, chunks_as_numpy=use_numpy)

  File "C:\Users\me\AppData\Local\Continuum\anaconda3\lib\site-packages\gensim\models\ldamodel.py", line 750, in update
    current_metrics = callback.on_epoch_end(pass_)

  File "C:\Users\me\AppData\Local\Continuum\anaconda3\lib\site-packages\gensim\models\callbacks.py", line 288, in on_epoch_end
    value = metric.get_value(topics=topics, model=self.model, other_model=self.previous)

  File "C:\Users\me\AppData\Local\Continuum\anaconda3\lib\site-packages\gensim\models\callbacks.py", line 105, in get_value
    coherence=self.coherence, topn=self.topn

  File "C:\Users\me\AppData\Local\Continuum\anaconda3\lib\site-packages\gensim\models\coherencemodel.py", line 190, in __init__
    self.window_size = SLIDING_WINDOW_SIZES[self.coherence]

KeyError: None

此代码(找到here):

class EpochLogger(CallbackAny2Vec):
    '''Callback to log information about training'''

    def __init__(self):
        self.epoch = 0

    def on_epoch_begin(self, model):
        print("Epoch #{} start".format(self.epoch))

    def on_epoch_end(self, model):
        print("Epoch #{} end".format(self.epoch))
        self.epoch += 1

l = EpochLogger()
lda = gensim.models.ldamodel.LdaModel(doc_term_matrix, num_topics=genres_count, id2word = common_corpus, passes=150, callbacks=[l])

投掷:

Traceback (most recent call last):

  File "<ipython-input-167-e89e2bf41977>", line 1, in <module>
    lda = gensim.models.ldamodel.LdaModel(doc_term_matrix, num_topics=genres_count, id2word = common_corpus, passes=150, callbacks=[l])

  File "C:\Users\me\AppData\Local\Continuum\anaconda3\lib\site-packages\gensim\models\ldamodel.py", line 371, in __init__
    self.update(corpus, chunks_as_numpy=use_numpy)

  File "C:\Users\me\AppData\Local\Continuum\anaconda3\lib\site-packages\gensim\models\ldamodel.py", line 688, in update
    callback.set_model(self)

  File "C:\Users\me\AppData\Local\Continuum\anaconda3\lib\site-packages\gensim\models\callbacks.py", line 264, in set_model
    if any(metric.logger == "visdom" for metric in self.metrics):

  File "C:\Users\me\AppData\Local\Continuum\anaconda3\lib\site-packages\gensim\models\callbacks.py", line 264, in <genexpr>
    if any(metric.logger == "visdom" for metric in self.metrics):

AttributeError: 'EpochLogger' object has no attribute 'logger'

目前,我最感兴趣的是监控学习进度(达到眼球ETA)。

设置回调的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

更新自:

l =  gensim.models.callbacks.CoherenceMetric(corpus=common_corpus, logger='shell')

收件人:

l =  gensim.models.callbacks.CoherenceMetric(corpus=common_corpus, coherence="u_mass", logger='shell')

“ u_mass”只需要一个语料库。