MaartenGr / MaartenGr/BERTopic

Zero shot topic model with pre embedded zero shot topics

Open
#2,014 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

_Preface, I have tried to read through the current issues. I dont think that any issues raises what I am wanting. Issues like this https://github.com/MaartenGr/BERTopic/issues/2011 sound promising but is talking about something different. I apologise if this has already been discussed!_

I would like try out BERTopics zero shot modelling while using a proprietary embeding model (voyageai). Therefore I need to give BERTopic the embeddings for both the documents and zero shot topics.

An example would be something like this:
```python
from datasets import load_dataset

dataset = load_dataset("CShorten/ML-ArXiv-Papers")["train"]
docs = dataset["abstract"][:5_000]

zeroshot_topic_list = ["Clustering", "Topic Modeling", "Large Language Models"]

zeroshot_topic_list_embeddings = np.random.rand(len(zeroshot_topic_list), 1024).astype(np.float32)
document_embeddings = np.random.rand(len(docs), 1024).astype(np.float32)

topic_model = BERTopic(
embedding_model=None,
min_topic_size=5,
zeroshot_topic_list=zeroshot_topic_list,
embedded_zeroshot_topic_list=zeroshot_topic_list_embeddings
zeroshot_min_similarity=0.85
)

topics, _ = topic_model.fit_transform(docs, document_embeddings)

topic_model.get_topic_info()
```

Am I missing something with how BERTopic and zero-shot models should be working? If not I am happy to make PR with what seems to be the small changes that need to be made.

**Potential solution**
I have had a look through `_bertopic.py` and it seems to be a relatively straight forward process.
It seems that [here](https://github.com/MaartenGr/BERTopic/blob/be9376c99dba157707286b4d828277b5f3627572/bertopic/_bertopic.py#L3554) it could just pass it the given zero-shot topic embedidngs. These embeddings would come from another `init` arugment.
Then besides a few other changes like the `_is_zeroshot()` method.

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 in bertopic/_bertopic.py, especially the area around the linked line and the _is_zeroshot() method. Read how fit_transform currently handles document embeddings and zero-shot topics, then run the supplied Python example to understand the expected inputs. Done means BERTopic accepts precomputed zero-shot topic embeddings alongside document embeddings and produces topic information as shown.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
machine-learning
Issue type
Feature
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.