MaartenGr / MaartenGr/BERTopic

Topic names not set to zero-shot topic when `fit_transform` results in only zero-shot topics

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

The names for the topics are not set to the zero-shot topic names when the `fit_transform` results in only zero-shot topics .

If the result contains zero-shot topics and non-zero-shot topics the zero-shot topics get their name from `zeroshot_topic_list`. However when there are only zero-shot topics (by using a very low `zeroshot_min_similarity` for example) all documents get assigned to zero-shot topics, but the names are not taken from the list.

**Name for topic when running reproduction code:** 0_optimization_prediction_algorithms_optimal
**Expected name for topic:** Clustering

[Looking at the code](https://github.com/MaartenGr/BERTopic/blob/0bed25cdac0445fe01869e47b3b0beeaf124230e/bertopic/_bertopic.py#L489C1-L499C45), when not all documents are assigned to zeroshot topics `len(documents) > 0` is true and `combine_zeroshot_topics` is called where the topic/label mapping is updated. When all documents have been assigned this function is not called and the mapping is not updated.
```python
if len(documents) > 0:
# Cluster reduced embeddings
documents, probabilities = self._cluster_embeddings(umap_embeddings, documents, y=y)
if self._is_zeroshot() and len(assigned_documents) > 0:
documents, embeddings = self._combine_zeroshot_topics(
documents, embeddings, assigned_documents, assigned_embeddings
)
else:
# All documents matches zero-shot topics
documents = assigned_documents
embeddings = assigned_embeddings
```

### Reproduction

```python
from datasets import load_dataset

from bertopic import BERTopic
from bertopic.representation import KeyBERTInspired

# We select a subsample of 100 abstracts from ArXiv
dataset = load_dataset("CShorten/ML-ArXiv-Papers")["train"]
docs = dataset["abstract"][:100]

# We define a number of topics that we know are in the documents
zeroshot_topic_list = ["Clustering"]

# We fit our model using the zero-shot topics
# and we define a minimum similarity. For each document,
# if the similarity does not exceed that value, it will be used
# for clustering instead.
topic_model = BERTopic(
embedding_model="thenlper/gte-small",
min_topic_size=15,
zeroshot_topic_list=zeroshot_topic_list,
zeroshot_min_similarity=.000001, # Low value to make sure all documents are assigned to zero-shot topics
representation_model=KeyBERTInspired()
)
topics, _ = topic_model.fit_transform(docs)
print(topic_model.get_topic_info())

```

### BERTopic Version

0.17.3

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 around the fit_transform branch linked in the issue, especially the all-zero-shot-documents path and _combine_zeroshot_topics. Run the supplied reproduction with BERTopic 0.17.3 and inspect get_topic_info(); done means the sole zero-shot topic uses the name "Clustering" when every document is assigned to it.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.