我使用SentenceTransformer创建了嵌入,并对这些嵌入进行了BERTopic模型的培训。
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)然后,我使用泡菜保存了embeddings,使用topic_model.save()保存了topic_model。我也可以同时加载它们,但是当我尝试在一个新的文本上使用它时,例如:
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)我得到以下错误:
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. 嵌入是一个numpy数组。我该怎么解决这个问题?
发布于 2022-11-18 16:54:58
好吧我解决了。我必须使用相同的SentenceTransformer对文本进行编码,而不是在transform方法中使用整个嵌入。
embeddings = sentence_model.encode(sentence)
topics, probs = topic_model.transform(sentence, embeddings)
print(topics)
[-1]https://stackoverflow.com/questions/74492719
复制相似问题