MaartenGr / MaartenGr/BERTopic

Update topic embeddings and representations when merging clusters in `merge_models`

Open
#2,431 1 comment 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

### Feature request

Improve `BERTopic.merge_models` so that when two clusters from different models are merged, the resulting topic metadata (embeddings, representation, etc.) is updated to better reflect the new cluster.

### Motivation

I’ve been experimenting with `BERTopic.merge_models` and noticed a potential improvement regarding how cluster merges are handled.

Currently, when two clusters from different models are considered similar enough and are merged into a single topic in the resulting model, the merged topic keeps the embedding, topic representation, topic label, and topic aspects from the baseline model only. The contribution from the second model is not reflected in the merged topic.

This can lead to a merged topic that does not fully represent all documents from both models, especially if the second model has a substantial number of documents in that cluster.

### Your contribution

I suggest:
- Updating `topic_embeddings_` by averaging or weighted averaging embeddings from all merged clusters.
- Combining `topic_representations_` (e.g., recomputed c-TF-IDF).
- Combining `topic_labels_` and `topic_aspects_` (e.g., recalculate topic labels based based on new topic representation; recompute topic aspects)

This would make `merge_models` produce topics that more accurately represent the union of all merged clusters.

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 at the BERTopic.merge_models entry point and inspect how merged topics currently retain topic_embeddings_, topic_representations_, topic_labels_, and topic_aspects_. Define how metadata from both clusters should be combined, then verify that the resulting topic reflects the union of both models' clusters.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.