MaartenGr / MaartenGr/BERTopic

BERTopic random results with random_state set when input order is changed

Open
#2,276 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I'm using BERTopic with UMAP and HDBScan. I set the random_state of `UMAP` to a specific number, but I still get wildly different results on `fit_transform` if my input array (the corpus) is in another order. For example, When I have a collection of descriptions of size ~350 and I switch the first and last element, then my amount of clusters go from 20 to 24. Is this normal? Which step in the BERTopic process creates this randomness?

Code in question where I create the model:

bi_encoder = SentenceTransformer("all-mpnet-base-v2")
embeddings = bi_encoder.encode(input_corpus, show_progress_bar=True)

umap_model = UMAP(n_neighbors=3, n_components=3, min_dist=0.1, metric="cosine", random_state=40)
hdbscan_model = HDBSCAN(prediction_data=True)
vectorizer_model = CountVectorizer(ngram_range=(1, 2), stop_words="english")

topic_model = BERTopic(
embedding_model=bi_encoder,
vectorizer_model=vectorizer_model,
hdbscan_model=hdbscan_model,
umap_model=umap_model,
verbose=True,
)

Versions:
```
bertopic==0.16.4
umap-learn==0.5.7
hdbscan==0.8.40
sentence-transformers==3.3.1
```

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

Start with the supplied BERTopic reproduction and compare results after swapping the first and last corpus elements. Isolate the stages named in the report—fit_transform, UMAP, and HDBSCAN—to determine where order sensitivity appears. Done means identifying the responsible stage and documenting whether the behavior is expected or what actionable correction is needed.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.