BERTopic icon indicating copy to clipboard operation
BERTopic copied to clipboard

Hook to get topic words before representation fine-tuning

Open ronykrell opened this issue 3 years ago • 7 comments

We'd like to compare BERTopic's topic words (i.e. the output BERTopic would give without any representation fine-tuning) with that of using Open AI prompts for representation fine-tuning. It seems however that BERTopic overwrites the topic representations with the representation fine-tuning outputs. We may try to run BERTopic with and without fine-tuning and compare outputs while trying to make it as deterministic as possible, but wondering if there is a simpler way.

ronykrell avatar May 30 '23 14:05 ronykrell

This is indeed troublesome! You can use .update_topics to change the topic representations but going back and forward is not ideal.

Fortunately, in the upcoming update you will be able to model multiple topic representations simultaneously, with the option of switching between them when you run visualizations. You will be able to do the following:

from bertopic.representation import KeyBERTInspired, OpenAI

representation_model = {
    "KeyBERT": KeyBERTInspired(),
    "ChatGPT": OpenAI(...)
}

topic_model = BERTopic(representation_model=representation_model)

Then, you can view all topic representations with:

topic_model.get_topic(topic_id, full=True)

or

topic_model.get_topic_info()

I intend to release it somewhere this or next week.

MaartenGr avatar May 30 '23 15:05 MaartenGr

That sounds amazing @MaartenGr. Will there also be an option to view the original topic representations in this new release (i.e. before any fine-tuning / as if no representation_model was passed in)?

ronykrell avatar May 30 '23 15:05 ronykrell

Definitely! The example above actually is exactly that. It creates additional representations and does not overwrite them unless you name them "Main" in the dictionary.

MaartenGr avatar May 30 '23 15:05 MaartenGr

Hello @MaartenGr,

Thank you for this amazing update! I'm trying to use this new functionality on models I trained (using v 0.15.0 of BERTopic), but when viewing the topic info it only shows the "Main" representation after updating the topics, although it does seem like there is a difference in the representation. It doesn't seem to pick up that I added multiple representations, though. See code and output below:

from bertopic.representation import KeyBERTInspired
from bertopic.representation import PartOfSpeech
from bertopic.representation import MaximalMarginalRelevance

import pandas as pd

topic_model_r = topic_model

rep_keybert = KeyBERTInspired(top_n_words = 10, nr_repr_docs = 5, nr_samples = 500, nr_candidate_words = 100)
rep_mmr = MaximalMarginalRelevance(diversity = 0.3, top_n_words = 10)

repr_model = {
    "KeyBERT": rep_keybert,
    #"PartOfSpeech" : rep_pos,
    "MaximalMarginalRelevance": rep_mmr,
}

topic_model_r.representation_model = repr_model
topic_model_r.update_topics(docs)

The output for topic_model.get_topic(7, full = True) is then: {'Main': [('hands', 0.0991436191549958), ('wash', 0.06624220722798681), ('wash hands', 0.06260501711386304), ('hand', 0.06122654755730086), ('water', 0.03848137892847256), ('sanitizer', 0.0366187323278245), ('coronavirus', 0.03531214731425888), ('toilet', 0.03409063038905142), ('soap', 0.03409063038905142), ('washing', 0.032962701352822074)]}

and the output for topic_model_r.get_topic(7, full = True) is then: {'Main': [('hands', 0.10459432559788026), ('wash', 0.06996529647596977), ('hand', 0.06463832078238306), ('to', 0.055024020604969984), ('the', 0.05307875887476921), ('your', 0.05299819270362738), ('and', 0.05243881115577942), ('water', 0.04062706216131535), ('sanitizer', 0.038697571161666756), ('of', 0.037326329150152375)]}

So the first output is the original representation and the second output is altered, but it doesn't indicate which representation model is used.

Any help would be appreciated!

Spekkboom avatar Jun 09 '23 10:06 Spekkboom

I think this functionality has not been released yet in 0.15

ronykrell avatar Jun 09 '23 13:06 ronykrell

@DieSpekboom The main issue here is the use of the following:

topic_model_r.representation_model = repr_model
topic_model_r.update_topics(docs)

This behavior is not supported in BERTopic. You would instead have to use the parameter representation_model in .update_topics.

MaartenGr avatar Jun 09 '23 13:06 MaartenGr

Awesome, thank you @MaartenGr !

Spekkboom avatar Jun 09 '23 13:06 Spekkboom