BERTopic [不均匀形状未解决] [Colab] ValueError: 使用序列设置数组元素,在1个维度之后,请求的数组具有不均匀的形状,检测到的形状是(2,) + 不均匀部分,

d7v8vwbk  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(138)

使用BERTopic进行一些研究任务时,我得到了:
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2,) + inhomogeneous part
是否有计划支持 BERTopicnumpy >1.23.5 以及 numba > 0.56.4BERTopic ?
这将使得在没有手动降级(!pip install numba==0.56.4)的情况下更容易在Colab中使用BERTopic,尤其是对于那些没有Colab.pro的人。
谢谢。
[注意]
相关问题:closed | [ #1697 , #1602 , #1309 ]
相关问题:open | [ #1814 , #1799 , #1684 , #1584 , #1421 , #1269 ]

SO: https://stackoverflow.com/a/76504825

PS:我收集到的一个解决方法是降级numpy!
在问题 #1421 中:@aaron-imani 在 _guided_topic_modelling() 中暗示了 np.average() 。@MaartenGr指出,将 numba 设置为 0.56.4 或更早的时间应该理想地解决了这个问题。
感谢 @MaartenGr 在之前的问题的参与和建议。

环境: colab.research.google.com

注:(截至2024年4月26日,Colab运行的是 python: 3.10.12, bertopic: 0.16.1, numpy: 1.25.2, numba: 0.58.1 )

BERTopic:

  1. # Install BERTopic
  2. !pip install bertopic
  3. # Load libraries
  4. from bertopic import BERTopic
  5. from bertopic.representation import KeyBERTInspired
  6. from bertopic.vectorizers import ClassTfidfTransformer
  7. from umap import UMAP
  8. from sentence_transformers import SentenceTransformer

[代码片段]

  1. # Train BERTopic
  2. topic_model_03 = BERTopic(
  3. verbose=True,
  4. min_topic_size= 5, #10, #12, #15, ## the higher, the lower the clusters/topics
  5. nr_topics = 4, #5, ## reduce the initial number of topics
  6. #seed_topic_list=seed_topic_list, ##ValueError: ...
  7. n_gram_range = (1,3), ## n-gram range for the CountVectorizer
  8. zeroshot_topic_list=zeroshot_topic_list,
  9. zeroshot_min_similarity=.35, #.55, #.75, #.85,
  10. embedding_model="thenlper/gte-small", ## pass string directly to sbert sentence-transformers models
  11. #umap_model = umap_model, ## dimensionality
  12. ctfidf_model = ctfidf_model,
  13. representation_model=KeyBERTInspired(),
  14. )

种子主题看起来像这样(部分被屏蔽)

  1. ### Try with Guided Representation
  2. seed_topic_list = [["sustainability", "...", "sustain", "...", "..."],
  3. ["climate change", "climate", "...", "...", "...", "...", "ozone"],
  4. ["social justice", "social", "...", "...", "...", "...", "...", "..."],
  5. ["net zero", "..."]
  6. ]
  7. ## fit and transform
  8. #topics_03, probs_03 = topic_model_03.fit_transform(docs)
  9. ## visualise documents' topics spread
  10. #topic_model_03.visualize_documents(docs)
dxpyg8gm

dxpyg8gm1#

是否有计划支持BERTopic以支持numpy >1.23.5和numba > 0.56.4?
我认为这只是特定于有种子的主题建模的情况。所有其他BERTopic示例都应该支持这些版本。话虽如此,我确实更希望更新这一行:
BERTopic/bertopic/_bertopic.py
127e794中的第3762行| embeddings[indices] =np.average([embeddings[indices], seed_topic_embeddings[seed_topic]], weights=[3, 1]) |
将其改为仍然进行加权平均,但不需要使用weights参数(我认为这是主要问题所在)。最好的话,我希望完全去掉对np.average的使用,因为它在使用numba时会带来很多问题。相反,手动进行加权平均可能是一个简单的解决方案,即通过将embeddings相乘、加上seed_topic_embeddings并除以4来实现。

相关问题