Colab中导入SentenceTransformer失败,报错AttributeError求助
解决Colab中导入SentenceTransformer的AttributeError问题
执行from sentence_transformers import SentenceTransformer时触发错误链:
AttributeError: module 'tensorflow._api.v2.compat.v2.internal' has no attribute 'register_load_context_function'
后续引发RuntimeError,提示无法导入transformers.modeling_tf_utils、transformers.integrations.integration_utils模块
已尝试重装TensorFlow、transformers和sentence-transformers但无效,可通过以下方法修复:
锁定兼容版本
Colab默认TensorFlow版本与最新transformers存在兼容性问题,安装指定匹配版本:!pip uninstall -y tensorflow transformers sentence-transformers !pip install tensorflow==2.15.0 transformers==4.37.2 sentence-transformers==2.2.2切换到PyTorch环境
SentenceTransformers原生支持PyTorch,可彻底避开TensorFlow依赖冲突:!pip uninstall -y tensorflow !pip install torch torchvision torchaudio sentence-transformers重启运行时
安装完成后,点击Colab顶部「Runtime」→「Restart runtime」,再重新执行导入代码。
内容的提问来源于stack exchange,提问作者A1iMansour
相关产品推荐
相关产品推荐

