BERTopic icon indicating copy to clipboard operation
BERTopic copied to clipboard

topics_over_time run time very long after running topic_model.update_topics

Open studentdave opened this issue 4 years ago • 4 comments

####after running the following,

#build the hdbscanner
hdbscan_model_used = HDBSCAN(min_cluster_size=1000, metric='euclidean', 
                        cluster_selection_method='eom', prediction_data=True, min_samples=30)
#build the topic modeler, lots of customization done here
topic_model = BERTopic(embedding_model="all-mpnet-base-v2",
                       hdbscan_model=hdbscan_model_used,
                       min_topic_size=1000,
                       language="english", 
                       calculate_probabilities=True, 
                       verbose=True)
topics, probs = topic_model.fit_transform(docs)

####the following time analysis takes about 1.5 minutes.

%%time
topics_over_time = topic_model.topics_over_time(docs=docs, 
                                                topics=topics, 
                                                timestamps=df_docs['date_friendly'].tolist(), 
                                                global_tuning=True, 
                                                evolution_tuning=True, 
                                                nr_bins=7*24)

####BUT after i run the topic update (after the above),

vectorizer_model = CountVectorizer(ngram_range=(1, 2), stop_words="english")
topic_model.update_topics(docs, topics, vectorizer_model=vectorizer_model)

and i try to run the above time analysis again, it never finishes it just runs and runs forever. (i stop it at 10 mins) why would topic update cause this?

studentdave avatar May 27 '22 08:05 studentdave

Setting min_topic_size to 1000 indicates that you are using quite a large dataset right? That most likely means that when you use a CountVectorizer with a larger ngram_range, the size of the vocabulary explodes as many combinations of tokens will be generated. To circumvent this issue, it might be worthwhile to use the vectorizer like this instead:

vectorizer_model = CountVectorizer(ngram_range=(1, 2), stop_words="english", min_df=10)

The min_df parameter only considers words that have a minimum frequency of 10 across all documents. That will limit the size of the vocabulary, which is definitely helpful when you are using a larger ngram_range. You can set min_df even larger to further limit the size but also to remove words that are not particularly helpful since they only appear a few times.

NOTE: I edited your original post so that the code would generated in code-blocks which allows for easier reading.

MaartenGr avatar May 28 '22 06:05 MaartenGr

yes, i have 500K (and soon 5M) sentences. the vectorizer_model step only takes a few minutes to run (on 4x GPU), but it's the topics_over_time that suddenly goes from 1.5 minutes to over 10 mins (doesn't end) after i run the vectorizer_model step (that is before I run vectorizer_model, the topics_over_time only takes 1.5mins, but if i run the vectorizer_model step (which takes only a few minutes), then, the topics_over_time step takes over 10-15 mins. i'm wondering why that would happen? will take your min_df advise though as well! and thx u for the edit.

studentdave avatar May 28 '22 08:05 studentdave

but it's the topics_over_time that suddenly goes from 1.5 minutes to over 10 mins (doesn't end) after i run the vectorizer_model step (that is before I run vectorizer_model, the topics_over_time only takes 1.5mins, but if i run the vectorizer_model step (which takes only a few minutes), then, the topics_over_time step takes over 10-15 mins. i'm wondering why that would happen?

During the topics_over_time step, we convert the documents to bag-of-words representations. These representations are sparse matrices and typically n x m in size, where n is the number of documents and m the entire vocabulary. This is done for each single timestep, which means that for you it will be 7*24 times that the bag-of-word representation is being generated. There are also some calculations done with these representations to create an evolutionary view of the topics.

However, when you use a CountVectorizers that has a larger n-gram, then the resulting vocabulary will be much higher. This results in bag-of-words representations that are much larger than when you are only using 1-grams. As a result, I would expect that it takes a bit more time to generate these very large sparse matrices. Combine that with the 7*24 times that they are being generated, the result is that is takes some more time.

MaartenGr avatar May 28 '22 12:05 MaartenGr

got it! thx you. will adjust.

studentdave avatar May 28 '22 12:05 studentdave