'How to fix LDA model coherence score runtime Error?

text='Alice is a student.She likes studying.Teachers are giving a lot of homewok.'

I am trying to get topics from a simple text(like above) with coherance score.This is my LDA model:

id2word = corpora.Dictionary(data_lemmatized)
texts = data_lemmatized
corpus = [id2word.doc2bow(text) for text in texts]

lda_model = gensim.models.ldamodel.LdaModel(corpus=corpus,
                                           id2word=id2word,
                                           num_topics=5, 
                                           random_state=100,
                                           update_every=1,
                                           chunksize=100,
                                           passes=10,
                                           alpha='auto',
                                           per_word_topics=True)
# Print the Keyword in the 10 topics
pprint(lda_model.print_topics())
doc_lda = lda_model[corpus]

When i try to run this coherance model:

coherence_model_lda = CoherenceModel(model=lda_model, texts=data_lemmatized, dictionary=id2word, 
coherence='c_v')
coherence_lda = coherence_model_lda.get_coherence()
print('\nCoherence Score: ', coherence_lda)

I am supposed to get this king of output-> Coherence Score: 0.532947587081

I get this error: raise RuntimeError(''' RuntimeError: An attempt has been made to start a new process before the current process has finished its bootstrapping phase.

    This probably means that you are not using fork to start your
    child processes and you have forgotten to use the proper idiom
    in the main module:

        if __name__ == '__main__':
            freeze_support()
            ...

    The "freeze_support()" line can be omitted if the program
    is not going to be frozen to produce an executable.

What should i do to fix this?



Solution 1:[1]

I have faced the same issue. Adding 'Coherence Model' inside if__name__=="main" resolved the issue for me.

if __name__ == "__main__":

     coherence_model_lda = CoherenceModel(model=lda_model, texts=data_lemmatized, 
                                                          dictionary=id2word, 
                                                              coherence='c_v')
     coherence_lda = coherence_model_lda.get_coherence()
     print('\nCoherence Score: ', coherence_lda)

Solution 2:[2]

I'm having the same issue when running gensim Nmf and the way to fix it was to change from coherence='c_v' to coherence='u_mass'

Solution 3:[3]

You can use coherence='c_v' without problem. My answer is quite similar to AKHILA one. But I call freeze_support() in the main process and start the method with support for Windows.

Consider the structure from the beginning:

# imports
from multiprocessing import Process, freeze_support
import ...

# general constants and variables
...

# functions definition
def ...
...

def ...
...

# main function
def principal(): # can be another name
...
...

if __name__ == '__main__':
  freeze_support()
  Process(target=main).start()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AKHILA SRI MANASA
Solution 2 tezzaaa
Solution 3 Eduardo Freitas