I have created embeddings using SentenceTransformer and trained a BERTopic model on those embeddings.
sentence_model = SentenceTransformer("all-MiniLM-L6-v2")
embeddings = sentence_model.encode(training_docs, show_progress_bar=True)
topic_model = BERTopic().fit_transform(training_docs, embeddings)
topic_model.reduce_topics(training_docs, nr_topics=5)
I have then saved the embeddings
using pickle and topic_model using topic_model.save()
. I can also load them both but when I try to use it on a new text such as:
with open('embeddings.pickle', 'rb') as pkl:
embeddings = pickle.load(pkl)
topic_model = BERTopic.load('mybertopic')
sentence = 'I have found my car.'
topics, probs = topic_model.transform(sentence, embeddings)
I get the following error:
ValueError: Make sure that the embeddings are a numpy array with shape: (len(docs), vector_dim) where vector_dim is the dimensionality of the vector embeddings.
The embeddings are a numpy array. How do I solve this?
Okay I solved it. I have to encode my text using the same SentenceTransformer and not use the entire embeddings in transform
method.
embeddings = sentence_model.encode(sentence)
topics, probs = topic_model.transform(sentence, embeddings)
print(topics)
[-1]