MaartenGr / MaartenGr/BERTopic

Representation has >=30 terms when using MMR to update topics if pre-calculated embeddings are used

Open
#2,323 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
7.8k
Forks
920
Avg merge
22h 24m
Merged PRs (30d)
5

Description

### Have you searched existing issues? 🔎

- [x] I have searched and found no existing issues

### Desribe the bug

See evaluation results below. Basically, using MMR with `update_topics` ignores the `top_n_words` parameter (always returns at least 30), but other representation models work as expected. This only happens when pre-calculated embeddings are passed to BERTopic: `model.fit_transform(text, embeddings=embeddings)`

I think it's related to `_extract_words_per_topic`, which retrieves at least the top 30 terms before reducing it back to `self.top_n_words`. Maybe something isn't being set right internally?

A minor bug at any rate.

### Reproduction

```python
import os
import pandas as pd

from bertopic import BERTopic
from bertopic.representation import MaximalMarginalRelevance, PartOfSpeech
from sentence_transformers import SentenceTransformer

text =

embedding_model = SentenceTransformer('all-MiniLM-L12-v2')
sent_encoded = embedding_model.encode(text, convert_to_tensor=False)

model = BERTopic()
topics, probs = model.fit_transform(text , embeddings=sent_encoded)

print(model.top_n_words) # -> set to 10, as expected
print(model.get_topic(0, True)) # -> 10 terms

# Update topics with PartOfSpeech
model.update_topics(text, representation_model=PartOfSpeech())

print(model.top_n_words) # -> set to 10, as expected
print(model.get_topic(0, True)) # -> 10 terms

# Update topics with MMR
model.update_topics(text, representation_model=MaximalMarginalRelevance())

print(model.top_n_words) # -> still 10, as expected
print(model.get_topic(0, True)) # -> 30 terms?
```

### BERTopic Version

0.17.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the provided reproduction with pre-calculated embeddings, then inspect update_topics and _extract_words_per_topic, which the report identifies as the likely path. Compare MMR with PartOfSpeech and verify that MMR respects top_n_words and returns the configured number of terms.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.