SentenceTransformer社区检测代码卡在community_detection行求助
调试建议
针对你遇到的util.community_detection无响应问题,给出以下具体调试步骤:
调整阈值参数:你设置的
threshold=0.2过低,对于语义相似度较高的文本(你的3个句子均为物流跟踪相关),过低的阈值会让算法构建出过于密集的关联图,导致计算陷入死循环或超长耗时。建议先尝试将阈值调高到0.7-0.9区间,示例代码:clusters = util.community_detection(embeddings3, threshold=0.8, min_community_size=1)先手动计算相似度矩阵:提前生成相似度矩阵,确认矩阵是否正常,再传入社区检测函数,排查是否是矩阵生成环节的问题:
# 先计算余弦相似度矩阵 cos_sim = util.cos_sim(embeddings3, embeddings3) print(cos_sim) # 传入矩阵执行社区检测 clusters = util.community_detection(cos_sim, threshold=0.8, min_community_size=1)检查库版本兼容性:不同版本的
sentence-transformers中community_detection的实现可能存在差异。尝试升级到最新稳定版:pip install --upgrade sentence-transformers若升级后问题依旧,可尝试降级到已知稳定版本(如2.2.2)测试。
强制使用CPU运行:Apple Silicon的GPU加速可能存在兼容性问题,强制切换到CPU运行排查:
model = SentenceTransformer('all-MiniLM-L6-v2', device='cpu')添加调试日志:在调用社区检测前后加入打印语句,确认程序确实卡在该步骤,同时可以验证流程节点:
print("开始执行社区检测...") clusters = util.community_detection(embeddings3, threshold=0.8, min_community_size=1) print("社区检测完成,结果:", clusters)
内容的提问来源于stack exchange,提问作者Pushkar Kathayat
相关产品推荐
相关产品推荐

