Error while computing coherence values of topic models

Dear all,

I am building a topic model (using gensim) from a large corpus of texts, and I use the coherence value to decide on the number of topics I want the model to build.

I use the following formula to compute the coherence value for various numbers of topics:

def compute_coherence_values(dictionary, corpus, texts, limit, start, step):
    Compute c_v coherence for various number of topics

    dictionary : Gensim dictionary
    corpus : Gensim corpus
    texts : List of input texts
    limit : Max num of topics

    model_list : List of LDA topic models
    coherence_values : Coherence values corresponding to the LDA model with respective number of topics
    coherence_values = []
    model_list = []
    for num_topics in range(start, limit, step):
        model = gensim.models.wrappers.LdaMallet('PATH/mallet-2.0-4.8/bin/mallet', corpus=corpus, num_topics=num_topics, id2word=dictionary, iterations=ITERATIONS,  
                workers=N_WORKERS, optimize_interval=OPTIMIZE_INTERVAL)
        coherencemodel = CoherenceModel(model=model, texts=tokenized_texts, dictionary=dictionary, coherence='c_v')

    return model_list, coherence_values

When I run the following line of code,

model_list, coherence_values = compute_coherence_values(dictionary=dictionary, corpus=corpus, texts=tokenized_texts, start=10, limit=100, step=10)

I get this error at some point:

type: 13520 new topic: 2
0:9 5:8 
java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
	at cc.mallet.topics.WorkerRunnable.sampleTopicsForOneDoc(
	at java.base/java.util.concurrent.Executors$
	at java.base/
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(
	at java.base/java.util.concurrent.ThreadPoolExecutor$
	at java.base/

I tried to rerun the code several times, and the error always occurs at a different point.

I have used this method before and it worked fine at that time. So far existing threads on Github and StackOverflow are not clarifying it for me, so I hope there are one or two experts in this room who are better in understanding errors than I am :slight_smile:



When in your loop is “at some point”?

Could you try again with N_WORKERS = 1 (i.e. making sure it’s single core)?


Whether it’s the number of workers (changed it from 3 to 1) or the fact that I’m using a better WiFi-network today, no errors in the past 6 hours :ok_hand:t3: Thanks!

1 Like

I stumbled upon this fix:
This might allow you to use multiple workers and save you some precious :timer_clock:

1 Like


As a personal rule of thumb I always prefer training my topic models in single-core (this way, with a seed, the results are replicable). I usually run several models at the same time (so it’s multicore anyway), but the RAM requirements might be limiting if you use MALLET (in this case through the gensim wrapper).

Note that you can also use the gensim implementation of LDA (or multicore) which uses a Variational Bayes sampling instead of MALLET’s Gibbs sampler. VB will be faster but less precise, and another difference will be that with gensim you’ll stream your data (no RAM limitation then).


Thanks @melvin.wevers - I tried this because it sounded like my problem, but am getting the same error unfortunately.

Thanks for this @simon - will try to figure this out. I have a workaround for now, so that’s something. :slight_smile:

1 Like

not 100% related to your question, but might be importatnt. There are known issues associated with C_V coherence measure. So it is recommended to use C_P, NPMI or UCI for evaluating topics. See: and


If you deployed MALLET, make sure that Java was installed properly in your local/cloud server.

Here is the sample code to install Java:
First, set the install function.

def install_java(): !apt-get install -y openjdk-8-jdk-headless -qq > /dev/null os.environ["JAVA_HOME"] = "/usr/lib/jvm/java-8-openjdk-amd64"

After that, install Java.

Don’t forget to import os if you didn’t
import os

In addition, I would change the path 'PATH/mallet-2.0-4.8/bin/mallet'

For example,

os.environ['MALLET_HOME'] = '/content/mallet-2.0.8'
mallet_path = '/content/mallet-2.0.8/bin/mallet'

from gensim.models.wrappers import LdaMallet
ldamallet = LdaMallet(mallet_path, corpus=corpus, num_topics=20, id2word=id2word)