BERTopic Documentation

repository·master·Indexed 27 days ago

https://github.com/maartengr/bertopic

BERTopic is a modular topic modeling technique that leverages transformer models and c-TF-IDF to create dense, interpretable clusters. Version 0.17.4 supports guided, supervised, semi-supervised, and multimodal modeling. The library provides a flexible pipeline for embedding, dimensionality reduction, clustering, and representation, with specialized features for dynamic topic modeling, hierarchical topic structures, and online learning via partial_fit.

Tokens
57.8K
Snippets
160
Records
247
Agent score
92%

What's inside BERTopic

  1. Understand the BERTopic algorithm pipeline

    master

    BERTopic follows a modular six-step pipeline to generate topic representations. Because each step is independent, you can swap out sub-models to build a custom topic model tailored to your data.

    The standard pipeline consists of:

    1. Embeddings: Converting documents into numerical vectors.
    2. Dimensionality Reduction: Reducing the vector space (e.g., using UMAP).
    3. Clustering: Grouping reduced embeddings (e.g., using HDBSCAN).
    4. Tokenization: Converting clusters into bag-of-words representations.
    5. Weighting Scheme: Calculating topic importance (e.g., using Class TF-IDF).
    6. Representation Tuning (Optional): Fine-tuning topic descriptions using specialized models like bertopic.representation or LLMs.
  2. Understand BERTopic Modularity

    master

    BERTopic is built as a modular pipeline. While the default sequence uses sentence-transformers, UMAP, HDBSCAN, and c-TF-IDF, you can swap out or remove any of the following steps to build customized topic models:

    1. Embedding: Embedding documents.
    2. Dimensionality Reduction: Reducing the dimensionality of embeddings.
    3. Clustering: Clustering reduced embeddings into topics.
    4. Tokenization: Tokenizing topics.
    5. Weighting: Weighting tokens.
    6. Representation: Representing topics with one or multiple representations.
  3. Understand the BERTopic algorithm steps

    master

    BERTopic follows a modular six-step pipeline to transform documents into topics:

    1. Embed documents: Converts documents into numerical representations using embedding models (default: sentence-transformers).
    2. Dimensionality reduction: Reduces the dimensionality of embeddings to handle the curse of dimensionality (default: UMAP).
    3. Cluster Documents: Groups reduced embeddings into clusters using density-based techniques (default: HDBSCAN).
    4. Bag-of-words: Combines all documents in a cluster into a single document and counts word frequencies to create a cluster-level representation.
    5. Topic representation: Uses class-based TF-IDF (c-TF-IDF) to identify words that are most representative of a specific cluster compared to others.
    6. Fine-tune Topic representation (Optional): Refines the c-TF-IDF keywords or generates summaries using external models (e.g., GPT, T5, KeyBERT) via the bertopic.representation module.
  4. Modular Architecture of BERTopic

    master

    BERTopic is designed to be modular, allowing you to swap or remove any of the following steps in the pipeline:

    1. Embedding: Document embedding.
    2. Dimensionality Reduction: Reducing embedding dimensions (e.g., UMAP).
    3. Clustering: Grouping embeddings into topics (e.g., HDBSCAN).
    4. Tokenization: Processing topic tokens.
    5. Weighting: Weighting tokens (e.g., c-TF-IDF).
    6. Representation: Describing topics with one or multiple representation models.
  5. Push and Load models from HuggingFace Hub

    master

    You can share your BERTopic models via the HuggingFace Hub.

    1. Login: Use huggingface-cli login in your terminal or from huggingface_hub import login; login() in a script.
    2. Push: Use topic_model.push_to_hf_hub() to upload your trained model.
    3. Load: Use BERTopic.load("repo_id") to download and load the model from the Hub.
    from bertopic import BERTopic
    
    # Train model
    topic_model = BERTopic().fit(my_docs)
    
    # Push to HuggingFace Hub
    topic_model.push_to_hf_hub(
        repo_id="MaartenGr/BERTopic_ArXiv",
        save_ctfidf=True
    )
    
    # Load from HuggingFace
    loaded_model = BERTopic.load("MaartenGr/BERTopic_ArXiv")
  6. Use Mistral (GGUF) via ctransformers for topic representation

    master

    To use quantized Mistral models (like Zephyr) via ctransformers, first install the necessary packages:

    pip install ctransformers[cuda]
    pip install --upgrade git+https://github.com/huggingface/transformers

    When loading the model with AutoModelForCausalLM.from_pretrained, use the gpu_layers parameter to offload layers to the GPU (set to 0 if no GPU is available). You must define a prompt template that includes [DOCUMENTS] and [KEYWORDS] tags to guide the model.

    from ctransformers import AutoModelForCausalLM
    from transformers import AutoTokenizer, pipeline
    from bertopic.representation import TextGeneration
    from bertopic import BERTopic
    
    # Load quantized model
    model = AutoModelForCausalLM.from_pretrained(
        "TheBloke/zephyr-7B-alpha-GGUF",
        model_file="zephyr-7b-alpha.Q4_K_M.gguf",
        model_type="mistral",
        gpu_layers=50,
        hf=True
    )
    tokenizer = AutoTokenizer.from_pretrained("HuggingFaceH4/zephyr-7b-alpha")
    
    # Create pipeline
    generator = pipeline(
        model=model, tokenizer=tokenizer,
        task='text-generation',
        max_new_tokens=50,
        repetition_penalty=1.1
    )
    
    # Define prompt with tags
    prompt = """<|system|>You are a helpful, respectful and honest assistant for labeling topics..</s>
    <|user|>
    I have a topic that contains the following documents:
    [DOCUMENTS]
    
    The topic is described by the following keywords: '[KEYWORDS]'.
    
    Based on the information about the topic above, please create a short label of this topic. Make sure you to only return the label and nothing more.</s>
    <|assistant|>"""
    
    # Use in BERTopic
    zephyr = TextGeneration(generator, prompt=prompt)
    representation_model = {"Zephyr": zephyr}
    topic_model = BERTopic(representation_model=representation_model, verbose=True)
  7. Configure embedding models in BERTopic

    master

    BERTopic uses sentence-transformers by default to convert documents into numerical representations.

    Default Models:

    • "all-MiniLM-L6-v2": Optimized for English semantic similarity.
    • "paraphrase-multilingual-MiniLM-L12-v2": A larger model supporting 50+ languages, selected automatically if the input language is not English.

    You can replace these with any embedding model that fits your use case.

  8. Visualize Hierarchical Topic Representations

    master

    To see how topic representations change when merging topics, you can visualize the hierarchy with specific hierarchical labels. This requires first calculating the hierarchical topics using topic_model.hierarchical_topics(docs) and then passing that result to the visualization function.

    When using the interactive visualization, hovering over the black circles reveals the topic representation at that specific level of the hierarchy, helping you identify logical merges or sub-topics within larger themes.

    from bertopic import BERTopic
    from sklearn.datasets import fetch_20newsgroups
    
    # 1. Prepare data
    docs = fetch_20newsgroups(subset='all',  remove=('headers', 'footers', 'quotes'))["data"]
    
    # 2. Train BERTopic
    topic_model = BERTopic(verbose=True)
    topics, probs = topic_model.fit_transform(docs)
    
    # 3. Calculate hierarchical topics
    hierarchical_topics = topic_model.hierarchical_topics(docs)
    
    # 4. Visualize with hierarchical labels
    topic_model.visualize_hierarchy(hierarchical_topics=hierarchical_topics)
  9. Perform multi-aspect topic modeling

    master

    Multi-aspect topic modeling allows you to generate multiple different representations (e.g., keywords, summaries, or custom labels) for a single topic during the .fit or .fit_transform stages.

    To implement this, pass a dictionary to the representation_model parameter in the BERTopic constructor.

    • The main representation (used by most visualization options) must be defined using the key "Main".
    • Additional aspects can be defined using any other keys. These keys can map to a single representation model or a list of models to be used as a pipeline.
    from bertopic.representation import KeyBERTInspired
    from bertopic.representation import PartOfSpeech
    from bertopic.representation import MaximalMarginalRelevance
    from bertopic import BERTopic
    from sklearn.datasets import fetch_20newsgroups
    
    # Documents to train on
    docs = fetch_20newsgroups(subset='all',  remove=('headers', 'footers', 'quotes'))['data']
    
    # The main representation of a topic
    main_representation = KeyBERTInspired()
    
    # Additional ways of representing a topic
    aspect_model1 = PartOfSpeech("en_core_web_sm")
    aspect_model2 = [KeyBERTInspired(top_n_words=30), MaximalMarginalRelevance(diversity=.5)]
    
    # Add all models together to be run in a single `fit` via a dictionary
    representation_model = {
       "Main": main_representation,
       "Aspect1":  aspect_model1,
       "Aspect2":  aspect_model2
    }
    
    topic_model = BERTopic(representation_model=representation_model).fit(docs)
  10. Customize LLM prompts using tags

    master

    When using text generation Large Language Models (LLMs) as representation models in BERTopic, you can customize prompts using two specific tags:

    • [KEYWORDS]: Replaced by the topic's keywords.
    • [DOCUMENTS]: Replaced by the most representative documents for the topic.

    To access the default prompts used by a model, use representation_model.default_prompt_. If you have already trained a model, you can access the prompts used during training via topic_model.representation_model.prompts_.

  11. Perform supervised topic modeling with BERTopic

    master

    You can perform supervised topic modeling by replacing the clustering step with a classification algorithm and skipping dimensionality reduction. This allows BERTopic to learn the relationship between existing labels and documents, while still generating c-TF-IDF topic representations for those labels. This approach also enables you to use .transform() to predict topics for new, unseen documents.

    To implement this, pass an instance of BaseDimensionalityReduction to umap_model to skip dimensionality reduction, and pass a classifier (e.g., LogisticRegression) to hdbscan_model to replace the clustering step.

    from bertopic import BERTopic
    from bertopic.vectorizers import ClassTfidfTransformer
    from bertopic.dimensionality import BaseDimensionalityReduction
    from sklearn.linear_model import LogisticRegression
    
    # 1. Prepare your documents and labels
    docs = ["your document 1", "your document 2"]
    y = [0, 1]
    
    # 2. Configure models to skip dimensionality reduction and use a classifier
    empty_dimensionality_model = BaseDimensionalityReduction()
    clf = LogisticRegression()
    ctfidf_model = ClassTfidfTransformer(reduce_frequent_words=True)
    
    # 3. Create the supervised BERTopic instance
    topic_model = BERTopic(
        umap_model=empty_dimensionality_model,
        hdbscan_model=clf,
        ctfidf_model=ctfidf_model
    )
    
    # 4. Fit the model using the labels (y)
    topics, probs = topic_model.fit_transform(docs, y=y)
  12. Find similar topics between different BERTopic models

    master

    If you have trained separate BERTopic models on different datasets, you can compare their topic representations. To compare topic embeddings, you must use the exact same embedding_model for both BERTopic instances.

    Once both models are fitted, you can calculate the cosine similarity between their topic_embeddings_ using sklearn.metrics.pairwise.cosine_similarity. This produces a similarity matrix that allows you to map a topic from one model to its most similar counterpart in another.