2
$\begingroup$

I've used BERTopic with success for the following tasks: get topics, visualise (topics, barcharts, documents ...) and DTM (extended to get area plot with considerable success).

However, I am unable to use the find_topics() function

(There are a few others I'm struggling with, which I'll post as new questions so as not to conflate this one).

I get an error message indicating that I'm using embedding (which is true).

# Prepare embeddings using default 'sentence embedding'
sentence_model = SentenceTransformer("all-MiniLM-L6-v2")
embeddings = sentence_model.encode(docs_bert, show_progress_bar=True)

Trying to solve that, I have tried to instantiate a new model without embedding

model_ngram_embed2 = BERTopic(embedding_model=embeddings)

but it then throws an error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I need to instantiate before I can fit_transform the model to my doc (text corpus), after which I would then be able to find_topics().
How do I go about that? What should be done?

Regarding find_topics(), I've read allowing precomputed embeddings in bertopic.find_topics() issue
NB: Python 3.8.8 | IPython 7.31.1 | BERTopic 0.11.0

$\endgroup$

1 Answer 1

0
$\begingroup$

To resolve this, I did the following.
Firstly, I updated BERTopic to 0.12.0. This did not resolve on its own.
What works is excluding seed_topics: i.e. not using semi-supervised mode.

%%time
## instantiate BERTopic with bigram
model_ngram_noembed2 = BERTopic(top_n_words=10, min_topic_size=5, n_gram_range=(1,2), nr_topics=41, verbose=True)  
                     #, embedding_model=embeddings, seed_topic_list=seed_topic_list)

## Fit the models on doc with embedding, generate topics, and return the docs with topics
topics_bert_noembed2, probs_noembed2 = model_ngram_noembed2.fit_transform(docs_bert_dtm)

For completeness, the rest are

## Explicate 'define' within the topic model
keyterm_explore = 'define'

#pd_keyterm_explore = pd.DataFrame(topics_bert.find_topics(keyterm_explore,))
'''
Exception: This method can only be used if you did not use custom embeddings.'''

pd_keyterm_explore = pd.DataFrame(model_ngram_noembed2.find_topics(keyterm_explore), 
             columns=model_ngram_noembed2.find_topics(keyterm_explore)[0],
            dtype='float').round(decimals=2)

print(f'Exploring {keyterm_explore}: \n{pd_keyterm_explore}')
    [out]
    Exploring define: 
        21     12     19    7     3 
    0  21.00  12.00  19.00  7.00  3.00
    1   0.37   0.36   0.34  0.32  0.32
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.